# 【BZOJ4571】[SCOI2016] 美味（主席树）

### 代码

#include<bits/stdc++.h>
#define N 200000
#define Log 17
#define Gmax(x,y) (x<(y)&&(x=(y)))
#define Gmin(x,y) (x>(y)&&(x=(y)))
using namespace std;
int n,a[N+5];
class Class_FIO
{
private:
#define Fsize 100000
#define pc(ch) (void)(FoutSize<Fsize?Fout[FoutSize++]=ch:(fwrite(Fout,1,Fsize,stdout),Fout[(FoutSize=0)++]=ch))
int Top,FoutSize;char ch,*A,*B,Fin[Fsize],Fout[Fsize],Stack[Fsize];
public:
Class_FIO() {A=B=Fin;}
inline void writeln(int x) {if(!x) return pc('0'),pc('\n');while(x) Stack[++Top]=x%10+48,x/=10;while(Top) pc(Stack[Top--]);pc('\n');}
inline void clear() {fwrite(Fout,1,FoutSize,stdout),FoutSize=0;}
}F;
class Class_ChairmanTree//主席树
{
private:
#define TreeSize 100000
#define LogN 20
int tot,Root[N+5];
struct Tree
{
int Son[2],Size;
}node[N*LogN+5];
inline void ins(int l,int r,int &rt,int lst,int val)
{
if(node[rt=++tot]=node[lst],++node[rt].Size,!(l^r)) return;
register int mid=l+r>>1;
val<=mid?ins(l,mid,node[rt].Son[0],node[lst].Son[0],val):ins(mid+1,r,node[rt].Son[1],node[lst].Son[1],val);
}
inline bool qry(int l,int r,int rt1,int rt2,int ql,int qr)
{
if(ql<=l&&r<=qr) return node[rt2].Size^node[rt1].Size;
register int mid=l+r>>1;
return (ql<=mid&&qry(l,mid,node[rt1].Son[0],node[rt2].Son[0],ql,qr))||(qr>mid&&qry(mid+1,r,node[rt1].Son[1],node[rt2].Son[1],ql,qr));
}
public:
inline void Init(int len,int *data) {for(register int i=1;i<=len;++i) ins(0,TreeSize,Root[i],Root[i-1],data[i]);}//初始化
inline bool Query(int l,int r,int ql,int qr) {return Gmax(ql,0),Gmin(qr,TreeSize),ql<=qr?qry(0,TreeSize,Root[l-1],Root[r],ql,qr):false;}//询问第l~r个元素中是否有数字在[ql,qr]范围内
}ChairmanTree;
int main()
{
register int i,query_tot,x,y,l,r,L,R,t,ans;