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!

InputThe 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.

OutputOutput 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 表示初识线段树,对区间维护前缀后缀表示无力,不过最后还是卡过去了。注释应该比较清晰。。。
清楚线段树的结构,和区间前缀后缀 左孩子右孩子的拼接情况。。。说不清楚。。。。
 #include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define FO freopen("in.txt","r",stdin);
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define debug(x) cout << "&&" << x << "&&" << endl;
#define lowbit(x) (x&-x)
#define mem(a,b) memset(a, b, sizeof(a));
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=;
const int inf = 0x3f3f3f3f;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
//head const int maxn=;
int pre[maxn<<],suf[maxn<<],maxx[maxn<<],n,m,st[maxn];//维护区间前缀1,区间后缀1,st模拟栈 void pushup(int rt,int x) {
pre[rt]=pre[rt<<];//rt的前缀1就是左孩子前缀1
suf[rt]=suf[rt<<|];//rt后缀1就是右孩子后缀1
maxx[rt]=max(maxx[rt<<],maxx[rt<<|]);//rt的最大两种情况
maxx[rt]=max(maxx[rt],pre[rt<<|]+suf[rt<<]);
if(suf[rt<<|]==(x>>)) suf[rt]+=suf[rt<<];// 如果右孩子的后缀全是1,可以拼接左孩子的后缀
if(pre[rt<<]==(x-(x>>))) pre[rt]+=pre[rt<<|];//如果左孩子前缀全是1 可以拼接右孩子的前缀
} void build(int rt,int L,int R) {
pre[rt]=suf[rt]=maxx[rt]=R-L+;//这里置为R-L+1 和 常规用pushup效果一样
int mid=(L+R)>>;
if(L!=R) {
build(rt<<,L,mid);
build(rt<<|,mid+,R);
}
} void updata(int rt,int L,int R,int pos,int val) {
if(L==R) {//单点修改
pre[rt]=suf[rt]=maxx[rt]=val;
return;
}
int mid=(L+R)>>;
if(pos<=mid) updata(rt<<,L,mid,pos,val);
else updata(rt<<|,mid+,R,pos,val);
pushup(rt,R-L+);
} int query(int rt,int L,int R,int pos) {
if(L==R||maxx[rt]==||maxx[rt]==(R-L+))//断点 || maxx为0 || maxx最大
return maxx[rt];
int mid=(L+R)>>;
if(pos<=mid) {//如果在左子树
if(pos>=mid-suf[rt<<]+) return pre[rt<<|]+suf[rt<<];//如果大于左孩子的后缀1 == 包含两部分
else return query(rt<<,L,mid,pos); //否则搜左子树
} else {//如果在右子树
if(pos<=mid++pre[rt<<|]-) return pre[rt<<|]+suf[rt<<];//如果小于右孩子的前缀1 == 包含两部分
else return query(rt<<|,mid+,R,pos);//否则右子树
}
} int main() {
while(~scanf("%d%d",&n,&m)) {
int cur=;
build(,,n);
int pos;
while(m--) {
char s[];
scanf("%s",s);
if(s[]=='D') {
scanf("%d",&pos);
st[cur++]=pos;
updata(,,n,pos,);
} else if(s[]=='Q') {
scanf("%d",&pos);
printf("%d\n",query(,,n,pos));
} else {
pos=st[--cur];
updata(,,n,pos,);
}
}
}
}
												

kuangbin专题七 HDU1540 Tunnel Warfare (前缀后缀线段树)的更多相关文章

  1. kuangbin专题七 HDU1166 敌兵布阵 (线段树或树状数组)

    C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...

  2. I - Tunnel Warfare HDU - 1540 线段树最大连续区间

    题意  :一段区间  操作1 切断点 操作2 恢复最近切断的一个点 操作3 单点查询该点所在最大连续区间 思路:  主要是push_up :  设区间x 为母区间  x<<1 ,x< ...

  3. HDU1540 Tunnel Warfare —— 线段树 区间合并

    题目链接:https://vjudge.net/problem/HDU-1540 uring the War of Resistance Against Japan, tunnel warfare w ...

  4. hdu1540 Tunnel Warfare

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. HDU--1540 Tunnel Warfare(线段树区间更新)

    题目链接:1540 Tunnel Warfare 以为单组输入 这个题多组输入 结构体记录每个区间左边和右边的连续区间 ms记录最大 在查询操作时: 1.这个点即将查询到右区间 看这个点 x 是否存在 ...

  6. hdu1540 Tunnel Warfare 线段树/树状数组

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

  7. hdu1540 Tunnel Warfare【线段树】

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

  8. HDU1540 Tunnel Warfare(线段树区间维护&求最长连续区间)题解

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  9. HDU-1540 Tunnel Warfare(区间连续点长度)

    http://acm.hdu.edu.cn/showproblem.php?pid=1540 Time Limit: 4000/2000 MS (Java/Others)    Memory Limi ...

随机推荐

  1. 基于候选区域的深度学习目标检测算法R-CNN,Fast R-CNN,Faster R-CNN

    参考文献 [1]Rich feature hierarchies for accurate object detection and semantic segmentation [2]Fast R-C ...

  2. JavaScript组合设模式--改进上述引入的例子

    对于组合设计模式: (1)组合模式中把对象分为两种(组合对象,和叶子对象) (2)组合对象和叶子对象实现:同一批操作 (3)对组合对象执行的操作可以向下传递到叶子节点进行操作 (4)这样就会弱化类与类 ...

  3. XSS的各种用途

    0x01 最常见之窃取用户cookie 当cookie没有设置HttpOnly属性时,可以通过javascript代码创建img,script,iframe等标签,并把src属性设置为自己部署的xss ...

  4. 根据URL下载文件

    commons-io 包中已经封装好了,直接可以使用 一.添加依赖 <dependency> <groupId>org.apache.commons</groupId&g ...

  5. windows python-mysql 安装

    https://www.cnblogs.com/gbx-bo/p/5993190.html

  6. 使用Aspectj 的配置文件方式进行aop操作

  7. cfree使用cygwin编译程序出现计算机丢失cygwin1.dll解决办法

    这种情况多是环境没配好,我的是64位cygwin C:\cygwin64\bin 加入到环境变量中,重打开cfree就可以解决.

  8. 托盘在XP下不能显示tooltip,在Vista和Windows7下正常

    转自:http://blog.csdn.net/debehe/article/details/4294053 奇怪的问题,想了很多可能的理由,最终的答案竟然是一开始就被我否认了的一种可能!! 问题现象 ...

  9. Windows cmd 将命令(/指令)写到一个文件里,直接运行这个文件。提高工作效率

    Windows cmd 批处理(cmd/bat)文件的简单使用介绍 前言 如果你想我一样,要每天都需要在cmd上,用键盘去敲击相同的命令,时间一长,你就觉得很无聊.有没有什么比较高效的方法,让我们不用 ...

  10. Entity Framework Tutorial Basics(25):Delete Single Entity

    Delete Entity using DBContext in Disconnected Scenario: We used the Entry() method of DbContext to m ...