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. CDM中遍历域及其约束条件、取值范围、引用它的项目

    Option   ExplicitValidationMode   =   TrueInteractiveMode =   im_BatchDim   mdl   '当前model'获取当前活动mod ...

  2. Json-lib 进行java与json字符串转换之二

    二.list和json字符串的互转 list-->>json字符串 public static void listToJSON(){ Student stu=new Student(); ...

  3. javascipt——对象的概念——数组

    一.Array 特点: 数组的长度是可变的: 数组的索引可以是数字.字符串: 数组的内容可以是任意内容: 可以通过索引获取之前不存在的一个位置,其值为undefined: 1.构造函数: new Ar ...

  4. CALayer的基本使用

    CALayer需要导入这个框架:#import <QuartzCore/QuartzCore.h> 一.CALayer常用属性 属性 说明 是否支持隐式动画 anchorPoint 和中心 ...

  5. 我积累的Java实用代码

    1.解压zip文件 /** * 解压输入的zip流,Java默认的解压只能处理UTF-8编码的文件或者目录名,否则会报MALFORMED异常 * * @param is 输入流 * @param ou ...

  6. day17 9.关闭资源与异常处理

    Java程序跟任何外部设备进行连接之后,都要把连接断开,把资源释放掉.Connection是一个重量级资源,Connecton占内存,Connection的获取是比较消耗资源和内存的.finally是 ...

  7. ROS探索总结(四)——简单的机器人仿真

    前边我们已经介绍了ROS的基本情况,以及新手入门ROS的初级教程,现在就要真正的使用ROS进入机器人世界了.接下来我们涉及到的很多例程都是<ROS by Example>这本书的内容,我是 ...

  8. SpringJdbc 【springjdbc的使用方法】

    1 什么是springjdbc spring对jdbc的封装 2 使用SpringJdbc的编程步骤 2.1 导包 spring-jdbc : springjdbc的包 mysql : MySQL的驱 ...

  9. java web前端发送请求的4种方式

    表单 action, 链接href,js 绑定按钮 ajax绑定标签 <h1>通过表单提交参数</h1> <form action="/day46_v1/Ser ...

  10. python pip ez_setup.py

    #!/usr/bin/env python """Bootstrap setuptools installation To use setuptools in your ...