莫队

https://www.luogu.com.cn/problem/P1494

#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn=5e6+10;
int sz;
struct node{
    int l,r,id;
    bool operator<(const node&p)const{
        return l/sz!=p.l/sz?l<p.l:r<p.r;
    }
}q[maxn];
ll answer,curl=1,curr,a[maxn];
ll cnt[maxn];
inline void add(ll k){
    answer+=cnt[a[k]];
    ++cnt[a[k]];
}
inline void del(ll k){
    cnt[a[k]]=cnt[a[k]]-1;
    answer-=cnt[a[k]];
}
vector<pair<ll,ll>>ans(maxn);
inline ll gcd(ll a,ll b){
    if(!a)return b;
    return gcd(b%a,a);
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    sz=sqrt(n);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&q[i].l,&q[i].r);
        q[i].id=i;
    }
    sort(q+1,q+1+m);
    for(int i=1;i<=m;i++){
        ll l=q[i].l,r=q[i].r;
        while(curl<l) del(curl++);
        while(curl>l) add(--curl);
        while(curr<r) add(++curr);
        while(curr>r) del(curr--);
        ans[q[i].id]={answer,1ll*(r-l+1ll)*(r-l)/(ll)2};
    }
    for(int i=1;i<=m;i++){
        ll xx=ans[i].first,zz=ans[i].second;
        ll uu=gcd(xx,zz);
        if(!zz||!xx){
            puts("0/1");continue;
        }
        xx/=uu;zz/=uu;
        printf("%lld/%lld\n",xx,zz);
    }
    return 0;
}

 

posted @ 2021-04-28 22:19  Acception  阅读(78)  评论(0)    收藏  举报