题解
我对莫队真是一无所知
这个东西显然可以用圆方树转成一个dfs序列
然后呢,用莫队计算每个询问区间的每个数出现的次数,从而顺带计算每个数字的奇偶性
但是我们要查的数字也用一个范围,可以直接用分块维护,修改\(O(1)\)查询\(O(n)\)
代码
#include <bits/stdc++.h>
#define enter putchar('\n')
#define space putchar(' ')
#define pii pair<int,int>
#define fi first
#define se second
#define MAXN 200005
#define pb push_back
#define mp make_pair
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) out(x / 10);
putchar('0' + x % 10);
}
struct qry_node {
int l,r,bl,v,y,id;
friend bool operator < (const qry_node &a,const qry_node &b) {
if(a.bl != b.bl) return a.bl < b.bl;
if(a.r != b.r) return a.r < b.r;
if(a.l != b.l) return a.l < b.l;
return a.id < b.id;
}
}qry[MAXN];
struct node {
int to,next;
}E[MAXN * 2];
int N,M,Q;
int head[MAXN],sumE;
int siz[MAXN],L[MAXN],a[MAXN],num[MAXN],cnt,S[350][2],bl[MAXN],br[MAXN],id[MAXN],tot;
int tims[MAXN],st,ed,ans[MAXN];
vector<int> ver[MAXN];
int sta[MAXN],top,dfn[MAXN],low[MAXN],idx;
void add(int u,int v) {
E[++sumE].to = v;
E[sumE].next = head[u];
head[u] = sumE;
}
void Tarjan(int u) {
dfn[u] = low[u] = ++idx;
sta[++top] = u;
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(dfn[v]) low[u] = min(low[u],dfn[v]);
else {
Tarjan(v);
if(low[v] >= dfn[u]) {
while(1) {
int x = sta[top--];
ver[u].pb(x);
if(x == v) break;
}
}
else low[u] = min(low[u],low[v]);
}
}
}
void dfs(int u) {
int s = ver[u].size();
dfn[u] = ++idx;siz[u] = 1;L[idx] = a[u];
for(int i = 0 ; i < s ; ++i) {
dfs(ver[u][i]);
siz[u] += siz[ver[u][i]];
}
}
void Init() {
read(N);read(M);
for(int i = 1 ; i <= N ; ++i) {read(a[i]);num[i] = a[i];}
sort(num + 1,num + N + 1);
cnt = unique(num + 1,num + N + 1) - num - 1;
for(int i = 1 ; i <= N ; ++i) a[i] = lower_bound(num + 1,num + cnt + 1,a[i]) - num;
int u,v;
for(int i = 1 ; i <= M ; ++i) {
read(u);read(v);add(u,v);add(v,u);
}
Tarjan(1);
idx = 0;
dfs(1);
int s = sqrt(cnt);
for(int i = 1 ; i <= cnt ; i += s) {
bl[++tot] = i,br[tot] = min(cnt,i + s - 1);
}
for(int i = 1 ; i <= tot ; ++i) {
for(int j = bl[i] ; j <= br[i] ; ++j) {
id[j] = i;
}
}
s = sqrt(N);
read(Q);
for(int i = 1 ; i <= Q ; ++i) {
read(qry[i].v);read(u);read(qry[i].y);
qry[i].id = i;qry[i].l = dfn[u];qry[i].r = dfn[u] + siz[u] - 1;
qry[i].bl = qry[i].l / s + 1;
}
sort(qry + 1,qry + Q + 1);
}
void Insert(int p) {
if(tims[p]) S[id[p]][tims[p] & 1]--;
++tims[p];
S[id[p]][tims[p] & 1]++;
}
void Erase(int p) {
S[id[p]][tims[p] & 1]--;
--tims[p];
if(tims[p]) S[id[p]][tims[p] & 1]++;
}
void Move(int l,int r) {
while(ed < r) {Insert(L[++ed]);}
while(st > l) {Insert(L[--st]);}
while(ed > r) {Erase(L[ed--]);}
while(st < l) {Erase(L[st++]);}
}
void Solve() {
st = 1,ed = 0;
for(int i = 1 ; i <= Q ; ++i) {
Move(qry[i].l,qry[i].r);
int res = 0;
int t;
if(qry[i].y >= num[cnt]) t = cnt;
else t = upper_bound(num + 1,num + cnt + 1,qry[i].y) - num - 1;
for(int j = 1 ; j <= id[t] - 1 ; ++j) {
res += S[j][qry[i].v];
}
for(int j = bl[id[t]] ; j <= t ; ++j) {
if(!tims[j]) continue;
res += ((tims[j] & 1) == qry[i].v);
}
ans[qry[i].id] = res;
}
for(int i = 1 ; i <= Q ; ++i) {
out(ans[i]);enter;
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Init();
Solve();
return 0;
}