博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu--1540 Tunnel Warfare(线段树+区间合并)
阅读量:4363 次
发布时间:2019-06-07

本文共 3705 字,大约阅读时间需要 12 分钟。

Description

During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones. 
Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately! 

Input

The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event. 
There are three different events described in different format shown below: 
D x: The x-th village was destroyed. 
Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself. 
R: The village destroyed last was rebuilt. 

Output

Output the answer to each of the Army commanders’ request in order on a separate line. 

Sample Input

7 9D 3D 6D 5Q 4Q 5RQ 4RQ 4

Sample Output

1024 题意:在一条地道上有n个村庄,进行m次操作,D操作是摧毁村庄X,Q操作是询问村庄X能与多少村庄是连续的,R操作是重建上一个被摧毁的村庄。打印每次询问结果 思路:首先建树,a[k].lazy为1说明[ a[k].l,a[k].r ]区间内村庄全部完好,a[k].lazy为0说明[ a[k].l,a[k].r ]区间内存在村庄被摧毁。用lm,rm和m分别表示从左端点往右数最大连续村庄数,从右端点往左数最大连续村庄数和区间内的最大连续村庄数。然后进行单点更新,有pushup对父节点进行更新。最后进行询问,用到的是二分的思想,分别对左子树和右子树进行统计。 AC代码:
1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 const int maxn=50000+10; 7 int num[maxn]; 8 struct note 9 { 10 int l,r,lm,rm,m,lazy; 11 }a[maxn<<2]; 12 int max(int a,int b) 13 { 14 if(a>b) 15 return a; 16 else 17 return b; 18 } 19 int build(int l,int r,int k) 20 { 21 a[k].l=l,a[k].r=r,a[k].lm=a[k].rm=a[k].m=(r-l+1); 22 a[k].lazy=1; 23 if(l==r) 24 return 0; 25 int mid=(l+r)/2; 26 build(l,mid,k*2); 27 build(mid+1,r,k*2+1); 28 return 0; 29 } 30 int pushup(int k) 31 { 32 if(a[k*2].rm<(a[k*2].r-a[k*2].l+1)) 33 a[k].lm=a[k*2].lm; 34 else 35 a[k].lm=a[k*2].rm+a[k*2+1].lm; 36 if(a[k*2+1].lm
a[k*2].r) 59 ins(d,n,k*2+1); 60 pushup(k); 61 return 0; 62 } 63 int sea(int n,int k) 64 { 65 if(a[k].lazy==1||a[k].r==a[k].l) 66 { 67 return a[k].m; 68 } 69 if(n<=a[k*2].r) 70 { 71 if(n>=a[k*2].r-a[k*2].rm+1) 72 return sea(n,k*2)+sea(a[k*2+1].l,k*2+1); 73 else 74 return sea(n,k*2); 75 } 76 else 77 { 78 if(n<=a[k*2+1].lm+a[k*2+1].l-1) 79 return sea(n,k*2+1)+sea(a[k*2].r,k*2); 80 else 81 return sea(n,k*2+1); 82 } 83 return 0; 84 } 85 int main() 86 { 87 int n,m,k,b; 88 while(~scanf("%d%d",&n,&m)) 89 { 90 build(1,n,1); 91 k=0; 92 while(m--) 93 { 94 char s[5]; 95 scanf("%s",s); 96 if(s[0]=='D') 97 { 98 scanf("%d",&num[k]); 99 ins(num[k],0,1);100 k++;101 }102 else if(s[0]=='Q')103 {104 scanf("%d",&b);105 printf("%d\n",sea(b,1));106 }107 else108 {109 k--;110 ins(num[k],1,1);111 }112 }113 }114 115 return 0;116 }
View Code

 

转载于:https://www.cnblogs.com/wang-ya-wei/p/6055672.html

你可能感兴趣的文章
Django 和 html
查看>>
算法与数据结构(一)
查看>>
【java】对象变成垃圾被垃圾回收器gc收回前执行的操作:Object类的protected void finalize() throws Throwable...
查看>>
数据库建表练习(10.11作业)
查看>>
如何配置能让fiddler抓去https的请求?
查看>>
SpringBoot 2.0 更优雅的配置注入
查看>>
[慢查优化]联表查询注意谁是驱动表 & 你搞不清楚谁join谁更好时请放手让mysql自行判定...
查看>>
liunx之Centos6.8杀毒软件的安装
查看>>
充实的日子里忙忙碌碌
查看>>
十三、oracle 数据字典和动态性能视图
查看>>
插件开发-UI插件开发
查看>>
[转] vim自定义配置 和 在ubnetu中安装vim
查看>>
Windows环境下安装、卸载Apache
查看>>
HTTPS协议在Tomcat中启用的配置
查看>>
Collections.sort的使用
查看>>
圆形坠落模拟算法设计
查看>>
vi @-function
查看>>
2018年各大互联网前端面试题五(今日头条)
查看>>
Vue.js开发环境搭建的介绍
查看>>
python之路-SQLAlchemy
查看>>