# BZOJ3236 [Ahoi2013]作业 【莫队 + 树状数组】

BZOJ3236

## 题解

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
#define lbt(x) (x & -x)
using namespace std;
const int maxn = 100005,maxm = 3000005,INF = 1000000000;
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,m,a[maxm],b[maxm],bi,B,tot,ans1[maxm],ans2[maxm];
struct Que{int l,r,a,b,bl,id;}q[maxm];
inline bool operator <(const Que& a,const Que& b){
return a.bl == b.bl ? a.r < b.r : a.l < b.l;
}
int getn(int x){return lower_bound(b + 1,b + 1 + tot,x) - b;}
int bac[maxm];
struct BIT{
int s[maxm];
void add(int u,int v){while (u <= tot) s[u] += v,u += lbt(u);}
int query(int u){int re = 0; while (u) re += s[u],u -= lbt(u); return re;}
int sum(int l,int r){return query(r) - query(l - 1);}
}T1,T2;
void solve(){
sort(q + 1,q + 1 + m);
int L = q[1].l,R = q[1].r;
for (int i = L; i <= R; i++){
bac[a[i]]++;
}
ans1[q[1].id] = T1.sum(q[1].a,q[1].b);
ans2[q[1].id] = T2.sum(q[1].a,q[1].b);
for (int i = 2; i <= m; i++){
while (L != q[i].l || R != q[i].r){
if (L < q[i].l){
bac[a[L]]--;
L++;
}
if (L > q[i].l){
L--;
bac[a[L]]++;
}
if (R < q[i].r){
R++;
bac[a[R]]++;
}
if (R > q[i].r){
bac[a[R]]--;
R--;
}
}
ans1[q[i].id] = T1.sum(q[i].a,q[i].b);
ans2[q[i].id] = T2.sum(q[i].a,q[i].b);
}
for (int i = 1; i <= m; i++)
printf("%d %d\n",ans1[i],ans2[i]);
}
int main(){
REP(i,n) a[i] = b[++bi] = read();
REP(i,m){