Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10051 Accepted Submission(s): 3935
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 1166 1255 1828 3016
//用的是poj3667 Hotel的模板,只需要改个query就可以了
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 55555;
int lsum[maxn << 2], rsum[maxn << 2], msum[maxn << 2];
int cover[maxn << 2];
int stack[maxn];
void PushDown(int rt, int m) {
if (cover[rt] != -1) {
cover[rt << 1] = cover[rt << 1 | 1] = cover[rt];
msum[rt << 1] = lsum[rt << 1] = rsum[rt << 1] = cover[rt] ? 0 : m - (m >> 1);
msum[rt << 1 | 1] = lsum[rt << 1 | 1] = rsum[rt << 1 | 1] = cover[rt] ? 0 : (m >> 1);
cover[rt] = -1;
}
}
void PushUp(int rt, int m) {
lsum[rt] = lsum[rt << 1];
rsum[rt] = rsum[rt << 1 | 1];
if (lsum[rt] == m - (m >> 1)) lsum[rt] += lsum[rt << 1 | 1];
if (rsum[rt] == (m >> 1)) rsum[rt] += rsum[rt << 1];
msum[rt] = max(lsum[rt << 1 | 1] + rsum[rt << 1], max(msum[rt << 1], msum[rt << 1 | 1]));
}
void build(int l, int r, int rt) {
msum[rt] = lsum[rt] = rsum[rt] = r - l + 1;
cover[rt] = -1;
if (l == r) return;
int m = (l + r) >> 1;
build(lson);
build(rson);
}
void update(int L, int R, int c, int l, int r, int rt) {
if (L <= l && r <= R) {
msum[rt] = lsum[rt] = rsum[rt] = c ? 0 : r - l + 1;
cover[rt] = c;
return;
}
PushDown(rt, r - l + 1);
int m = (l + r) >> 1;
if (L <= m) update(L, R, c, lson);
if (m < R) update(L, R, c, rson);
PushUp(rt, r - l + 1);
}
int query(int w, int l, int r, int rt) {
if (l == r || msum[rt] == r - l + 1 || msum[rt] == 0)return msum[rt];//到了叶子节点或者该访问区间为空或者已满都不必要往下走了
PushDown(rt, r - l + 1);
int m = (l + r) >> 1;
if (w <= m)
{//代表左子树右边连续区间的左边界值
if (w >= m - rsum[rt << 1] + 1)return query(w, lson) + query(m + 1, rson);//在左子树的右区间内,则要看右子树的左区间有多长并返回
return query(w, lson);
}
else
{//如果不在左子树的右边界区间内,则只需要看左子树
if (w <= m + lsum[rt << 1 | 1])return query(m, lson) + query(w, rson);
return query(w, rson);
}
}
int main() {
int n, m, top;
char op[10];
while (scanf("%d%d",&n,&m)!=EOF)
{
top = 0;
build(1, n, 1);
while (m--)
{
scanf("%s", op);
if (op[0] == 'D')
{
scanf("%d", &stack[top]);
update(stack[top], stack[top], 1, 1, n, 1);
top++;
}
else if (op[0] == 'R')
{
if (top > 0) {
top--;
update(stack[top], stack[top], 0, 1, n, 1);
}
}
else
{
int t;
scanf("%d", &t);
printf("%d\n", query(t, 1, n, 1));
}
}
}
return 0;
}