蒟蒻刚学莫队求助,悬关

P2709 小B的询问

本人代码是对的你看看 ``` #include<cstdio> #include<cmath> #include<algorithm> #define int long long using namespace std; const int maxn=5e4+4; int n,m,k; int a[maxn],b[maxn]; int cnt[maxn],ans[maxn]; struct node { int l,r,id,pos; }q[maxn]; bool cmp(node x,node y) { if(x.pos==y.pos) return x.r<y.r; return x.pos<y.pos; } signed main() { scanf("%lld%lld%lld",&n,&m,&k); int block=sqrt(n); for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); b[i]=(i-1)/block+1; } for(int i=1;i<=m;i++) { scanf("%lld%lld",&q[i].l,&q[i].r); q[i].id=i; q[i].pos=b[q[i].l]; } sort(q+1,q+m+1,cmp); int l=1,r=0; int tmp=0; for(int i=1;i<=m;i++) { while(l>q[i].l) l--,cnt[a[l]]++,tmp+=(2*cnt[a[l]]-1); while(r<q[i].r) r++,cnt[a[r]]++,tmp+=(2*cnt[a[r]]-1); while(l<q[i].l) cnt[a[l]]--,tmp-=(2*cnt[a[l]]+1),l++; while(r>q[i].r) cnt[a[r]]--,tmp-=(2*cnt[a[r]]+1),r--; ans[q[i].id]=tmp; } for(int i=1;i<=m;i++) printf("%lld\n",ans[i]); return 0; } ```
by MilchstraBe @ 2023-08-28 19:39:32


@[MilchstraBe](/user/1060433) 谢谢大佬,已关注!
by 017_007 @ 2023-08-28 19:47:06


不用谢
by MilchstraBe @ 2023-08-28 19:47:41


# 此贴结 问题出在pos的赋值上,应该是 q[i].pos=(q[i].l-1)/t+1; 而我写成了 p[i].pos=(i-1)/t+1; 也感谢二楼大佬提供的正确代码,我才看出了我的代码的问题。
by 017_007 @ 2023-08-28 19:49:24


|