hdu1540 && POJ2892 Tunnel Warfare
2015-01-11 12:40
519 查看
【比赛提醒】BestCoder 你报名了吗?(点击报名)
【科普】什么是BestCoder?如何参加?
Total Submission(s): 4513 Accepted Submission(s): 1725
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
Sample Output
Source
POJ Monthly
Recommend
LL | We have carefully selected several similar problems for you: 1542 1255 1698 1828 2871
这道题也是典型的线段树区间合并的题, 主要是要记录一段区间左边连续的长度, 右边连续长度, 单点更新,查询时只要判断要查的点是不是落在中间连续处,否则就继续递归查询
【科普】什么是BestCoder?如何参加?
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4513 Accepted Submission(s): 1725
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
Source
POJ Monthly
Recommend
LL | We have carefully selected several similar problems for you: 1542 1255 1698 1828 2871
这道题也是典型的线段树区间合并的题, 主要是要记录一段区间左边连续的长度, 右边连续长度, 单点更新,查询时只要判断要查的点是不是落在中间连续处,否则就继续递归查询
/************************************************************************* > File Name: hdu1540.cpp > Author: ALex > Mail: 405045132@qq.com > Created Time: 2015年01月11日 星期日 12时24分34秒 ************************************************************************/ #include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 500010; stack <int> re; struct node { int l, r; int l_sum; int r_sum; }tree[N << 2]; void pushup (int p) { tree[p].l_sum = tree[p << 1].l_sum; tree[p].r_sum = tree[p << 1 | 1].r_sum; if (tree[p << 1].l_sum == tree[p << 1].r - tree[p << 1].l + 1) { tree[p].l_sum += tree[p << 1 | 1].l_sum; } if (tree[p << 1 | 1].r_sum == tree[p << 1 | 1].r - tree[p << 1 | 1].l + 1) { tree[p].r_sum += tree[p << 1].r_sum; } } void build (int p, int l, int r) { tree[p].l = l; tree[p].r = r; tree[p].l_sum = tree[p].r_sum = (r - l + 1); if (l == r) { return; } int mid = (l + r) >> 1; build (p << 1, l, mid); build (p << 1 | 1, mid + 1, r); } void update (int p, int pos, int sta) { if (tree[p].l == tree[p].r) { tree[p].l_sum = tree[p].r_sum = sta; return; } int mid = (tree[p].l + tree[p].r) >> 1; if (pos <= mid) { update (p << 1, pos, sta); } else { update (p << 1 | 1, pos, sta); } pushup (p); } int query (int p, int pos) { if (tree[p].l == tree[p].r) { if (tree[p].l_sum == 1) { return 1; } return 0; } int mid = (tree[p].l + tree[p].r) >> 1; if (pos <= mid) { if (pos >= mid - tree[p << 1].r_sum + 1) { return tree[p << 1].r_sum + tree[p << 1 | 1].l_sum; } return query (p << 1, pos); } else { if (pos <= mid + tree[p << 1 | 1].l_sum) { return tree[p << 1 | 1].l_sum + tree[p << 1].r_sum; } return query (p << 1 | 1, pos); } } int main() { int n, m; int x; char op[10]; while (~scanf("%d%d", &n, &m)) { build (1, 1, n); while (!re.empty()) { re.pop(); } while (m--) { scanf("%s", op); if(op[0] == 'R') { if (re.empty()) { continue; } update (1, re.top(), 1); re.pop(); } else { scanf("%d", &x); if (op[0] == 'D') { update (1, x, 0); re.push(x); } else { printf("%d\n", query (1, x)); } } } } return 0; }
相关文章推荐
- HDU 1540 && POJ 2892 Tunnel Warfare (线段树,区间合并).
- poj 2892 &&hdu 1540 Tunnel Warfare
- hdu 1540 & poj 2892Tunnel Warfare(线段树区间合并)
- Hdu 1540 && Poj 2892 Tunnel Warfare
- hdu 1540 & poj 2892 Tunnel Warfare 线段树区间合并
- hdu 1540 && poj 2892
- POJ 2892 Tunnel Warfare || HDU 1540(树状数组+二分 || 线段树的单点更新+区间查询)
- poj 2892 hdu 1540Tunnel Warfare(poj 线段树 区间合并)
- poj 2892 || hdu 1540 Tunnel Warfare
- HDU 1540 POJ 2892 Tunnel Warfare
- HDU 1540 && POJ 2892 线段树 单点染色 区间查询
- HDU 1540 POJ 2892 Tunnel Warfare
- HDOJ 1540 && POJ 2892 —— 线段树
- POJ 2892 Tunnel Warfare && HDOJ 1540 (线段树)
- HDU 1540 POJ 2892 线段树区间合并
- 【维护区间最长连续子序列 && 线段树 && 区间归并】HDU - 1540 Tunnel Warfare
- poj 1001 && hdu 1063 (java版本)
- HDU_1455 && POJ_1011 Sticks (dfs)
- 线段树专辑——hdu 1540 Tunnel Warfare
- poj 1503 && hdu 1047 大数相加