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. 使用svn进行本地代码管理

    简述 这里讨论的是如何管理自己个人电脑的个人项目的代码.和SVN服务器配置无关. 可以先到TortoiseSVN官网下载安装包进行安装. 代码仓库管理 比如现在有一个工程需要进行管理,可以先将该工程放 ...

  2. rsync mac->windows openssh

    rsync -azvP --progress -e "ssh -p 6666" /Users/codar/360\344\272\221\347\233\230/ghld/ rsy ...

  3. How to Enabling and Diabling VxDMP devices for use with Oracle ASM

    Enable DMP support for ASM to make DMP devices visible to ASM as available disks To make DMP devices ...

  4. 问题:oracle 不等于;结果:Oracle中的不等于号

    Oracle中的不等于号 今天碰到一个Oracle不等于的问题,最后搜索了一下,发现下面资料,拿来跟大家分享一下   关于Oracle中的不等于号: 在Oracle中, <> != ~= ...

  5. 从文件中读取yuv和h264数据

    1.从文件中读取h264数据 参考ffmpeg avc.c写的从文件中一帧帧读取h.264数据的demo #include <stdio.h> #include <stdlib.h& ...

  6. eclipse DDMS导出文件失败--android Failed to push the item

    我们在写安卓程序的时候,经常会用Eclipse导出模拟器的文件管理里面的文件,但有时候会报错,导致无法导出文件. 报错信息 Failed to push selection: Local path d ...

  7. Vim 配置文件===/etc/vimrc

    1.替换方法 替换对应的vimrc文件,定制自己的vimrc /etc/vimrc              替换此文件: /home/lmy/.vimrc     只对当前用户有效: Ubuntu9 ...

  8. python去掉括号之间的字符

    在字符串中识别括号并删除括号及其中的内容括号包括 大中小 3种括号 输入为 1个字符串 s="我是一个人(中国人)[真的]{确定}"; 输出为 result = "我是一 ...

  9. webfrom 母版页

    ASP.NET中母版页作用 一是提高代码的复用(把相同的代码抽出来) 二是使整个网站保持一致的风格和样式. 母版页存在就一定要有内容页的存在,否则母版页的存在就没有了意义. .master 一.添加母 ...

  10. c++ 类中模版成员函数

    C++函数模版与类模版. template <class T> void SwapFunction(T &first, T &second){ }//函数模版 templa ...