poj 2892---Tunnel Warfare(线段树单点更新、…
2018-06-17 23:04:39来源:未知 阅读 ()
题目链接
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 9 D 3 D 6 D 5 Q 4 Q 5 R Q 4 R Q 4
Sample Output
1 0 2 4
Hint
An illustration of the sample input:
OOOOOOO
D 3 OOXOOOO
D 6 OOXOOXO
D 5 OOXOXXO
R OOXOOXO
R OOXOOOO
Source
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <stack> using namespace std; const int maxn=50005; stack<int> s; struct Node{ int l,r,m; }tr[4*maxn]; void build(int i,int l,int r) { tr[i].l=tr[i].r=tr[i].m=r-l+1; if(l==r) return; int mid=(l+r)/2; build(2*i,l,mid); build(2*i+1,mid+1,r); } void update(int i,int l,int r,int x,int y) { if(l==r) { tr[i].l=tr[i].r=tr[i].m=y; return; } int mid=(l+r)/2; if(x<=mid) update(2*i,l,mid,x,y); else update(2*i+1,mid+1,r,x,y); if(tr[2*i].m==mid-l+1) tr[i].l=tr[2*i].m+tr[2*i+1].l; else tr[i].l=tr[2*i].l; if(tr[2*i+1].m==r-mid) tr[i].r=tr[2*i+1].m+tr[2*i].r; else tr[i].r=tr[2*i+1].r; tr[i].m=max(max(tr[2*i].m,tr[2*i+1].m),tr[2*i].r+tr[2*i+1].l); } int query(int i,int l,int r,int x) { int sum=0; if(l==r) return tr[i].m; if(r-l+1==tr[i].m) return tr[i].m; int mid=(l+r)/2; if(x<=mid){ if(mid-tr[2*i].r+1<=x) return tr[2*i].r+tr[2*i+1].l; else return query(2*i,l,mid,x); } else { if(tr[2*i+1].l+mid>=x) return tr[2*i].r+tr[2*i+1].l; else return query(2*i+1,mid+1,r,x); } } int main() { int n,m; while(scanf("%d",&n)!=EOF) { scanf("%d",&m); build(1,1,n); int x; char str[5]; while(!s.empty()) s.pop(); while(m--) { scanf("%s",str); if(str[0]=='D') { scanf("%d",&x); s.push(x); update(1,1,n,x,0); } else if(str[0]=='Q') { scanf("%d",&x); printf("%d\n",query(1,1,n,x)); } else { update(1,1,n,s.top(),1); s.pop(); } } } return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:42:出书最多
- POJ-3278 2020-04-01
- Asteroids!_poj2225 2020-02-09
- poj-1753题题解思路 2020-01-26
- POJ1852 2019-11-11
- POJ2431 优先队列+贪心 - biaobiao88 2019-11-03
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash