BZOJ 2038 小Z的袜子

莫队

基本上没什么变化,推一下公式就可以了

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 50005;
int n, m, t, a[N], freq[N];
ll p[N], q[N], ans;
struct Query{
    int l, r, id, block;
    bool operator < (const Query &rhs) const {
        return (block ^ rhs.block) ? l < rhs.l : (block & 1) ? r < rhs.r : r > rhs.r;
    }
}query[N];
 
void add(int k){
    freq[a[k]] ++;
    ans += freq[a[k]] - 1;
}
 
void remove(int k){
    freq[a[k]] --;
    ans -= freq[a[k]];
}
 
int main(){
 
    //freopen("data.txt", "r", stdin);
 
    n = read(), m = read();
    t = (int)sqrt(n);
    for(int i = 1; i <= n; i ++) a[i] = read();
    for(int i = 1; i <= m; i ++){
        query[i].l = read(), query[i].r = read();
        query[i].id = i, query[i].block = (query[i].l - 1) / t + 1;
    }
    sort(query + 1, query + m + 1);
    int l = 1, r = 0;
    for(int i = 1; i <= m; i ++){
        int curL = query[i].l, curR = query[i].r;
        while(l < curL) remove(l ++);
        while(r < curR) add(++ r);
        while(l > curL) add(-- l);
        while(r > curR) remove(r --);
        ll tmp = 1LL * (query[i].r - query[i].l + 1) * (query[i].r - query[i].l) / 2;
        if(!ans){
            p[query[i].id] = 0;
            continue;
        }
        ll f = gcd(ans, tmp), y = ans;
        y /= f, tmp /= f;
        p[query[i].id] = y, q[query[i].id] = tmp;
    }
    for(int i = 1; i <= m; i ++){
        if(!p[i]) printf("%lld/1\n", p[i]);
        else printf("%lld/%lld\n", p[i], q[i]);
    }
    return 0;
}
posted @ 2019-05-13 22:58  清楚少女ひなこ  阅读(109)  评论(0编辑  收藏  举报