码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1540 POJ 2892 Tunnel Warfare

时间:2015-12-05 09:45:21      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

线段树 区间合并 单点修改 区间查询。又是1秒钟构思,差错查了好久... ... 发现一个int型的定义成了char型,打脸。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

const int maxn= 50000+10;
int N,Q;
char op[5];
int num;
stack<int>S;
bool flag[maxn];
struct SegTree
{
    int lsum,rsum,msum;
}segTree[4*maxn];
int fail,ans1,ans2;

void pushUp(int rt,int len)
{
    if(segTree[2*rt].lsum==len-len/2)
        segTree[rt].lsum=segTree[2*rt].lsum+segTree[2*rt+1].lsum;
    else segTree[rt].lsum=segTree[2*rt].lsum;

    if(segTree[2*rt+1].rsum==len/2)
        segTree[rt].rsum=segTree[2*rt].rsum+segTree[2*rt+1].rsum;
    else segTree[rt].rsum=segTree[2*rt+1].rsum;

    segTree[rt].msum=max(segTree[2*rt].msum,segTree[2*rt+1].msum);
    segTree[rt].msum=max(segTree[rt].msum,segTree[2*rt+1].lsum+segTree[2*rt].rsum);
}

void build(int l,int r,int rt)
{
    if(l==r) 
    {
        segTree[rt].lsum=segTree[rt].rsum=segTree[rt].msum=1;
        return;
    }

    int m=(l+r)/2;
    build(l,m,2*rt);
    build(m+1,r,2*rt+1);
    pushUp(rt,r-l+1);
}

void update(int info,int node,int l,int r,int rt)
{
    if(l==r&&node==l)
    {
        segTree[rt].lsum=segTree[rt].rsum=segTree[rt].msum=info;
        return;
    }

    int m=(l+r)/2;
    if(node<=m) update(info,node,l,m,2*rt);
    else update(info,node,m+1,r,2*rt+1);

    pushUp(rt,r-l+1);
}

void quary1(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        ans1=ans1+segTree[rt].rsum;
        if(segTree[rt].rsum!=r-l+1) fail=1;
        return;
    }

    int m=(l+r)/2;
    if(R>m) quary1(L,R,m+1,r,2*rt+1);
    if(fail) return;
    if(L<=m) quary1(L,R,l,m,2*rt);
    if(fail) return;
}

void quary2(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        ans2=ans2+segTree[rt].lsum;
        if(segTree[rt].lsum!=r-l+1) fail=1;
        return;
    }

    int m=(l+r)/2;
    if(L<=m) quary2(L,R,l,m,2*rt);
    if(fail) return;
    if(R>m) quary2(L,R,m+1,r,2*rt+1);
    if(fail) return;
}

int main()
{
//    freopen("in.txt","r",stdin);
    while(~scanf("%d%d",&N,&Q))
    {
        while(!S.empty()) S.pop();
        
        build(1,N,1);
        for(int i=0;i<=N;i++) flag[i]=1;

        for(int i=1;i<=Q;i++)
        {
            scanf("%s",op);
            if(op[0]==D)
            {
                scanf("%d",&num);
                S.push(num);
                flag[num]=0;
                update(0,num,1,N,1);
            }
            else if(op[0]==Q)
            {
                scanf("%d",&num);
                if(!flag[num]) printf("%d\n",0);
                else
                {
                    ans1=0,ans2=0;
                    fail=0;
                    if(num-1>=1) quary1(1,num-1,1,N,1);
                    fail=0;
                    if(num+1<=N) quary2(num+1,N,1,N,1);

                    int ans=ans1+ans2+1;
                
                    printf("%d\n",ans);
                }
            }
            else if(op[0]==R)
            {
                if(S.empty()) continue;
                num=S.top();
                S.pop();
                flag[num]=1;
                update(1,num,1,N,1);
            }
        }
    }
    return 0;
}

 

HDU 1540 POJ 2892 Tunnel Warfare

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/5020997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!