Tunnel Warfare

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1540

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

HINT

题意

给你一个区间,有三个操作,使的一个村庄毁灭,使的上一个毁灭的村庄复活,查询这个村庄所在最长区间

题解:

啊,就是一个区间合并的简单线段树,单点修改,记录从左边的最大值,右边的最大值,区间的最大值

然后每次更新就好啦~

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 150001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff;   //无限大
const int inf=0x3f3f3f3f;
/* int buf[10];
inline void write(int i) {
  int p = 0;if(i == 0) p++;
  else while(i) {buf[p++] = i % 10;i /= 10;}
  for(int j = p-1; j >=0; j--) putchar('0' + buf[j]);
  printf("\n");
}
*/
//**************************************************************************************
inline ll read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
} struct node{
    int l,r;
    int len;
    int lm,rm,mm;
};
node a[maxn*4]; void build(int l,int r,int x){
    a[x].l=l,a[x].r=r;
    a[x].lm=a[x].rm=a[x].len=a[x].mm=r-l+1;
    if(l!=r){
        int mid=(l+r)>>1;
        build(l,mid,x<<1);
        build(mid+1,r,x<<1|1);
    }
}
void updata(int i,int t,int x)
{
    if(a[i].l == a[i].r){a[i].lm = a[i].rm = a[i].mm = x;return;}
    int mid = (a[i].l+a[i].r)>>1;
    if(t<=mid)updata(2*i,t,x);
    else updata(2*i+1,t,x);
    a[i].lm = a[2*i].lm;
    a[i].rm = a[2*i+1].rm;
    a[i].mm = max(max(a[2*i].mm,a[2*i+1].mm),a[2*i].rm+a[2*i+1].lm);
    if(a[2*i].lm == a[2*i].r-a[2*i].l+1)a[i].lm += a[2*i+1].lm;
    if(a[2*i+1].rm == a[2*i+1].r-a[2*i+1].l+1)a[i].rm += a[2*i].rm;
}
int query(int x,int p)
{
    if(a[x].l==a[x].r||a[x].mm==0||a[x].mm==a[x].len)
        return a[x].mm;
    int mid=(a[x].l+a[x].r)>>1;
    if(p<=mid){
        if(p>=a[x<<1].r-a[x<<1].rm+1)
            return query(x<<1,p)+query(x<<1|1,mid+1);
        else
            return query(x<<1,p);
    }
    else{
        if(p<=a[x<<1|1].l+a[x<<1|1].lm-1)
            return query(x<<1|1,p)+query(x<<1,mid);
        else
            return query(x<<1|1,p);
    }
} int main()
{
    int n,m,y;
    char x[3];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        stack<int> k;
        memset(a,0,sizeof(a));
        build(1,n,1);
        for(int i=0;i<m;i++)
        {
            scanf("%s",x);
            if(x[0]=='D')
            {
                y=read();
                updata(1,y,0);
                k.push(y);
            }
            else if(x[0]=='Q')
            {
                y=read();
                printf("%d\n",query(1,y));
            }
            else
            {
                updata(1,k.top(),1);
                k.pop();
            }
        }
    }
}

最新文章

  1. Codeforces Round #363 (Div. 2)A-D
  2. 7 HandlerSet 处理程序链表类——Live555源码阅读(一)基本组件类
  3. jquery的checkbox问题
  4. 文件频繁IO能有多大的差别
  5. [codility]Array-closest-ascenders
  6. uboot从SD卡烧写内核和文件系统
  7. ubuntu14.04 安装
  8. SSIS之Foreach循环容器用法
  9. ASP.NET WebForm 的路由
  10. 201521123036 《Java程序设计》第2周学习总结
  11. 【原创】使用workstation安装Xenserver 6.5+cloudstack 4.10----本地存储模式
  12. 小说接入UC浏览器内核技术对话(二)
  13. 学习sharding-jdbc 分库分表扩展框架
  14. TP5多模块开发
  15. SpringCloud(1)---基于RestTemplate微服务项目案例
  16. Calendar 得到前一天当前时间
  17. 使用Novell.Directory.Ldap.NETStandard在.NET Core中验证AD域账号
  18. java学习第03天(运算符、语句)
  19. Fri Oct 31 18:00:00 UTC+0800 2008转换为yyyy-mm-dd
  20. struct tm

热门文章

  1. 很多人都没用过的轻量级Oracle数据库数据导出工具SQLLDR2——性能超赞
  2. 013 GC机制
  3. ssh使两台机器建立连接
  4. 让Linux应用更加得心应手的
  5. [Spring Data JPA问题]Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException
  6. DotNetOpenAuth实践之WCF资源服务器配置
  7. [实战]MVC5+EF6+MySql企业网盘实战(22)——图片列表
  8. Js数组的常用的方法概述
  9. 在分享到微信里的网页中,打开qq对话框。
  10. mysql字符编码相关