• 线段树维护动态连续子段HDU1540


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

    #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
    #include <cstdio>//sprintf islower isupper
    #include <cstdlib>//malloc  exit strcat itoa system("cls")
    #include <iostream>//pair
    #include <fstream>//freopen("C:\Users\13606\Desktop\草稿.txt","r",stdin);
    #include <bitset>
    //#include <map>
    //#include<unordered_map>
    #include <vector>
    #include <stack>
    #include <set>
    #include <string.h>//strstr substr
    #include <string>
    #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
    #include <cmath>
    #include <deque>
    #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
    #include <vector>//emplace_back
    //#include <math.h>
    //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
    #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
    using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
    //******************
    int abss(int a);
    int lowbit(int n);
    int Del_bit_1(int n);
    int maxx(int a,int b);
    int minn(int a,int b);
    double fabss(double a);
    void swapp(int &a,int &b);
    clock_t __STRAT,__END;
    double __TOTALTIME;
    void _MS(){__STRAT=clock();}
    void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
    //***********************
    #define rint register int
    #define fo(a,b,c) for(rint a=b;a<=c;++a)
    #define fr(a,b,c) for(rint a=b;a>=c;--a)
    #define mem(a,b) memset(a,b,sizeof(a))
    #define pr printf
    #define sc scanf
    #define ls rt<<1
    #define rs rt<<1|1
    typedef long long ll;
    const double E=2.718281828;
    const double PI=acos(-1.0);
    //const ll INF=(1LL<<60);
    const int inf=(1<<30);
    const double ESP=1e-9;
    const int mod=(int)1e9+7;
    const int N=(int)1e6+10;
    
    struct node
    {
        int l,r;
        int lmax,rmax,len;
    }tr[N<<2];
    
    void up(int rt)
    {
        tr[rt].rmax=tr[rs].rmax;
        if(tr[rs].rmax==tr[rs].len)tr[rt].rmax=tr[rs].len+tr[ls].rmax;
        tr[rt].lmax=tr[ls].lmax;
        if(tr[ls].lmax==tr[ls].len)tr[rt].lmax=tr[ls].len+tr[rs].lmax;
    }
    void Build(int l,int r,int rt)
    {
        tr[rt].l=l;
        tr[rt].r=r;
        tr[rt].len=r-l+1;
        tr[rt].lmax=tr[rt].rmax=r-l+1;
        if(l==r)return;
        int mid=l+r>>1;
        Build(l,mid,ls);
        Build(mid+1,r,rs);
    //    up(rt);
    }
    void Delete(int pos,int l,int r,int rt)
    {
        if(l==r)
        {
            tr[rt].lmax=tr[rt].rmax=0;
            return;
        }
        int mid=l+r>>1;
        if(pos<=mid)Delete(pos,l,mid,ls);
        else Delete(pos,mid+1,r,rs);
        up(rt);
    }
    void Rebuild(int pos,int l,int r,int rt)
    {
        if(l==r)
        {
            tr[rt].lmax=tr[rt].rmax=1;
            return;
        }
        int mid=l+r>>1;
        if(pos<=mid)Rebuild(pos,l,mid,ls);
        else Rebuild(pos,mid+1,r,rs);
        up(rt);
    }
    int QnumL(int pos,int l,int r,int rt)
    {
        if(pos>=r)return tr[rt].rmax;
        int mid=l+r>>1;
        if(pos>mid)
        {
            if(tr[rs].lmax<pos-tr[rs].l+1)
            {
                return max(QnumL(pos,mid+1,r,rs),tr[rs].rmax-(tr[rs].r-pos+1)+1);
            }
            else return pos-tr[rs].l+1+tr[ls].rmax;
        }
        else return QnumL(pos,l,mid,ls);
    }
    int QnumR(int pos,int l,int r,int rt)
    {
        if(pos<=l)return tr[rt].lmax;
        int mid=l+r>>1;
        if(pos<=mid)
        {
            if(tr[ls].rmax<tr[ls].r-pos+1)
            {
                return max(QnumR(pos,l,mid,ls),tr[ls].lmax-(pos-tr[ls].l+1)+1);
            }
            else return tr[ls].r-pos+1+tr[rs].lmax;
        }
        else return QnumR(pos,mid+1,r,rs);
    }
    
    int op[N];
    bool D[N];
    int main()
    {
        int n,m;
        while(~sc("%d%d",&n,&m))
        {
            int cnt=0;
            Build(1,n,1);
            for(int i=1;i<=n;++i)
                D[i]=1;
            for(int i=1;i<=m;++i)
            {
                char j[5];
                int pos=0;
                sc("%s",j);
                if(j[0]!='R')sc("%d",&pos);
    
                if(j[0]=='D')
                    Delete(pos,1,n,1),op[++cnt]=pos,D[op[cnt]]=0;
                else if(j[0]=='Q')
                {
                    if(D[pos]==0)
                    {
                        pr("0
    ");
                        continue;
                    }
                    int ansl=QnumL(pos,1,n,1);
                    int ansr=QnumR(pos,1,n,1);
                    pr("%d
    ",ansl+ansr+(ansl||ansr?-1:0));
                }
                else Rebuild(op[cnt],1,n,1),D[op[cnt]]=1,cnt--;
            }
        }
        return 0;
    }
    
    /**************************************************************************************/
    
    int maxx(int a,int b)
    {
        return a>b?a:b;
    }
    
    void swapp(int &a,int &b)
    {
        a^=b^=a^=b;
    }
    
    int lowbit(int n)
    {
        return n&(-n);
    }
    
    int Del_bit_1(int n)
    {
        return n&(n-1);
    }
    
    int abss(int a)
    {
        return a>0?a:-a;
    }
    
    double fabss(double a)
    {
        return a>0?a:-a;
    }
    
    int minn(int a,int b)
    {
        return a<b?a:b;
    }
  • 相关阅读:
    指针类型强制转换
    Spark大师之路:广播变量(Broadcast)源代码分析
    [Python]sqlite3二进制文件存储问题(BLOB)(You must not use 8-bit bytestrings unless you use a text_factory...)
    把字符串转化成整型显示
    一张图让你看清Java集合类(Java集合类的总结)
    Java读书笔记三(字符串)
    Afinal载入网络图片及下载文件用法
    netfilter/iptables 结构要点
    OpenGL 实现Interpolation插值算法
    GPU 编程入门到精通(五)之 GPU 程序优化进阶
  • 原文地址:https://www.cnblogs.com/--HPY-7m/p/11787232.html
Copyright © 2020-2023  润新知