简单莫队

题目:小Z的袜子

莫队:当年国家队队长莫涛大犇发明的算法orz

#include

using namespace std;

const int maxn = 50010;
const int64_t mod = 1e9 + 7;

int n, m;
int k;
int x[maxn];
int sum[maxn];
struct P
{
    int l, r;
    int num;
    int64_t ans1, ans2;
}que[maxn];

bool cmp1(P x,P y)
{
    return ((x.l/k l)
        {
            sum[x[l]]--;
            ans -= sum[x[l]];
            l++;
        }
        while(L < l)
        {
            l--;
            ans += sum[x[l]];
            sum[x[l]]++;
        }
        while(R > r)
        {
            r++;
            ans += sum[x[r]];
            sum[x[r]]++;
        }
        while(R < r)
        {
            sum[x[r]]--;
            ans -= sum[x[r]];
            r--;
        }
        que[i].ans1 = ans;
        que[i].ans2 = 1LL * (R - L + 1) * (R - L) / 2;
    }
    sort(que + 1, que + m + 1, cmp2);
    for(int i = 1; i <= m; i++)
    {
        int64_t tmp = __gcd(que[i].ans1, que[i].ans2);
        if(tmp == 0)
            cout << "0/1" << endl;
        else
            printf("%lld/%lld\n", que[i].ans1 / tmp, que[i].ans2 / tmp);
    }
    return 0;
}

你可能感兴趣的:(图论)