# 【BZOJ3333】排队计划（树状数组+线段树）

### 代码

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 500000
#define LL long long
using namespace std;
int n,a[N+5],s[N+5],dc,dv[N+5];
class FastIO
{
private:
#define FS 100000
#define pc(c) (C==E&&(clear(),0),*C++=c)
#define D isdigit(c=tc())
int T;char c,*A,*B,*C,*E,FI[FS],FO[FS],S[FS];
public:
I FastIO() {A=B=FI,C=FO,E=FO+FS;}
Tp I void read(Ty& x) {x=0;W(!D);W(x=(x<<3)+(x<<1)+(c&15),D);}
Tp I void writeln(Ty x) {W(S[++T]=x%10+48,x/=10);W(T) pc(S[T--]);pc('\n');}
I void clear() {fwrite(FO,1,C-FO,stdout),C=FO;}
}F;
class SegmentTree//线段树
{
private:
#define PT CI l=1,CI r=n,CI rt=1
#define LT l,mid,rt<<1
#define RT mid+1,r,rt<<1|1
#define PU(x) (O[x].V=O[x<<1].V+O[x<<1|1].V,O[x].H=min(O[x<<1].H,O[x<<1|1].H))
struct node {LL V;int H;}O[N<<2];
public:
I void Build(PT)//建树
{
if(l==r) return (void)(O[rt].V=s[l],O[rt].H=a[l]);
int mid=l+r>>1;Build(LT),Build(RT),PU(rt);
}
I void U(CI s,CI x,PT)//修改
{
if(l==r) return (void)(O[rt].V=0,O[rt].H=dc+1);int mid=l+r>>1;//清零的同时赋一个极大值
s<=mid&&O[rt<<1].H<=x&&(U(s,x,LT),0),O[rt<<1|1].H<=x&&(U(s,x,RT),0),PU(rt);//如果存在小于等于x的数就去修改
}
I LL Q() {return O[1].V;}//询问整个序列和
}S;
struct TreeArray//树状数组
{
int v[N+5];I void U(RI x) {W(x<=dc) ++v[x],x+=x&-x;}//修改
I int Q(RI x,RI t=0) {W(x) t+=v[x],x-=x&-x;return t;}//询问
}T;
int main()
{