会了

//2709 in fact 

#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

ll n,m,k,a[(int)5e4+9],fk,belong[(int)5e4+9],ans[(int)5e4+9],l=1,r,sum,cnt[(int)5e4+9];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m>>k;
    for(int i=1;i<=n;++i)cin>>a[i];

    fk=ceil(n/sqrt(m));
    for(int i=1;i<=n;++i){
        belong[i]=(i-1)/fk;
    }

    vector<array<int,3>> q;
    for(int i=1,x,y;i<=m;++i){
        cin>>x>>y;
        q.push_back({x,y,i});
    }
    sort(q.begin(),q.end(),[](const array<int,3>& aa,const array<int,3>& bb){return belong[aa[0]]==belong[bb[0]]?aa[1]<bb[1]:belong[aa[0]]<belong[bb[0]];});

    for(auto &&[aa,bb,cc]:q){
        while(l<aa)sum-=2ll*cnt[a[l]]-1ll,cnt[a[l]]-=1,l+=1;
        while(l>aa)l-=1,sum+=2ll*cnt[a[l]]+1ll,cnt[a[l]]+=1;
        while(r<bb)r+=1,sum+=2ll*cnt[a[r]]+1ll,cnt[a[r]]+=1;
        while(r>bb)sum-=2ll*cnt[a[r]]-1ll,cnt[a[r]]-=1,r-=1;
        ans[cc]=sum;
    }
    for(int i=1;i<=m;++i)cout<<ans[i]<<"\n";
    return 0;
}
此文章已被阅读次数:正在加载...更新于