小B的詢問(小Z的襪子簡單版,莫隊)

小B的詢問

剛寫完小Z的襪子,然後刪了些代碼,反手把這題A了。。。

題意、思路見小Z的襪子

#include "bits/stdc++.h"
#define hhh printf("hhh\n")
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;
typedef long long ll;
typedef pair<int,int> pr;
inline int read() {int x=0;char c=getchar();while(c<'0'||c>'9')c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return x;}

const int maxn = 5e4+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
const double eps = 1e-7;

int n, m, k, len;
int c[maxn], cnt[maxn];
ll ans1[maxn], ans;

struct Q{
    int l, r, id;
    friend bool operator < (const Q &a, const Q &b) {
        if((a.l-1)/len==(b.l-1)/len) {
            if((a.l-1)/len%2) return a.r>b.r;
            else return a.r<b.r;
        }
        return a.l<b.l;
    }
}q[maxn];

inline void update(int p, int f) {
    ans-=1ll*cnt[c[p]]*cnt[c[p]];
    cnt[c[p]]+=f;
    ans+=1ll*cnt[c[p]]*cnt[c[p]];
}

int main() {
    //ios::sync_with_stdio(false); cin.tie(0);
    n=read(), m=read(), k=read(); len=sqrt(n);
    for(int i=1; i<=n; ++i) c[i]=read();
    for(int i=1; i<=m; ++i) q[i]=(Q){read(),read(),i};
    sort(q+1,q+1+m);
    int l=1, r=0;
    int cnt=0;
    for(int i=1; i<=m; ++i) {
        while(l<q[i].l) update(l++,-1);
        while(l>q[i].l) update(--l,1);
        while(r<q[i].r) update(++r,1);
        while(r>q[i].r) update(r--,-1);
        ans1[q[i].id]=ans;
    }
    for(int i=1; i<=m; ++i) printf("%lld\n", ans1[i]);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章