hdu 1540 神奇的线段树
来源:互联网 发布:免费手机记账软件 编辑:程序博客网 时间:2024/06/07 05:45
Problem 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!
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.
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
题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少
思路:代码中的注释已经比较详细了,所以不多解释什么,在线段树的区间内,我们要用三个变量记录左边连续区间,右边连续区间和最大连续区间
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- #include <math.h>
- #include <stdlib.h>
- using namespace std;
- const int maxn = 50000+10;
- int n,m;
- int s[maxn],top;//s为模拟栈
- struct node
- {
- int l,r;
- int ls,rs,ms;//ls,左端最大连续区间,rs右端最大连续区间,ms区间内最大连续区间
- } a[maxn<<2];
- void init(int l,int r,int i)
- {
- a[i].l = l;
- a[i].r = r;
- a[i].ls = a[i].rs = a[i].ms = r-l+1;
- if(l!=r)
- {
- int mid = (l+r)>>1;
- init(l,mid,i*2);
- init(mid+1,r,2*i+1);
- }
- }
- void insert(int i,int t,int x)
- {
- if(a[i].l == a[i].r)
- {
- if(x==1)
- a[i].ls = a[i].rs = a[i].ms = 1;//修复
- else
- a[i].ls = a[i].rs = a[i].ms = 0;//破坏
- return ;
- }
- int mid = (a[i].l+a[i].r)>>1;
- if(t<=mid)
- insert(2*i,t,x);
- else
- insert(2*i+1,t,x);
- a[i].ls = a[2*i].ls;//左区间
- a[i].rs = a[2*i+1].rs;//右区间
- a[i].ms = max(max(a[2*i].ms,a[2*i+1].ms),a[2*i].rs+a[2*i+1].ls);//父亲区间内的最大区间必定是,左子树最大区间,右子树最大区间,左右子树合并的中间区间,三者中最大的区间值
- if(a[2*i].ls == a[2*i].r-a[2*i].l+1)//左子树区间满了的话,父亲左区间要加上右孩子的左区间
- a[i].ls += a[2*i+1].ls;
- if(a[2*i+1].rs == a[2*i+1].r-a[2*i+1].l+1)//同理
- a[i].rs += a[2*i].rs;
- }
- int query(int i,int t)
- {
- if(a[i].l == a[i].r || a[i].ms == 0 || a[i].ms == a[i].r-a[i].l+1)//到了叶子节点或者该访问区间为空或者已满都不必要往下走了
- return a[i].ms;
- int mid = (a[i].l+a[i].r)>>1;
- if(t<=mid)
- {
- if(t>=a[2*i].r-a[2*i].rs+1)//因为t<=mid,看左子树,a[2*i].r-a[2*i].rs+1代表左子树右边连续区间的左边界值,如果t在左子树的右区间内,则要看右子树的左区间有多长并返回
- return query(2*i,t)+query(2*i+1,mid+1);
- else
- return query(2*i,t);//如果不在左子树的右边界区间内,则只需要看左子树
- }
- else
- {
- if(t<=a[2*i+1].l+a[2*i+1].ls-1)//同理
- return query(2*i+1,t)+query(2*i,mid);
- else
- return query(2*i+1,t);
- }
- }
- int main()
- {
- int i,j,x;
- char ch[2];
- while(~scanf("%d%d",&n,&m))
- {
- top = 0;
- init(1,n,1);
- while(m--)
- {
- scanf("%s",ch);
- if(ch[0] == 'D')
- {
- scanf("%d",&x);
- s[top++] = x;
- insert(1,x,0);
- }
- else if(ch[0] == 'Q')
- {
- scanf("%d",&x);
- printf("%d\n",query(1,x));
- }
- else
- {
- if(x>0)
- {
- x = s[--top];
- insert(1,x,1);
- }
- }
- }
- }
- return 0;
- }
0 0
- hdu 1540 神奇的线段树
- 神奇的线段树
- HDU 5643 (神奇的打表 线段树)
- 【bzoj3165】[Heoi2013]Segment 神奇的线段树
- smoj2011(神奇的线段树)
- HDU 神奇的花瓣
- bzoj3244 NOI2013树的计数 神奇脑洞题+线段树
- CF 452F Permution 神奇的线段树判断
- 【JZOJ5270】【GDOI2018模拟】神奇的矩阵(二维线段树)
- hdu 1540 线段树
- hdu 1540 线段树
- hdu简单的线段树
- hdu 2256 神奇的矩阵
- Bzoj3790:神奇项链:manacher+线段树+贪心
- poj 2892 hdu 1540 不错的线段树题
- hdu 1540 Tunnel Warfare (线段树 记录的信息)
- JZOJ4417 【HNOI2016模拟4.1】神奇的字符串 线段树维护信息
- jzoj4417 【HNOI2016模拟4.1】神奇的字符串 (映射,权值线段树)
- Controller通过@RequestBody接收参数
- SD3.0协议解读四
- Xenomai thread migration
- Cocos2d3.0话说ValueMap的一般用法
- Android的一些小东西和工具
- hdu 1540 神奇的线段树
- 在导入asihttprequest包时出问题
- 字符串移位
- JAVA泛型
- 山寨智能机root后误删启动器解决办法?
- 使用Java在文件里插入一行
- 【HDU 3480】Division
- Maven+Hibernate4.3.5+c3p0配置
- vpn_centos6.sh 下载