hdu1540线段树连续区间

模板题>.<当初学了一波又忘了

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define MIN(a,b) a<b ? a:b

using namespace std;

const double g=10.0,eps=1e-9;
const int N=50000+10,maxn=100000+10,inf=0x3f3f3f;

int ml[N<<2],mr[N<<2],lr[N<<2],ans;
void btree(int l,int r,int rt)
{
    ml[rt]=mr[rt]=lr[rt]=r-l+1;
    if(l==r)return ;
    int m=(l+r)>>1;
    btree(ls);
    btree(rs);
}
void update(int l,int r,int rt,int x,int c)
{
    if(l==r)
    {
        ml[rt]=mr[rt]=lr[rt]=c;
        return ;
    }
    int m=(l+r)>>1;
    if(x<=m)update(ls,x,c);
    else update(rs,x,c);
    if((ml[rt]=ml[rt<<1])==m-l+1)ml[rt]+=ml[rt<<1|1];
    if((mr[rt]=mr[rt<<1|1])==r-m)mr[rt]+=mr[rt<<1];
    lr[rt]=max(max(lr[rt<<1],lr[rt<<1|1]),mr[rt<<1]+ml[rt<<1|1]);
}
void query(int l,int r,int rt,int x)
{
    if(l==r||lr[rt]==0||lr[rt]==r-l+1)
    {
        ans+=lr[rt];
        return ;
    }
    int m=(l+r)>>1;
    if(x<=m)
    {
        if(x>m-mr[rt<<1])query(ls,x),query(rs,m+1);
        else query(ls,x);
    }
    else
    {
        if(x<m+1+ml[rt<<1|1])query(ls,m),query(rs,x);
        else query(rs,x);
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m;
    while(cin>>n>>m){
        btree(1,n,1);
        stack<int>st;
        while(m--){
            string s;
            cin>>s;
            if(s[0]=='D')
            {
                int x;
                cin>>x;
                st.push(x);
                update(1,n,1,x,0);
            }
            else if(s[0]=='R')
            {
                update(1,n,1,st.top(),1);
                st.pop();
            }
            else
            {
                ans=0;
                int x;
                cin>>x;
                query(1,n,1,x);
                cout<<ans<<endl;
            }
        }
    }
    return 0;
}
View Code

 

posted @ 2017-07-04 18:58  walfy  阅读(156)  评论(0编辑  收藏  举报