标签:main 表示 turn long style etc clu inf none
THUWC 考了莫队(这个应该可以说吧)
然而不会莫队,签到失败,所以找到了一道长得差不多的题写一写
为什么这么长时间都没有发现这道题(半恼
#include<bits/stdc++.h> #define LL long long using namespace std; inline int read() { int x = 0,f = 1;char ch = getchar(); for(;!isdigit(ch);ch = getchar())if(ch == ‘-‘) f = -f; for(;isdigit(ch);ch = getchar())x = 10 * x + ch - ‘0‘; return x * f; } const int maxn = 50010; int n,a[maxn]; int bl[maxn],tot,cntl[maxn],cntr[maxn]; LL ans[maxn]; struct Ques { int l,r,flag,pos; Ques(){} Ques(int _1,int _2,int _3,int _4): l(_1),r(_2),flag(_3),pos(_4){} bool operator < (const Ques &b)const{return (bl[l] == bl[b.l]) ? (r < b.r) : (bl[l] < bl[b.l]);} }qs[maxn << 2]; int main() { n = read(); for(int i=1;i<=n;i++)a[i] = read(); int BLSIZE = sqrt(n); for(int i=1;i<=n;i++)bl[i] = i / BLSIZE; int q = read(); for(int i=1;i<=q;i++) { int xl = read(),xr = read(),yl = read(),yr = read(); qs[++tot] = Ques(xr,yr,1,i); if(xl > 1)qs[++tot] = Ques(xl - 1,yr,-1,i); if(yl > 1)qs[++tot] = Ques(xr,yl - 1,-1,i); if(xl > 1 && xr > 1)qs[++tot] = Ques(xl - 1,yl - 1,1,i); } sort(qs + 1,qs + tot + 1); int l = 0,r = 0; LL now = 0; for(int i=1;i<=tot;i++) { while(l < qs[i].l){l++;now += cntr[a[l]];cntl[a[l]]++;} while(r < qs[i].r){r++;now += cntl[a[r]];cntr[a[r]]++;} while(l > qs[i].l){cntl[a[l]]--;now -= cntr[a[l]];l--;} while(r > qs[i].r){cntr[a[r]]--;now -= cntl[a[r]];r--;} ans[qs[i].pos] += qs[i].flag * now; } for(int i=1;i<=q;i++)cout << ans[i] << endl; }
标签:main 表示 turn long style etc clu inf none
原文地址:https://www.cnblogs.com/Kong-Ruo/p/10326348.html