hdu 1540 Tunnel Warfare
2012-07-22 17:00
288 查看
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2273 Accepted Submission(s): 837
[align=left]Problem Description[/align]
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!
[align=left]Input[/align]
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.
[align=left]Output[/align]
Output the answer to each of the Army commanders’ request in order on a separate line.
[align=left]Sample Input[/align]
7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
[align=left]Sample Output[/align]
1
0
2
4
[align=left]Source[/align]
POJ Monthly
[align=left]Recommend[/align]
LL
//求x所在最长的连续可用序列
//query函数写的我蛋疼,判断各种情况
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <stack> #define N 50000 #define lson l,m,k<<1 #define rson m+1,r,k<<1|1 using namespace std; struct node { int lm,rm,m; int len; }; node st[N<<2]; void up(int &k) { int ls=k<<1,rs=k<<1|1; st[k].lm=st[ls].lm==st[ls].len?st[ls].len+st[rs].lm:st[ls].lm; st[k].rm=st[rs].rm==st[rs].len?st[rs].len+st[ls].rm:st[rs].rm; st[k].m=max(max(st[ls].m,st[rs].m),st[ls].rm+st[rs].lm); //st[k].m=max(max(st[k].lm,st[k].rm),st[k].m); } void build(int l,int r,int k) { st[k].len=r-l+1; if(l==r) { st[k].lm=st[k].rm=st[k].m=1; return ; } int m=(l+r)>>1; build(lson); build(rson); up(k); } int flag; void update(int &index,int l,int r,int k) { if(l==r) { st[k].lm=st[k].rm=st[k].m=flag; return; } int m=(l+r)>>1; if(index<=m) update(index,lson); else update(index,rson); up(k); } int query(int &index,int l,int r,int k) { if(st[k].m==0) return 0; if(st[k].m==st[k].len) return st[k].len; int m=(l+r)>>1; if(st[k].lm&&st[k].lm+l-1>=index) return st[k].lm; if(st[k].rm&&r-st[k].rm+1<=index) return st[k].rm; // printf("%d %d %d\n",l,r,m-st[k<<1].rm+1); if(st[k<<1].rm&&index<=m&&m-st[k<<1].rm+1<=index) return st[k<<1].rm+st[k<<1|1].lm; if(st[k<<1|1].lm&&index>m&&m+st[k<<1|1].lm>=index) return st[k<<1].rm+st[k<<1|1].lm; if(index<=m) return query(index,lson); else return query(index,rson); } int main() { int n,m; char op; int data; while(scanf("%d%d",&n,&m)!=EOF) { stack<int> s; build(1,n,1); while(m--) { getchar(); scanf("%c",&op); switch(op) { case 'Q':scanf("%d",&data); printf("%d\n",query(data,1,n,1));break; case 'D':scanf("%d",&data);s.push(data); flag=0;update(data,1,n,1);break; case 'R':data=s.top();s.pop(); flag=1;update(data,1,n,1);break; } } } return 0; }
相关文章推荐
- hdu 1540 Tunnel Warfare(线段树区间合并)
- HDU 1540 Tunnel Warfare(线段树 区间合并 最大连续区间)
- HDU 1540 Tunnel Warfare
- 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(线段树区间合并)
- HDU 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare
- hdu 1540 Tunnel Warfare(线段树)(第三部分 区间合并)
- hdu 1540 Tunnel Warfare 线段树--区间合并
- hdu 1540 Tunnel Warfare【线段树】
- HDU1540 Tunnel Warfare(线段树区间合并 | 线段树新姿势)
- hdu 1540 Tunnel Warfare 线段树 区间合并
- HDU 1540 Tunnel Warfare(线段树区间合并)
- HDU 1540——Tunnel Warfare(线段树,区间合并+单点更新+单点查询)