POJ2892Tunnel Warfare

写完之后各种调试各种TLE,第二天早上整理了下思路,重写了一遍,立马AC了;具体见注释

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>
using namespace std;
#define INF 0xfffffff
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=55555;
int num[maxn<<1];
int sum[maxn<<2],lsum[maxn<<2],rsum[maxn<<2];
//sum数组存储连续村庄个数,lsum存储区间内从左起连续个数,rsum存储区间内从右起连续个数
void pushup(int rt,int m)
{
    lsum[rt]=lsum[rt<<1];
    rsum[rt]=rsum[rt<<1|1];
    if(lsum[rt]==(m-(m>>1))) lsum[rt]+=lsum[rt<<1|1];
    if(rsum[rt]==(m>>1)) rsum[rt]+=rsum[rt<<1];
    sum[rt]=max(max(sum[rt<<1],sum[rt<<1|1]),rsum[rt<<1]+lsum[rt<<1|1]);
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        lsum[rt]=rsum[rt]=sum[rt]=1;
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt,r-l+1);
}
void update(int key,int add,int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=lsum[rt]=rsum[rt]=add?1:0;
        return ;
    }
    int m=(l+r)>>1;
    if(key<=m) update(key,add,lson);
    else update(key,add,rson);
    pushup(rt,r-l+1);
}
int query(int key,int l,int r,int rt)
{
    if(l==r)
    {
        return sum[rt];
    }
    int m=(l+r)>>1;
    int ans=0;
    int a=l+lsum[rt]-1;int b=r-rsum[rt]+1;
    if(key>=l&&key<=a) ans=lsum[rt];
    if(key>=b&&key<=r) ans=max(ans,rsum[rt]);
    int c=m-rsum[rt<<1]+1;int d=m+lsum[rt<<1|1];
    if(key>=c&&key<=d) ans=max(ans,rsum[rt<<1]+lsum[rt<<1|1]);
    if(ans) return ans;
    if(key<=m) return query(key,lson);
    else return query(key,rson);
}
int main()
{
    int n,m,a;
    char str[100];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,n,1);
        int cnt=0;
        while(m--)
        {
            scanf("%s",str);
            if(str[0]=='D')
            {
                scanf("%d",&a);
                update(a,0,1,n,1);
                num[cnt++]=a;
            }
            else if(str[0]=='R')
            {
                int t=num[--cnt];
                update(t,1,1,n,1);
            }
            else
            {
                scanf("%d",&a);
                printf("%d\n",query(a,1,n,1));
            }
        }
    }
    return 0;
}
时间: 2024-11-24 01:47:50

POJ2892Tunnel Warfare的相关文章

POJ-2892-Tunnel Warfare(线段树)

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

POJ--2892--Tunnel Warfare【线段树】区间合并

链接:http://poj.org/problem?id=2892 题意:有n个村庄排成一排,三种操作: 1. D x 摧毁村庄x 2. Q x 询问村庄x的最长一段没有被摧毁的村庄数量 3. R   恢复上一个被摧毁的村庄 思路:线段树区间合并,lsum记录当前节点往左的最长连续距离,rsum记录当前节点往右的最长连续距离. #include<cstring> #include<string> #include<fstream> #include<iostrea

poj2892--Tunnel Warfare(线段树)

题目链接:点击打开链接 题目大意:给出n个格子,三种操作,D k:消除第k个格子,R:恢复最后一次消除的格子,Q k:问和k相连的最长连续序列. 求最长连续序列,线段树统计每段中被消除的最大点和最小点,更新点k,查询段[1,k][k,n],然后相减得到结果 #include <cstdio> #include <cstring> #include <stack> #include <algorithm> using namespace std ; #defi

hdu1540 Tunnel Warfare 线段树/树状数组

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 con

HDU 1540 Tunnel Warfare(线段树,单点更新,区间查询)

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 vill

【线段树区间合并】HDU1540-Tunnel Warfare

一.题目 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

hdu--1540 Tunnel Warfare(线段树+区间合并)

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

HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并

Tunnel Warfare                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast

uva 1416 Warfare And Logistics (最短路树)

uva 1416 Warfare And Logistics Description The army of United Nations launched a new wave of air strikes on terrorist forces. The objective of the mission is to reduce enemy's logistical mobility. Each air strike will destroy a path and therefore inc