蒟蒻 60pts wa求助
查看原帖
蒟蒻 60pts wa求助
393674
jixiang楼主2021/10/27 22:09
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<iostream>
const int N = 5e6 + 9;
typedef long long ll;
using namespace std;
typedef pair <ll, ll>pll;
int n, m, k;
ll cnt[N], w[N];
pll ans[N];
int len;

struct node
{
    int id, l, r;
} q[N];

ll gcd(ll a, ll b)
{
    if(b == 0)return a;
    else return gcd (b, a % b);
}

inline int g(int i)
{
    return i / len;
}

bool operator  < (const node &a, const node &b)
{
    if (g(a.l) == g(b.l))
    {
        if(g(a.l) % 2 == 1)return  a.r < b.r;
        else return a.r > b.r;
    }
    else return g(a.l) < g(b.l);
}

inline void del(int x, ll &res)
{
    res -= cnt[x] * (cnt[x] - 1);
    cnt[x]--;
    res += cnt[x] * (cnt[x] - 1);
}

inline void add(int x, ll &res)
{
    res -= cnt[x] * (cnt[x] - 1);
    cnt[x]++;
    res += cnt[x] * (cnt[x] - 1);
}

template<typename T> void read(T &x)
{
    x = 0;
    bool f = 1 ;
    char ch = getchar();
    while(ch > '9' || ch < '0'){if(ch == '-')f = 0; ch = getchar();}
    while (ch <= '9' && ch >= '0'){x = (x << 1) + (x << 3) + ch - '0'; ch = getchar();}
    if(!f)x *= -1;
}

inline void wri(ll x)
{
    if(x < 0)putchar('-'), x = -x;
    if(x > 9)wri(x / 10);
    putchar(x % 10 + '0');
    return ;
}

signed main(void)
{
    read(n);
    read(m);
    for(int i = 1 ; i <= n ; i++ )read(w[i]);
    len = sqrt(n);
    for (int i = 1 ; i <= m ; i++ )read(q[i].l), read(q[i].r), q[i].id = i;
    sort(q + 1, q + 1 + m);

    ll res = 0;

    for (int k = 1, i = 1, j = 0; k <= m ; k++ )
    {
        int l = q[k].l, r = q[k].r;
        if(l == r)
        {
            ans[q[k].id] = {0, 1};
            continue;
        }
        while (i < l)del(w[i++], res);
        while (i > l)add(w[--i], res);
        while (j > r)del(w[j--], res);
        while (j < r)add(w[++j], res);

        if(res == 0) ans[q[k].id] = {res, 1};
        else ans[q[k].id] = {res, (r - l + 1)*(r - l)};
    }

    for(int i = 1; i <= m; i++)
    {
        ll gc = gcd(ans[i].second, ans[i].first);
        wri(ans[i].first / gc);
        putchar('/');
        wri(ans[i].second / gc);
        puts("");
    }
    return 0;
}


2021/10/27 22:09
加载中...