hdu Tunnel Warfare(线段树)
来源:互联网 发布:mysql 新建数据库 编辑:程序博客网 时间:2024/05/18 00:24
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3789 Accepted Submission(s): 1427
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线段树记录每个点的前缀和,以及后缀和包含其本身!这题有几个地方比较坑爹:1、在修复点的时候记得先判断一下点是否已经修复,如果已经修复过了就不用再修复了;2、在摧毁一个点的时候,也要先判断一下这个点是否已经被摧毁,若已摧毁就不用再摧毁;3、如果一个点被连续摧毁,比如:D 2D 1D 1D 11被摧毁3次,那么接着:RRR这3个R都是在修复1!!4、用cin 还超时!我用了scanf!!#include <iostream>#include <cstdio>#include <stack>using namespace std;const int maxn = 50010;struct tree{int l , r , sl , sr;tree(int a = 0 , int b = 0 , int c = 0 , int d = 0){l = a , r = b , sl = c , sr = d;}}a[4*maxn];int n , m , SL , SR , sta[maxn];stack<int> D;void pushdown(int k){if(a[k].l < a[k].r){a[2*k].sl += a[k].sl;a[2*k].sr += a[k].sr;a[2*k+1].sl += a[k].sl;a[2*k+1].sr += a[k].sr;a[k].sl = 0;a[k].sr = 0;}}void build(int l , int r , int k){a[k].l = l;a[k].r = r;a[k].sl = 0;a[k].sr = 0;if(l == r){a[k].sl = l;a[k].sr = n-l+1;return;}int mid = (l+r)/2;build(l , mid , 2*k);build(mid+1 , r , 2*k+1);}void add(int l , int r , int k , int sl , int sr){if(l <= a[k].l && a[k].r <= r){a[k].sl += sl;a[k].sr += sr;}else{int mid = (a[k].l+a[k].r)/2;if(mid >= r){add(l , r , 2*k , sl , sr);}else if(mid < l){add(l , r , 2*k+1 , sl , sr);}else{add(l , mid , 2*k , sl , sr);add(mid+1 , r , 2*k+1, sl , sr);}}}void querry(int l , int r , int k){if(l <= a[k].l && a[k].r <= r){SL = a[k].sl;SR = a[k].sr;}else{pushdown(k);int mid = (a[k].l+a[k].r)/2;if(mid >= r){querry(l , r , 2*k);}else if(mid < l){querry(l , r , 2*k+1);}else{querry(l , mid , 2*k);querry(mid , r , 2*k+1);}}}void initial(){build(0 , n+1 , 1);SL = 0;SR = 0;while(!D.empty()) D.pop();for(int i = 0; i < maxn; i++){sta[i] = 1;}sta[0] = 0;}void computing(){char op;sta[n+1] = 0;while(m--){getchar();/*for(int i = 1; i <= n; i++){querry(i , i , 1);cout << SL+SR-1 << " ";}cout <<endl;*///cin >> op;scanf("%c" , &op);if(op == 'D'){int d;//cin >> d;scanf("%d" , &d);D.push(d);if(sta[d] == 1){//D.push(d);querry(d , d , 1);add(d-SL+1 , d , 1 , 0 , -1*SR);add(d , d+SR-1 , 1 , -1*SL , 0);sta[d] = 0;}}if(op == 'R'){if(!D.empty()){int R = D.top();D.pop();if(sta[R] == 0){sta[R] = 1;int l1 , l2 , r1 , r2;querry(R-1 , R-1 , 1);l1 = SL;r1 = SR;querry(R+1 , R+1 , 1);l2 = SL;r2 = SR;add(R-l1 , R , 1 , 0 , r2+1);add(R , R+r2 , 1 , l1+1 , 0);}}}if(op == 'Q'){int q;//cin >> q;scanf("%d" , &q);if(sta[q] == 0) cout << 0 <<endl;else{querry(q , q , 1);cout << SL+SR-1 <<endl;}}}}int main(){while(scanf("%d%d" , &n , &m) != EOF){initial();computing();}return 0;}
0 0
- hdu 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 线段树 / set
- hdu-154-Tunnel Warfare(线段树,区间)
- HDU 1540Tunnel Warfare Tunnel Warfare (线段树- 区间合并-单点更新查询)
- HDU_1540 Tunnel Warfare 线段树
- poj2892--Tunnel Warfare(线段树)
- Tunnel Warfare(线段树)
- HDU 1540 Tunnel Warfare 【线段树--最大连续区间】
- hdu 1540 Tunnel Warfare(线段树 连续区间)
- Sqlite3,C++开发详解
- 数据结构学习笔记-线索二叉树
- mahout将数据转化成序列化文件、稀疏向量
- ORACLE分页查询SQL语法——最高效的分页
- 跨行清算系统的实现过程
- hdu Tunnel Warfare(线段树)
- 就是找一个温暖的人过一辈子
- Android图形合成和显示系统---基于高通MSM8k MDP4平台
- Linux /dev目录详解
- 秋衣春生
- Android原理揭秘系列之View、ViewGroup
- 黑马程序员学习笔记——如果用Eclipse查看java源码
- pig—WordCount analysis
- 2014年4月操作系统份额:Windows XP仍然在26%以上,Linux微涨