# [bzoj3809] Gty的二逼妹子序列

莫队+分块。。分块修改O(1),查询O(n^0.5)

总复杂度O(n^1.5)

1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 #include<algorithm>
5 #include<cmath>
6 #define ll long long
7 using namespace std;
8 const int maxn=100233;
9 struct zs{int l,r,a,b,id;}q[1002333];
10 int B[maxn],a[maxn],mp[maxn],l[maxn],r[maxn];
11 int ans[1002333];
12 int sz[2333];
13 int i,j,k,n,m,kuai;
14
15 int ra;char rx;
17     rx=getchar(),ra=0;
18     while(rx<'0'||rx>'9')rx=getchar();
19     while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra;
20 }
21
23 inline void del(int x){mp[x]--,sz[B[x]]-=!mp[x];}
24 inline int query(int a,int b){
25     register int i;int sm=0,idl=B[a],idr=B[b];
26     if(idl==idr){
27         for(i=a;i<=b;i++)sm+=mp[i]>0;
28         return sm;
29     }
30     for(i=a;i<=r[idl];i++)sm+=mp[i]>0;
31     for(i=l[idr];i<=b;i++)sm+=mp[i]>0;
32     for(i=idl+1;i<idr;i++)sm+=sz[i];
33     return sm;
34 }
35
36 bool cmp(zs a,zs b){return B[a.l]==B[b.l]?((B[a.l]&1)?a.r<b.r:a.r>b.r):B[a.l]<B[b.l];}
37 int main(){
40     for(i=1;i<=B[n];i++)
41         l[i]=(i-1)*kuai+1,r[i]=i==B[n]?n:(l[i]+kuai-1);
43     sort(q+1,q+1+m,cmp);
44     register int l=1,r=0;
45     for(i=1;i<=m;i++){