hdu 1540 线段树区间合并
来源:互联网 发布:盗墓笔记坐骑进阶数据 编辑:程序博客网 时间:2024/06/13 19:42
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)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 <iostream>#define maxn 50010#include <stdio.h>using namespace std;struct Tree{ int ls,rs,ms,l,r;}tree[maxn*4];int n,m;int s[maxn],top;void build(int root,int l,int r){ tree[root].l=l; tree[root].r=r; tree[root].ls=tree[root].rs=tree[root].ms=r-l+1; if(l!=r) { int mid=(tree[root].l+tree[root].r)>>1; build(root<<1,l,mid); build(root<<1|1,mid+1,r); }}void insert(int root,int t,int x){ if(tree[root].l==tree[root].r) { if(x==1) tree[root].ls=tree[root].rs=tree[root].ms=1; else tree[root].ls=tree[root].rs=tree[root].ms=0; return; } int mid=(tree[root].l+tree[root].r)>>1; if(t<=mid) insert(root<<1,t,x); else insert(root<<1|1,t,x); tree[root].ls=tree[root<<1].ls; tree[root].rs=tree[root<<1|1].rs; tree[root].ms=max(max(tree[root<<1].ms,tree[root<<1|1].ms),tree[root<<1].rs+tree[root<<1|1].ls); if(tree[root<<1].ls==tree[root<<1].r-tree[root<<1].l+1) tree[root].ls+=tree[root<<1|1].ls; if(tree[root<<1|1].rs==tree[root<<1|1].r-tree[root<<1|1].l+1) tree[root].rs+=tree[root<<1].rs;}int query(int root,int t){ if(tree[root].l==tree[root].r||tree[root].ms==0||tree[root].ms==tree[root].r-tree[root].l+1) return tree[root].ms; int mid=(tree[root].l+tree[root].r)>>1; if(t<=mid) { if(t>=tree[root<<1].r-tree[root<<1].rs+1) return query(root<<1,t)+query(root<<1|1,mid+1); else return query(root<<1,t); } else { if(t<=tree[root<<1|1].l+tree[root<<1|1].ls-1) return query(root<<1|1,t)+query(root<<1,mid); else return query(root<<1|1,t); }}int main(){ char c[2]; int x; while(scanf("%d%d",&n,&m)!=EOF) { top=0; build(1,1,n); while(m--) { scanf("%s",c); if(c[0]=='D') { scanf("%d",&x); s[top++]=x; insert(1,x,0); } else if(c[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 1540 线段树+区间合并
- hdu 1540 线段树区间合并
- 线段树--区间合并--HDU 1540
- HDU 1540 线段树区间合并
- hdu 1540 线段树(区间合并入门)
- 线段树--区间合并hdu--1540
- HDU-1540 Tunnel Warfare 线段树 区间合并
- hdu 1540 Tunnel Warfare(线段树区间合并)
- hdu 1540 & poj 2892 Tunnel Warfare 线段树区间合并
- HDU 1540 POJ 2892 线段树区间合并
- 【Hdu】1540 Tunnel Warfare(线段树|区间合并)
- HDU 1540 Tunnel Warfare(线段树 区间合并 +单点更新)
- hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】
- HDU 1540 Tunnel Warfare(线段树区间合并)
- HDU 1540 Tunnel Warfare (线段树区间合并)
- HDU - 1540 Tunnel Warfare(线段树 区间合并)
- hdu 1540 Tunnel Warfare (线段树区间合并)
- jQuery源码分析之offset,position,offsetParent方法以及源码中常见的cssHooks,swap代码
- 模拟器系统键盘 不能弹出
- C++中的iter_swap函数的应用
- Android夜间模式的实现
- self = [super init]理解
- hdu 1540 线段树区间合并
- CocoaPods安装和使用
- R语言 分层抽样 strata (三),每层抽取80%
- malloc、calloc、realloc与free简要
- C++ 排序法之冒泡法和选择法 素数,递归调用,用星号(*)打印一个梯形
- XCode主题更改
- 再见2015
- WWDC2015 - Interface Builder技巧(IB 技巧)
- 在SQLite中使用事务