HYSBZ - 2038 - 小Z的袜子(hose)(莫队模板题)

题目链接:https://vjudge.net/contest/361581#problem/A

题目大意:问你一个区间内选两个数,有多大的概率选到两个同样的数

  算概率的方法很简单,设区间内相同的数的数量分别为n1, n2....,那么概率就是(C(n1,2) + C(n2,2) + .....) / C(l-r+1,2)然后约分就是了那么问题转化为求(C(n1,2) + C(n2,2) + .....),我们观察发现C(2,2) = 1, C(3,2) = 3, C(4, 2) = 6,那么,我们大胆的推测(瞎猜)n每增加1, 其组合数就增加n-1。之后再套莫队就能轻松做出来了!

#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 1e5+10;
int arr[maxn], pos[maxn], cnt[maxn]; ll res;
struct Q {
    int l, r, id; ll ans1, ans2;
} q[maxn];
inline void add(int p) {
    res += cnt[arr[p]]++; 
}
inline void sub(int p) {
    res -= --cnt[arr[p]];
}
bool cmp1(Q x, Q y) {
    return pos[x.l]==pos[y.l] ? x.r < y.r : pos[x.l] < pos[y.l];
}
bool cmp2(Q x, Q y) {
    return x.id < y.id;
}
int main(void) {
    int n, m;
    scanf("%d%d", &n, &m);
    int siz = sqrt(n)+eps;
    for (int i = 1; i<=n; ++i) {
        scanf("%d", &arr[i]);
        pos[i] = i/siz;
    }
    for (int i = 0; i<m; ++i) {
        scanf("%d%d", &q[i].l, &q[i].r);
        q[i].id = i;
    }
    sort(q, q+m, cmp1);
    int l = 1, r = 0;
    for (int i = 0; i<m; ++i) {
        while(q[i].l < l) add(--l);
        while(q[i].r > r) add(++r);
        while(q[i].l > l) sub(l++);
        while(q[i].r < r) sub(r--);
        ll t = q[i].r - q[i].l;
        q[i].ans1 = res/__gcd(res, (t+1)*t/2);
        q[i].ans2 = t*(t+1)/2/__gcd(res, (t+1)*t/2);
        if (q[i].ans1 == q[i].ans2) q[i].ans1 = q[i].ans2 = 1; 
        //如果上下两个数相等会得到0/0,那么结果就是1/1
    }
    sort(q, q+m, cmp2);
    for (int i = 0; i<m; ++i)
        printf("%lld/%lld\n", q[i].ans1, q[i].ans2);
    return 0;
}

 

posted @ 2020-03-11 11:32  shuitiangong  阅读(115)  评论(0编辑  收藏  举报