【luogu 2709 / BZOJ 3781】小B的询问
生活随笔
收集整理的這篇文章主要介紹了
【luogu 2709 / BZOJ 3781】小B的询问
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
【原題題面】傳送門
【題解大意】
莫隊(duì)。這題未免太果。
【code】
#include<bits/stdc++.h> using namespace std; #define File "" #define ll long long #define ull unsigned long long inline void file(){freopen(File".in","r",stdin);freopen(File".out","w",stdout); } inline int read(){int x=0,f=1; char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0'; ch=getchar();}return x*f; } const int mxn = 5e4+5; int n,m,k; int Block; int a[mxn],ans[mxn],cnt[mxn]; struct Q{int l,r,id; }q[mxn]; inline bool cmp(Q x,Q y){if((x.l/Block) ^ (y.l/Block)) return x.l < y.l;else if((x.l/Block)&1) return x.r < y.r;else return x.r > y.r; } ll answer(0); inline void mov(int x,int d){answer -= cnt[a[x]]*cnt[a[x]];cnt[a[x]] += d;answer += cnt[a[x]]*cnt[a[x]]; } int main(){ // file();n = read(),m = read(),k = read();Block = sqrt(n);for(int i = 1;i <= n; ++i) a[i] = read();for(int i = 1;i <= m; ++i) q[i].l = read(),q[i].r = read(),q[i].id = i;sort(q+1,q+1+m,cmp);int l(1),r(0);for(int i = 1;i <= m; ++i){int ql = q[i].l,qr = q[i].r;while(ql < l) mov(l-1,1),l--;while(ql > l) mov(l,-1),l++;while(qr < r) mov(r,-1),r--;while(qr > r) mov(r+1,1),r++;ans[q[i].id] = answer;}for(int i = 1;i <= m; ++i) printf("%d\n",ans[i]);return 0; } /* 6 4 3 1 3 2 1 1 3 1 4 2 6 3 5 5 6 */ View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/ve-2021/p/10900728.html
總結(jié)
以上是生活随笔為你收集整理的【luogu 2709 / BZOJ 3781】小B的询问的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .git文件解读
- 下一篇: 标准 I/O (带缓冲)