HDU-1540 Tunnel Warfare (线段树 维护端点信息)
来源:互联网 发布:淘宝上正规药店是哪个 编辑:程序博客网 时间:2024/06/04 19:47
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10405 Accepted Submission(s): 4086
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
#include <bits/stdc++.h>using namespace std;int h[50001];struct tree{int l, r, mx;}c[50000 << 2];void pushup(int o, int l, int r){c[o].mx = max(c[o << 1].mx, c[o << 1 | 1].mx);c[o].mx = max(c[o].mx, c[o << 1].r + c[o << 1 | 1].l);int mid = l + r >> 1;if(c[o << 1].l == mid - l + 1){c[o].l = c[o << 1].mx + c[o << 1 | 1].l;}else{c[o].l = c[o << 1].l;}if(c[o << 1 | 1].r == r - mid){c[o].r = c[o << 1 | 1].mx + c[o << 1].r;}else{c[o].r = c[o << 1 | 1].r;}}void build(int o, int l, int r){c[o].l = c[o].r = c[o].mx = r - l + 1;if(l == r) return;int mid = l + r >> 1;build(o << 1, l, mid);build(o << 1 | 1, mid + 1, r);}void add(int o, int l, int r, int id, int v){if(l == r){c[o].r = c[o].l = c[o].mx = v;return;}int mid = l + r >> 1;if(id <= mid) add(o << 1, l, mid, id, v);else add(o << 1 | 1, mid + 1, r, id, v);pushup(o, l, r);}int query(int o, int l, int r, int id){if(l == r) return c[o].mx;if(c[o].mx == 0) return 0;if(c[o].l == r - l + 1) return c[o].l;int mid = l + r >> 1;if(id <= mid){if(id >= mid - c[o << 1].r + 1){return query(o << 1, l, mid, id) + query(o << 1 | 1, mid + 1, r, mid + 1);}else{return query(o << 1, l, mid, id);}}else{if(id <= mid + c[o << 1 | 1].l){return query(o << 1 | 1, mid + 1, r, id) + query(o << 1, l, mid, mid);}else{return query(o << 1 | 1, mid + 1, r, id);}}}int main(){int n, m;while(scanf("%d %d", &n, &m) != EOF){int pos = 0, x;build(1, 1, n);char s[10];for(int i = 1; i <= m; ++i){scanf("%s", s);if(s[0] == 'D'){scanf("%d", &x);h[++pos] = x;add(1, 1, n, x, 0);}if(s[0] == 'R'){add(1, 1, n, h[pos--], 1);}if(s[0] == 'Q'){scanf("%d", &x);printf("%d\n", query(1, 1, n, x));}}}}/*题意:5e4的点,每个点要么为1要么为0,5e4次操作,每次某点要么0变1,要么1变0,要么查询某点所在的最长连续1的长度。思路:线段树维护端点在区间内的最长连续1的长度,查询时,即当某点所在线段树的区间是满的时候,注意扩散一下。*/
阅读全文
0 0
- 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 [二分 + 线段树]
- HDU 1540 Tunnel Warfare (线段树)
- hdu 1540 Tunnel Warfare 线段树 / set
- hdu Tunnel Warfare(线段树)
- hdu 1540 Tunnel Warfare (线段树维护左右最长连续区间)
- hdu1540 poj2892 Tunnel Warfare 线段树端点更新 SBT
- HDU 1540Tunnel Warfare Tunnel Warfare (线段树- 区间合并-单点更新查询)
- HDU 1540 Tunnel Warfare 【线段树--最大连续区间】
- hdu 1540 Tunnel Warfare(线段树 连续区间)
- 使用交叉存取得到更快推荐算法
- git github 写的比较好的博文
- 数据结构与算法(Python)——常见数据结构Part2(Common data structures)
- EOJ Monthly 2017.12 A+B+C
- 线程编程
- HDU-1540 Tunnel Warfare (线段树 维护端点信息)
- IO
- 二分查找---膨胀的木棍
- JS实现带有logo的二维码
- 使用 CodeMirror 打造属于自己的在线代码编辑器
- python3 内置核心数据类型
- django项目开发模拟auth的做一个登录验证(一)
- SpringBoot事务的使用
- 投影纹理与最小细节层级的混合应用