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. 2016.2.28 DataTable用法汇总

    将控件的DataSource转换为DataTable,但是,此控件的DataSource绑定时必须是DataTable,不能是List DataTable dt = (bgvRoutePortion. ...

  2. 如何解决SSH登录Solaris主机速度慢的问题

    SSH登录速度慢可能有多种原因. 1. 与DNS有关 缺省情况下,当客户端用SSH登录solaris服务器时,服务器会试图反向解析客户端的IP 地址(即把IP地址解析成机器名).如果Solaris系统 ...

  3. LookupError: unknown encoding: cp65001解决办法

    一.之前手上做的一个web项目,漏洞频发,服务器用的是菜鸟云服务器,那个应急响应中心不错,想不到乌云倒了,白帽子竟然被阿里系养了,题外话了,首先感谢白帽子提的漏洞,同时也感慨自己安全知识,以及意识的薄 ...

  4. oracle --(二)分区(extent)

    基本关系:数据库---表空间---数据段---分区---数据块 一.分区(extent)分区extent是比数据块大一级的存储结构,是几个逻辑上相邻的data block的组合.我们知道,物理存储通常 ...

  5. sql 一些偶尔会用到的写法和函数 不定时更新

    小数转整数: --round() 遵循四舍五入把原值转化为指定小数位数,如: ) -- =1 ) -- =2 --floor() 向下舍入为指定小数位数 如: SELECT floor(1.45) - ...

  6. CoreData的增删改查

    首先使用CoreData创建Demo,勾上CoreData选项 然后创建Entity对象,点击Add Entity(+)按钮 生成Entity对象 重命名双击Entity选项,然后输入Person 设 ...

  7. 【转】手把手教你用Strace诊断问题

    原博客地址:http://huoding.com/2015/10/16/474 早些年,如果你知道有个 strace 命令,就很牛了,而现在大家基本都知道 strace 了,如果你遇到性能问题求助别人 ...

  8. ROS Learning-021 learning_tf-05(编程) now() 和 Time(0) 的区别 (Python版)

    ROS Indigo learning_tf-05 now() 和 Time(0)的区别 (Python版) - waitForTransform() 函数 我使用的虚拟机软件:VMware Work ...

  9. delete请求,删除不成功?

    因为,在数据库底层,其实并没有删除该数据,只是将数据的标识设置为is_deleted.因此,最后即使删除了,查询的时候还是会显示在界面. 故,需要重写get请求.

  10. html 里 checkbox里 只要选中就会自动添加checked=“checked”么?

    事实上HTML代码是不会发生变化的,但是控件对象的属性会发生变化以反映这个操作的结果.也就是说,该对象的checked属性值会由false变成true.但元素标签中并不会插入checked=" ...