zoukankan      html  css  js  c++  java
  • 线段树-维护区间合并

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

    写了我老久

      1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
      2 #include <cstdio>//sprintf islower isupper
      3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
      4 #include <iostream>//pair
      5 #include <fstream>//freopen("C:\Users\13606\Desktop\草稿.txt","r",stdin);
      6 #include <bitset>
      7 //#include <map>
      8 //#include<unordered_map>
      9 #include <vector>
     10 #include <stack>
     11 #include <set>
     12 #include <string.h>//strstr substr
     13 #include <string>
     14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
     15 #include <cmath>
     16 #include <deque>
     17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
     18 #include <vector>//emplace_back
     19 //#include <math.h>
     20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
     21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
     22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
     23 //******************
     24 int abss(int a);
     25 int lowbit(int n);
     26 int Del_bit_1(int n);
     27 int maxx(int a,int b);
     28 int minn(int a,int b);
     29 double fabss(double a);
     30 void swapp(int &a,int &b);
     31 clock_t __STRAT,__END;
     32 double __TOTALTIME;
     33 void _MS(){__STRAT=clock();}
     34 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
     35 //***********************
     36 #define rint register int
     37 #define fo(a,b,c) for(rint a=b;a<=c;++a)
     38 #define fr(a,b,c) for(rint a=b;a>=c;--a)
     39 #define mem(a,b) memset(a,b,sizeof(a))
     40 #define pr printf
     41 #define sc scanf
     42 #define ls rt<<1
     43 #define rs rt<<1|1
     44 typedef long long ll;
     45 const double E=2.718281828;
     46 const double PI=acos(-1.0);
     47 //const ll INF=(1LL<<60);
     48 const int inf=(1<<30);
     49 const double ESP=1e-9;
     50 const int mod=(int)1e9+7;
     51 const int N=(int)1e6+10;
     52 
     53 struct node
     54 {
     55     int l,r;
     56     int lmax,rmax,len;
     57 }tr[N<<2];
     58 
     59 void up(int rt)
     60 {
     61     tr[rt].rmax=tr[rs].rmax;
     62     if(tr[rs].rmax==tr[rs].len)tr[rt].rmax=tr[rs].len+tr[ls].rmax;
     63     tr[rt].lmax=tr[ls].lmax;
     64     if(tr[ls].lmax==tr[ls].len)tr[rt].lmax=tr[ls].len+tr[rs].lmax;
     65 }
     66 void Build(int l,int r,int rt)
     67 {
     68     tr[rt].l=l;
     69     tr[rt].r=r;
     70     tr[rt].len=r-l+1;
     71     tr[rt].lmax=tr[rt].rmax=r-l+1;
     72     if(l==r)return;
     73     int mid=l+r>>1;
     74     Build(l,mid,ls);
     75     Build(mid+1,r,rs);
     76 //    up(rt);
     77 }
     78 void Delete(int pos,int l,int r,int rt)
     79 {
     80     if(l==r)
     81     {
     82         tr[rt].lmax=tr[rt].rmax=0;
     83         return;
     84     }
     85     int mid=l+r>>1;
     86     if(pos<=mid)Delete(pos,l,mid,ls);
     87     else Delete(pos,mid+1,r,rs);
     88     up(rt);
     89 }
     90 void Rebuild(int pos,int l,int r,int rt)
     91 {
     92     if(l==r)
     93     {
     94         tr[rt].lmax=tr[rt].rmax=1;
     95         return;
     96     }
     97     int mid=l+r>>1;
     98     if(pos<=mid)Rebuild(pos,l,mid,ls);
     99     else Rebuild(pos,mid+1,r,rs);
    100     up(rt);
    101 }
    102 int QnumL(int pos,int l,int r,int rt)
    103 {
    104     if(pos>=r)return tr[rt].rmax;
    105     int mid=l+r>>1;
    106     if(pos>mid)
    107     {
    108         if(tr[rs].lmax<pos-tr[rs].l+1)
    109         {
    110             return max(QnumL(pos,mid+1,r,rs),tr[rs].rmax-(tr[rs].r-pos+1)+1);
    111         }
    112         else return pos-tr[rs].l+1+tr[ls].rmax;
    113     }
    114     else return QnumL(pos,l,mid,ls);
    115 }
    116 int QnumR(int pos,int l,int r,int rt)
    117 {
    118     if(pos<=l)return tr[rt].lmax;
    119     int mid=l+r>>1;
    120     if(pos<=mid)
    121     {
    122         if(tr[ls].rmax<tr[ls].r-pos+1)
    123         {
    124             return max(QnumR(pos,l,mid,ls),tr[ls].lmax-(pos-tr[ls].l+1)+1);
    125         }
    126         else return tr[ls].r-pos+1+tr[rs].lmax;
    127     }
    128     else return QnumR(pos,mid+1,r,rs);
    129 }
    130 
    131 int op[N];
    132 bool D[N];
    133 int main()
    134 {
    135     int n,m;
    136     while(~sc("%d%d",&n,&m))
    137     {
    138         int cnt=0;
    139         Build(1,n,1);
    140         for(int i=1;i<=n;++i)
    141             D[i]=1;
    142         for(int i=1;i<=m;++i)
    143         {
    144             char j[5];
    145             int pos=0;
    146             sc("%s",j);
    147             if(j[0]!='R')sc("%d",&pos);
    148 
    149             if(j[0]=='D')
    150                 Delete(pos,1,n,1),op[++cnt]=pos,D[op[cnt]]=0;
    151             else if(j[0]=='Q')
    152             {
    153                 if(D[pos]==0)
    154                 {
    155                     pr("0
    ");
    156                     continue;
    157                 }
    158                 int ansl=QnumL(pos,1,n,1);
    159                 int ansr=QnumR(pos,1,n,1);
    160                 pr("%d
    ",ansl+ansr+(ansl||ansr?-1:0));
    161             }
    162             else Rebuild(op[cnt],1,n,1),D[op[cnt]]=1,cnt--;
    163         }
    164     }
    165     return 0;
    166 }
    167 
    168 /**************************************************************************************/
    169 
    170 int maxx(int a,int b)
    171 {
    172     return a>b?a:b;
    173 }
    174 
    175 void swapp(int &a,int &b)
    176 {
    177     a^=b^=a^=b;
    178 }
    179 
    180 int lowbit(int n)
    181 {
    182     return n&(-n);
    183 }
    184 
    185 int Del_bit_1(int n)
    186 {
    187     return n&(n-1);
    188 }
    189 
    190 int abss(int a)
    191 {
    192     return a>0?a:-a;
    193 }
    194 
    195 double fabss(double a)
    196 {
    197     return a>0?a:-a;
    198 }
    199 
    200 int minn(int a,int b)
    201 {
    202     return a<b?a:b;
    203 }
  • 相关阅读:
    css 透明气泡效果
    uniapp 跳转tabbar页面传递参数
    unaipp 发送验证码倒计时
    uniapp 返回顶部
    js 实现放大镜效果
    js 禁用右键菜单和禁止复制
    js 表格的添加和删除操作
    js 留言板(带删除功能)
    推荐几个好用的网站
    pc端使用rem适配
  • 原文地址:https://www.cnblogs.com/--HPY-7m/p/11714103.html
Copyright © 2011-2022 走看看