hdoj 1540 Tunnel Warfare 【线段树 区间合并】
2016-01-09 02:51
393 查看
Tunnel WarfareTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6106 Accepted Submission(s): 2361 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! 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 |
D x 表示破坏x村庄,Q x 表示查询x村庄所能联络的村庄数,R 表示重建最后一个被破坏的村庄。
思路:查询单点所在连续块的最大长度,以前做过区间的,点和区间也就查询不同。
点只有两种情况,区间内的连续长度 或者 左区间右连续+右区间左连续
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <string> #define INF 0x3f3f3f3f #define eps 1e-8 #define MAXN (50000+10) #define MAXM (500000) #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rf(a) scanf("%lf", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%.2lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define LL long long #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 #define PI acos(-1.0) using namespace std; struct Tree { int l, r, len; int lsum, rsum, sum; }; Tree tree[MAXN<<2]; void PushUp(int o) { tree[o].lsum = tree[ll].lsum; tree[o].rsum = tree[rr].rsum; tree[o].sum = max(tree[ll].sum, tree[rr].sum); tree[o].sum = max(tree[o].sum, tree[ll].rsum + tree[rr].lsum); if(tree[o].lsum == tree[ll].len) tree[o].lsum += tree[rr].lsum; if(tree[o].rsum == tree[rr].len) tree[o].rsum += tree[ll].rsum; } void Build(int o, int l, int r) { tree[o].l = l; tree[o].r = r; tree[o].len = tree[o].lsum = tree[o].rsum = tree[o].sum = r-l+1; if(l == r) return ; int mid = (l + r) >> 1; Build(lson); Build(rson); } void Update(int o, int pos, int val) { if(tree[o].l == tree[o].r) { tree[o].lsum = tree[o].rsum = tree[o].sum = val; return ; } int mid = (tree[o].l + tree[o].r) >> 1; if(pos <= mid) Update(ll, pos, val); else Update(rr, pos, val); PushUp(o); } int Query(int o, int pos) { if(tree[o].l == tree[o].r) return tree[o].sum; int mid = (tree[o].l + tree[o].r) >> 1; if(pos <= mid) { int ans1 = Query(ll, pos), ans2 = 0; if(pos >= tree[ll].r - tree[ll].rsum + 1) ans2 = tree[ll].rsum + tree[rr].lsum; return max(ans1, ans2); } else { int ans1 = Query(rr, pos), ans2 = 0; if(pos <= tree[rr].l + tree[rr].lsum - 1) ans2 = tree[ll].rsum + tree[rr].lsum; return max(ans1, ans2); } } int main() { int n, m; while(scanf("%d%d", &n, &m) != EOF) { Build(1, 1, n); stack<int> S; W(m) { char op[2]; int x; Rs(op); if(op[0] == 'D') { Ri(x); Update(1, x, 0); S.push(x); } else if(op[0] == 'Q') { Ri(x); Pi(Query(1, x)); } else { if(!S.empty()) { x = S.top(); S.pop(); Update(1, x, 1); } } } } return 0; }
相关文章推荐
- zoj 3777 Problem Arrangement 【状压dp】
- poj 1390 Blocks 【区间dp】
- nyoj 10 skiing 【记忆化】
- nyoj 737 石子合并(一) 【区间dp】
- mybatis动态sql
- vim 基础学习之插入模式
- mybatis常见问题
- LeetCode Jump Game II
- 数据库第三范式
- mybatis常用配置——resultMap
- GitHub-入门
- mysql---逻辑和数据库设计及权限
- GitHub-入门
- [R] R package "benchmark"
- java中static{}块
- Codeforces Round #338 (Div. 2)B. Longtail Hedgehog解题报告
- [俄勒冈大学]养成Python写作习惯
- vim 基础学习之普通模式
- 数据库第二范式
- 码农路,渐行渐远