Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3318    Accepted Submission(s): 1280

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

分析:设置lsum[n],rsum[n]分别表示区间左端连续村庄个数,区间右端连续村庄个数

查询的时候只要查询1~a右端连续个数+a~n左端连续个数,如果大于0就输出个数-1(因为a算了两次)

写完百度了下别人的代码,发现别人写的线段树和自己写的不怎么一样,于是就想学学,奈何。。。实在是不愿意看别人的代码,特别是那些没格式的还比较乱的。。。自己太懒了

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=50000+10;
int lsum[MAX<<2],rsum[MAX<<2];//分别记录区间内最大连续村庄,左端最大连续村庄,右端最大连续村庄
int s[MAX],size,n,m,a;//s记录破坏的村庄编号
char ch[2];
bool mark[MAX];//mark标记村庄是否破坏,0表示没破坏,1表示破坏 void Upfather(int n,int m){//更新区间左右端连续村庄个数
lsum[n]=lsum[n<<1];
rsum[n]=rsum[n<<1|1];
if(lsum[n] == m-(m>>1))lsum[n]+=lsum[n<<1|1];
if(rsum[n] == m>>1)rsum[n]+=rsum[n<<1];
} void BuildTree(int left,int right,int n){
lsum[n]=rsum[n]=right-left+1;
if(left == right)return;
int mid=left+right>>1;
BuildTree(left,mid,n<<1);
BuildTree(mid+1,right,n<<1|1);
} void Update(int p,int date,int left,int right,int n){
if(left == right){lsum[n]=(rsum[n]+=date);return;}
int mid=left+right>>1;
if(p<=mid)Update(p,date,left,mid,n<<1);
else Update(p,date,mid+1,right,n<<1|1);
Upfather(n,right-left+1);
} int QueryL(int L,int R,int left,int right,int n){//查询a~n的左端连续村庄个数
if(L<=left && right<=R)return lsum[n];
int mid=left+right>>1,lans,rans;
if(L<=mid)lans=QueryL(L,R,left,mid,n<<1);
if(R>mid)rans=QueryL(L,R,mid+1,right,n<<1|1);
if(R<=mid)return lans;
if(L>mid)return rans;
if(lans == mid-L+1)return lans+rans;
return lans;
}
/*另一种写法或许更好理解
int QueryL(int L,int R,int left,int right,int n){
if(L<=left && right<=R)return lsum[n];
int mid=left+right>>1,lans,rans;
if(R<=mid)return QueryL(L,R,left,mid,n<<1);
else if(L>mid)return QueryL(L,R,mid+1,right,n<<1|1);
else{
lans=QueryL(L,mid,left,mid,n<<1);
rans=QueryL(mid+1,R,mid+1,right,n<<1|1);
if(lans == mid-L+1)return lans+rans;
return lans;
}
}
*/ int QueryR(int L,int R,int left,int right,int n){//查询1~a右端村庄连续个数
if(L<=left && right<=R)return rsum[n];
int mid=left+right>>1,lans,rans;
if(L<=mid)lans=QueryR(L,R,left,mid,n<<1);
if(R>mid)rans=QueryR(L,R,mid+1,right,n<<1|1);
if(R<=mid)return lans;
if(L>mid)return rans;
if(rans == R-mid)return lans+rans;
return rans;
}
/*另一种写法或许更好理解
int QueryR(int L,int R,int left,int right,int n){
if(L<=left && right<=R)return rsum[n];
int mid=left+right>>1,lans,rans;
if(R<=mid)return QueryR(L,R,left,mid,n<<1);
else if(L>mid)return QueryR(L,R,mid+1,right,n<<1|1);
else{
lans=QueryR(L,mid,left,mid,n<<1);
rans=QueryR(mid+1,R,mid+1,right,n<<1|1);
if(rans == R-mid)return lans+rans;
return rans;
}
}
*/ int main(){
while(~scanf("%d%d",&n,&m)){
BuildTree(1,n,1);
size=0;
memset(mark,false,sizeof(bool)*(n+2));
mark[0]=true;
for(int i=0;i<m;++i){
scanf("%s",ch);
if(ch[0] == 'D'){
scanf("%d",&a);
s[++size]=a;
if(!mark[a])Update(a,-1,1,n,1),mark[a]=true;
}else if(ch[0] == 'R'){
while(!mark[s[size]])--size;//已经被修复过了就修复下一个,比如3 2 2,第一次修复2,现在修复3而不是2
if(size)Update(s[size],1,1,n,1),mark[s[size--]]=false;
}else{
scanf("%d",&a);
int temp=QueryL(a,n,1,n,1)+QueryR(1,a,1,n,1);
printf("%d\n",temp>0?temp-1:0);
}
}
}
return 0;
}

hdu1540之线段树单点更新+区间合并的更多相关文章

  1. POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 D ...

  2. HDU 3308 LCIS(线段树单点更新区间合并)

    LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...

  3. hdu 5316 Magician(2015多校第三场第1题)线段树单点更新+区间合并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:给你n个点,m个操作,每次操作有3个整数t,a,b,t表示操作类型,当t=1时讲a点的值改 ...

  4. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  5. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  6. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  7. hdu 1166线段树 单点更新 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. hdu1166(线段树单点更新&区间求和模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...

  9. hdu2795(线段树单点更新&区间最值)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要 ...

随机推荐

  1. BZOJ 1058 报表统计 (STL)

    题解:数据结构的基本操作,用STL可以完美实现,就是比较慢…… #include <cstdio> #include <map> #include <set> #i ...

  2. java学习之JDBC

    之前学习了数据库原理,上学期也学了oracle数据库,我的学习视频上是讲的mysql数据库,其实都差不多,复习了下sql知识,数据库的学习就没有写下来了,就从Java怎么操作数据库开始吧. 因为这年过 ...

  3. Flex TextInput的restrict属性应用

    1,<mx:TextInput id="test_ti" width="160" maxChars="20" restrict=&qu ...

  4. SQL实现递归及存储过程中 In() 参数传递解决方案

    1.SQL递归 在SQL Server中,我们可以利用表表达式来实现递归算法,一般用于阻止机构的加载及相关性处理. -->实现: 假设OrganiseUnit(组织机构表)中主要的三个字段为Or ...

  5. [置顶] js操作iframe兼容各种浏览器

    在做项目时,遇到了操作iframe的相关问题.业务很简单,其实就是在操作iframe内部某个窗体时,调用父窗体的一个函数.于是就写了两个很简单的htm页面用来测试,使用网上流行的方法在谷歌浏览器中始终 ...

  6. JDK和JRE的差异和区别

    来源:http://docs.oracle.com/javase/7/docs/

  7. Android各种效果集合

    QQ侧滑风格:http://www.cnblogs.com/lichenwei/p/4111252.html,通过继承HorizontalScrollView类来实现的.

  8. GIT简易使用流程

    git是目前世界上最先进的分布式版本控制系统(摘自廖雪峰官网) 首先需要在系统上安装git: Windows系统在这下载: RHEL/Centos/Fedora用户可以用以下命令安装:yum -y i ...

  9. python subprocess重定向标准输出

    subprocess.call("ping -c 1 %s" % ip,shell = True,stdout = open('/dev/null','w'),stderr = s ...

  10. open和fopen的区别:

    1.缓冲文件系统缓冲文件系统的特点是:在内存开辟一个“缓冲区”,为程序中的每一个文件使用,当执行读文件的操作时,从磁盘文件将数据先读入内存“缓冲区”, 装满后再从内存“缓冲区”依此读入接收的变量.执行 ...