【cdq分治】【整体二分】bzoj 3110: [Zjoi2013] HYSBZ - 3110 K大数查询

题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3110

题意:有N个位置,M个操作。操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c。如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少。注意是加入一个数,不是让这个数去求和。

题解:虽然是看cdq找到这题,但是感觉这个和平时做的三维偏序不大一样。这题其实是整体二分。就是首先,每次询问的答案应该是1,n之间的,然后我们会对答案进行二分。我们每次cdq分治时有四个参数,vl,vr分别代表此时二分到的可能答案的区间。l,r代表操作下标的区间。然后我们线段树维护的是现在的位置,有多少个值比mid大的数。然后每次遇到询问操作,我们可以通过线段树查找到此时询问操作的区间内有多少个比m大的数,记为tem,然后就像主席树一样,tem>=op[i].v,那么这个操作答案肯定是m+1,vr,否则答案肯定是l,m。然后就是二分处理啦。其实是答案二分,但是操作不是二分的,操作只是分组了。

然后注意不能在分治里建树,初始化的话直接就打个下标,下标clear记录的是这个节点的左右儿子是不是应该被初始化,注意初始化只能一次。

  1 /*************************************************************************
  2     > File Name: HYSBZ-3110.cpp
  3 # File Name: HYSBZ-3110.cpp
  4 # Author : xiaobuxie
  5 # QQ : 760427180
  6 # Email:[email protected]
  7 # Created Time: 2019年09月13日 星期五 15时22分58秒
  8  ************************************************************************/
  9 
 10 #include<iostream>
 11 #include<cstdio>
 12 #include<map>
 13 #include<cmath>
 14 #include<cstring>
 15 #include<set>
 16 #include<queue>
 17 #include<vector>
 18 #include<algorithm>
 19 using namespace std;
 20 typedef long long ll;
 21 #define inf 0x3f3f3f3f
 22 #define pq priority_queue<int,vector<int>,greater<int> >
 23 ll gcd(ll a,ll b){
 24     if(a<b) return gcd(b,a);
 25     return b==0?a:gcd(b,a%b);
 26 }
 27 
 28 const int N=5e4+9;
 29 struct node{
 30     ll sum,lazy;
 31     bool clear;
 32 }tr[N<<2];
 33 struct option{
 34     int ty,l,r,ans,id,ord;
 35     ll v;
 36 }op[N];
 37 bool cmpid(option a,option b){return a.id<b.id;}
 38 bool cmpord(option a,option b){return a.ord<b.ord;}
 39 int n,m;
 40 void push_down(int o,int l,int r){
 41     if(l!=r && tr[o].clear){
 42         tr[o<<1].sum=tr[o<<1|1].sum=tr[o<<1].lazy=tr[o<<1|1].lazy=0;
 43         tr[o<<1].clear=tr[o<<1|1].clear=1;
 44         tr[o].clear=0;
 45     }
 46     if(tr[o].lazy && l!=r){
 47         int m=(l+r)>>1;
 48         tr[o<<1].lazy+=tr[o].lazy;
 49         tr[o<<1|1].lazy+=tr[o].lazy;
 50         tr[o<<1].sum+=tr[o].lazy*(m-l+1);
 51         tr[o<<1|1].sum+=tr[o].lazy*(r-m);
 52         tr[o].lazy=0;
 53     }
 54 }
 55 void change(int o,int l,int r,int x,int y,ll v){
 56     push_down(o,l,r);
 57     if(x<=l && r<=y){
 58         tr[o].sum+=(r-l+1)*v;
 59         tr[o].lazy+=v;
 60         return;
 61     }
 62     int m=(l+r)>>1;
 63     if(x<=m) change(o<<1,l,m,x,y,v);
 64     if(y>m)  change(o<<1|1,m+1,r,x,y,v);
 65     tr[o].sum=tr[o<<1].sum+tr[o<<1|1].sum;
 66 }
 67 ll query(int o,int l,int r,int x,int y){
 68     push_down(o,l,r);
 69     if(x<=l && r<=y) return tr[o].sum;
 70     int m=(l+r)>>1;
 71     ll ans=0;
 72     if(x<=m) ans+=query(o<<1,l,m,x,y);
 73     if(y>m)  ans+=query(o<<1|1,m+1,r,x,y);
 74     tr[o].sum=tr[o<<1].sum+tr[o<<1|1].sum;
 75     return ans;
 76 }
 77 void cdq(int vl,int vr,int l,int r){
 78     if(vl==vr){
 79         for(int i=l;i<=r;++i) if(op[i].ty==2) op[i].ans=vl;
 80         return;
 81     }
 82     int m=(vl+vr)>>1;
 83     tr[1].sum=tr[1].lazy=0; tr[1].clear=1;
 84     int nl=0,nr=r-l+1;
 85     for(int i=l;i<=r;++i){
 86         if(op[i].ty==1){
 87             if(op[i].v <= m) op[i].id=++nl;
 88             else{
 89                 op[i].id=++nr;
 90                 change(1,1,n,op[i].l,op[i].r,1);
 91             }
 92         }
 93         else{
 94             ll tem=query(1,1,n,op[i].l,op[i].r);
 95             //cerr<<op[i].v<<' '<<op[i].ord<<' '<<tem<<endl;
 96             if(tem>=op[i].v) op[i].id=++nr;
 97             else{
 98                 op[i].id=++nl;
 99                 op[i].v-=tem;
100             }
101         }
102     }
103     sort(op+l,op+r+1,cmpid);
104     cdq(vl,m,l,l+nl-1);
105     cdq(m+1,vr,l+nl,r);
106 }
107 int main(){
108     scanf("%d %d",&n,&m);
109     for(int i=1;i<=m;++i) scanf("%d %d %d %lld",&op[i].ty,&op[i].l,&op[i].r,&op[i].v),op[i].ord=i;
110     cdq(1,n,1,m);
111     sort(op+1,op+1+m,cmpord);
112     for(int i=1;i<=m;++i) if(op[i].ty==2) printf("%d\n",op[i].ans);
113     return 0;
114 }
View Code

猜你喜欢

转载自www.cnblogs.com/xiaobuxie/p/11517182.html
今日推荐