#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define re register usingnamespace std;
constint N=1e6+10;
structQuery{ int l,r,id; }q[N];
int n,m,len,ans[N],cnt[N],a[N];
inlineintget(re int x) { return x/len; }
inlineboolcmp(const Query &a,const Query &b) { int i=get(a.l),j=get(b.l); if (i!=j) return i<j; return (a.r<b.r)^(i&1); }
inlinevoidadd(re int x,int &res) { if (!cnt[a[x]]) res++; cnt[a[x]]++; }
inlinevoiddel(re int x,int &res) { cnt[a[x]]--; if (!cnt[a[x]]) res--; }
inlinevoidread(re int &x) { char c; while ((c=getchar())<'0'||c>'9') ; x=c-'0'; while ((c=getchar())>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0'; }
intmain() { read(n); for (re int i=1;i<=n;++i) read(a[i]); read(m); len=1620; for (re int i=1;i<=m;++i) { read(q[i].l),read(q[i].r); q[i].id=i; } sort(q+1,q+m+1,cmp); re int res=0; for (re int i=1,l=1,r=0;i<=m;++i) { while (l>q[i].l) add(--l,res); while (l<q[i].l) del(l++,res); while (r<q[i].r) add(++r,res); while (r>q[i].r) del(r--,res); ans[q[i].id]=res; } for (int i=1;i<=m;++i) printf("%d\n",ans[i]); return0; }
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define re register usingnamespace std;
constint N=1.5e5+10,M=1e6+10;
structQuery{ int id,l,r,t; }q[N];
structModify{ int pos,t; }c[N];
int ans[N],cnt[M],n,m,cm,qm,len,a[N];
inlineintget(re int x) { return x/len; }
inlineboolcmp(const Query &a,const Query &b) { re int i1=get(a.l),i2=get(b.l); re int j1=get(a.r),j2=get(b.r); if (i1!=i2) return i1<i2; if (j1!=j2) return j1<j2; return a.t<b.t; }
inlinevoidadd(re int a,int &res) { if (!cnt[a]) res++; cnt[a]++; }
inlinevoiddel(re int a,int &res) { cnt[a]--; if (!cnt[a]) res--; }
inlinevoidread(re int &x) { char c; while ((c=getchar())<'0'||c>'9') ; x=c-'0'; while ((c=getchar())>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0'; }
intmain() { read(n);read(m); for (re int i=1;i<=n;++i) read(a[i]); for (re int i=1;i<=m;++i) { re char op[2];re int a,b; scanf("%s",op); read(a);read(b); if (op[0]=='Q') ++qm,q[qm]=(Query){qm,a,b,cm}; else ++cm,c[cm]=(Modify){a,b}; } len=pow((double)n*cm,1.0/3.0);//pow((double)n*n,1.0/3.0) sort(q+1,q+qm+1,cmp); re int res=0; for (re int k=1,i=1,j=0,tnow=0;k<=qm;++k) { re int id=q[k].id,l=q[k].l,r=q[k].r,t=q[k].t; while (i>l) add(a[--i],res); while (i<l) del(a[i++],res); while (j<r) add(a[++j],res); while (j>r) del(a[j--],res); while (tnow<t) { ++tnow; if (c[tnow].pos>=i&&c[tnow].pos<=j) { del(a[c[tnow].pos],res); add(c[tnow].t,res); } swap(a[c[tnow].pos],c[tnow].t); } while (tnow>t) { if (c[tnow].pos>=i&&c[tnow].pos<=j) { del(a[c[tnow].pos],res); add(c[tnow].t,res); } swap(a[c[tnow].pos],c[tnow].t); tnow--; } ans[id]=res; } for (re int i=1;i<=qm;++i) printf("%d\n",ans[i]); return0; }
intmain() { scanf("%d %d",&n,&m); for (int i=1;i<=n;++i) scanf("%d",a+i),nums.push_back(a[i]); for (int i=1;i<=m;++i) scanf("%d %d",&q[i].l,&q[i].r); for (int i=1;i<=m;++i) q[i].id=i;
sort(nums.begin(),nums.end()); nums.erase(unique(nums.begin(),nums.end()),nums.end()); for (int i=1;i<=n;++i) a[i]=lower_bound(nums.begin(),nums.end(),a[i])-nums.begin(); len=sqrt(n); sort(q+1,q+m+1,cmp); for (int x=1;x<=m;) { int y=x; while (y<=m&&get(q[y].l)==get(q[x].l)) y++; int node=get(q[x].l)*len+len-1; while (x<=m&&q[x].r<=node) { ll res=0; for (int i=q[x].l;i<=q[x].r;++i) add(a[i],res); ans[q[x].id]=res; for (int i=q[x].l;i<=q[x].r;++i) cnt[a[i]]--; x++; }
ll res=0;int j=node; while (x<y) { int l=node+1,r=q[x].r; while (j<r) add(a[++j],res); ll bac=res; for (int k=q[x].l;k<=node;++k) add(a[k],res); ans[q[x].id]=res; for (int k=q[x].l;k<=node;++k) cnt[a[k]]--; res=bac; x++; } memset(cnt,0,sizeof cnt); }
for (int i=1;i<=m;++i) printf("%lld\n",ans[i]); return0; }
for (int i=h[x];~i;i=ne[i]) if (!f[e[i]][0]) dfs(e[i],x); la[x]=++top;euler[top]=x; }
intlca(int x,int y) { if (dep[x]<dep[y]) swap(x,y); for (int i=L;i>=0;--i) if (dep[f[x][i]]>=dep[y]) x=f[x][i]; if (x==y) return x; for (int i=L;i>=0;--i) if (f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i]; return f[x][0]; }
voidadd(int pos,int &res) { tim[pos]^=1; if (tim[pos]) { if (!cnt[a[pos]]) res++; cnt[a[pos]]++; } else { cnt[a[pos]]--; if (!cnt[a[pos]]) res--; } }
intmain() { memset(h,-1,sizeof h); cin>>n>>m; for (int i=1;i<=n;++i) scanf("%d",&a[i]); for (int i=1;i<=n;++i) nums.push_back(a[i]); sort(nums.begin(),nums.end()); nums.erase(unique(nums.begin(),nums.end()),nums.end()); for (int i=1;i<=n;++i) a[i]=lower_bound(nums.begin(),nums.end(),a[i])-nums.begin();
for (int i=1,a,b;i<n;++i) { scanf("%d %d",&a,&b); add_edge(a,b);add_edge(b,a); } dfs(1,1); len=sqrt(top);
for (int i=1,a,b;i<=m;++i) { scanf("%d %d",&a,&b); if (fi[a]>fi[b]) swap(a,b); int l=lca(a,b); if (l==a) q[i]=(Query){i,fi[a],fi[b]}; else q[i]=(Query){i,la[a],fi[b],l}; }
sort(q,q+m,cmp);
for (int k=1,i=1,j=0,res=0;k<=m;++k) { int l=q[k].l,r=q[k].r,id=q[k].id,p=q[k].L; while (i>l) add(euler[--i],res); while (i<l) add(euler[i++],res); while (j<r) add(euler[++j],res); while (j>r) add(euler[j--],res); if (p) add(p,res); ans[id]=res; if (p) add(p,res); }
for (int i=1;i<=m;++i) printf("%d\n",ans[i]); return0; }
boolcmp(const Query &a,const Query &b) { int i=get(a.l),j=get(b.l); if (i!=j) return i<j; return a.r<b.r; }
intget_count(int x) { int res=0; while (x) { res++; x-=lowbit(x); } return res; }
intmain() { scanf("%d %d %d",&n,&m,&k); for (int i=1;i<=n;++i) scanf("%d",a+i); for (int i=1;i<=m;++i) q[i].id=i; for (int i=1;i<=m;++i) scanf("%d %d",&q[i].l,&q[i].r); for (int i=0;i<(1<<14);++i) if (get_count(i)==k) nums.push_back(i); for (int i=1;i<=n;++i) { for (int j=0;j<nums.size();++j) g[nums[j]^a[i]]++; f[i]=g[a[i+1]]; }
len=sqrt(n); sort(q+1,q+m+1,cmp);
for (int i=1,L=1,R=0;i<=m;++i) { int l=q[i].l,r=q[i].r; ll &res=q[i].res;
if (R>r) rg[L-1].push_back((Range){i,r+1,R,1}); while (R>r) res-=f[--R]; if (R<r) rg[L-1].push_back((Range){i,R+1,r,-1}); while (R<r) res+=f[R++]; if (L>l) rg[R].push_back((Range){i,l,L-1,1}); while (L>l) res-=f[L-2]+!k,L--; if (L<l) rg[R].push_back((Range){i,L,l-1,-1}); while (L<l) res+=f[L-1]+!k,L++; }
memset(g,0,sizeof g); for (int i=1;i<=n;++i) { for (int j=0;j<nums.size();++j) g[nums[j]^a[i]]++; for (int j=0;j<rg[i].size();++j) { Range &tmp=rg[i][j]; for (int x=tmp.l;x<=tmp.r;++x) q[tmp.id].res+=g[a[x]]*tmp.t; } }
for (int i=2;i<=m;++i) q[i].res+=q[i-1].res; for (int i=1;i<=m;++i) ans[q[i].id]=q[i].res;
for (int i=1;i<=m;++i) printf("%lld\n",ans[i]); return0; }