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

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 (nm ≤ 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:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. 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

Hint

An illustration of the sample input:

      OOOOOOO

D 3   OOXOOOO

D 6   OOXOOXO

D 5   OOXOXXO

R     OOXOOXO

R     OOXOOOO

题目链接:POJ 2892

线段树的区间合并操作相比其他区间操作稍微难理解,但是还是可以写的一下的,主要就是对pushup和query的修改

做法就是seg数据里记录当前区间从左边即seg::l 开始向右连续连通长度,记为L,从右边seg::r 开始的向左连续连通长度,记为R。用seg::M表示是区间中最大的连续连通长度,显然M应该有三种情况,要么为L要么为R要么为左子树的R+右子树的L,最后的情况是必然会要比较的,但是前两种不一定会出现,比如左子树的L并没有达到父节点的mid即左子树的r,因此中间断开了,父节点的L仍时保持为左子树的L,父节点的R也相同道理,然后三者取一个最大的就得到了父节点的M,然后是L和R如何更新就看左子树的L和右子树的R是否完全覆盖了子区间,若完全覆盖则父节点的L与R可以拓展延长……

还有就是查询的时候加一个剪枝条件不然容易TLE,若当前区间已经完全覆盖或者最大连通长度M为0显然是不需要再递归查询的直接返回即可。

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=50010;
struct seg
{
int l,mid,r;
int L,M,R;
int len;
};
seg T[N<<2];
int st[N],top; inline void pushup(int k)
{
T[k].L=T[LC(k)].L;
T[k].R=T[RC(k)].R; if(T[k].L==T[LC(k)].len)
T[k].L+=T[RC(k)].L;
if(T[k].R==T[RC(k)].len)
T[k].R+=T[LC(k)].R; T[k].M=max<int>(T[LC(k)].R+T[RC(k)].L,max<int>(T[LC(k)].M,T[RC(k)].M));
}
void build(int k,int l,int r)
{
T[k].l=l;
T[k].r=r;
T[k].mid=MID(l,r);
T[k].len=r-l+1;
T[k].L=T[k].R=T[k].M=r-l+1;
if(l==r)
return ;
build(LC(k),l,T[k].mid);
build(RC(k),T[k].mid+1,r);
}
void update(int k,int x,char flag)
{
if(T[k].l==T[k].r)
{
if(flag=='D')
T[k].L=T[k].R=T[k].M=0;
else
T[k].L=T[k].R=T[k].M=1;
}
else
{
if(x<=T[k].mid)
update(LC(k),x,flag);
else
update(RC(k),x,flag);
pushup(k);
}
}
int query(int k,int x)
{
if(T[k].M==T[k].len||!T[k].M||T[k].len==1)
return T[k].M;
else
{
if(x<=T[k].mid)
{
if(x>=T[k].mid+1-T[LC(k)].R)
return query(LC(k),x)+query(RC(k),T[k].mid+1);
else
return query(LC(k),x);
}
else
{
if(x<=T[k].mid+T[RC(k)].L)
return query(LC(k),T[k].mid)+query(RC(k),x);
else
return query(RC(k),x);
}
}
}
int main(void)
{
char ops[3];
int n,m,x;
while (~scanf("%d%d",&n,&m))
{
top=0;
build(1,1,n);
while (m--)
{
scanf("%s",ops);
if(ops[0]=='D')
{
scanf("%d",&x);
update(1,st[top++]=x,'D');
}
else if(ops[0]=='Q')
{
scanf("%d",&x);
printf("%d\n",query(1,x));
}
else
update(1,st[--top],'R');
}
}
return 0;
}

POJ 2892 Tunnel Warfare(线段树单点更新区间合并)的更多相关文章

  1. hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并

    Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  2. hdu1540之线段树单点更新+区间合并

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Diablo3

    1.装备 主手:元素弓 副手:精细箭袋 头: 胸:燃火外套 手:娜塔亚的手感 护腕:稳击护腕 戒指:罗盘玫瑰+布尔凯索的婚戒 颈部:旅者之誓 腰:科雷姆的强力腰带(速度加25%) 腿:深渊挖掘裤 脚: ...

  2. 个人博客转移到CSDN

    因个人博客编辑的需要,现将我的博客转移到CSDN,如果没有特别需要,以后会在CSDN上继续书写博客 关于Fresco的分析以及后续的相关分析,也会转移到CSDN 个人CSDN主页: http://bl ...

  3. Fresco 源码分析(三) Fresco服务端处理(1) ImagePipeline为何物

    4.3 服务端的处理 备注: 因为是分析,而不是设计,所以很多知识我们类似于插叙的方式叙述,就是用到了哪个知识点,我们再提及相关的知识点,如果分析到了最后,我想想是不是应该将这个架构按照设计的方式,重 ...

  4. 发现磁盘的shell

    #!/bin/bash #set -x diskarray=(`cat /proc/diskstats |grep -E "\bsd[a-z]\b|\bxvd[a-z]\b|\bvd[a-z ...

  5. 自定义漂亮的Android SeekBar样式

    系统自带的SeekBar真是太难看了,不能容忍! 只能自己做了,先来张效果图 第1个Seekbar 背景是颜色,thumb是图片,上代码: <SeekBar android:id="@ ...

  6. WPF/Silverlight HierarchicalDataTemplate 模版的使用(转)

    上一篇 对Wpf/Silverlight Template 进行了总结,本篇继续上一篇,主要是介绍 HierarchicalDataTemplate 的使用方法.HierarchicalDataTem ...

  7. Redis Set 命令

        1.Set set是string类型的无序集合,其参考来源应该属于STL中的Set.   •set元素最大可以包含(2的32次方-1)个元素. •set的是通过hash table实现的,ha ...

  8. BZOJ2862 : 分糖果

    二分答案$x$表示最大的一段的和. 设$f[i]$表示前$i$个最多分几段,满足最大的一段不超过$x$,若$f[n]\geq k$,则可行, 则$f[i]=\max(f[j])+1,sum[i]-su ...

  9. HDU 2364 (记忆化BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2364 题目大意:走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往 ...

  10. 【BZOJ】1089: [SCOI2003]严格n元树(递推+高精度/fft)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1089 题意:求深度为d的n元树数目.(0<n<=32, 0<=d<=16) ...