BZOJ2038 小Z的袜子 莫队

BZOJ2038

 

题意:q(5000)次询问,问在区间中随意取两个值,这两个值恰好相同的概率是多少?分数表示;

感觉自己复述的题意极度抽象,还是原题意有趣(逃;

思路:设在L到R这个区间中,x这个值得个数为a个,y这个值的个数为b个,z这个值的个数为c个。

   那么答案即为 (a*(a-1)/2+b*(b-1)/2+c*(c-1)/2....)/((R-L+1)*(R-L)/2)

   化简得: (a^2+b^2+c^2+...x^2-(a+b+c+.....)) / ((R-L+1)*(R-L))

   显然其中(a+b+c+.....)就是区间的长度,每个值得个数总和。

   即: (a^2+b^2+c^2+...x^2-(R-L+1))/((R-L+1)*(R-L))

   每次sum记录a^2+b^2+c^2+...x^2,用莫队转移即可。

 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue

typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18

template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}
// #define _DEBUG;         //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------show time----------------------*/
                    const int B = 233;
                    #define bel(x) ((x-1)/B + 1)
                    int n,m;
                    ll sum = 0;
                    const int maxn = 50009;
                    struct node
                    {
                        ll le,ri;                        
                        int id;
                    }q[maxn];
                    struct res{
                        ll a,b;
                    }ans[maxn];
                    int cnt[maxn],col[maxn];
                    bool cmp(node a,node b){
                        if(bel(a.le) == bel(b.le)){
                            return a.ri < b.ri;
                        }
                        return bel(a.le) < bel(b.le);
                    }
                    void del(int x){
                        sum = sum - 1ll * cnt[x] * cnt[x];
                        cnt[x]--;
                        sum = sum + 1ll * cnt[x] * cnt[x];
                    }
                    void add(int x){
                        sum = sum - 1ll * cnt[x] * cnt[x];
                        cnt[x] ++;
                        sum = sum + 1ll * cnt[x] * cnt[x];
                    }
int main(){
                    scanf("%d%d", &n, &m);
                    for(int i=1; i<=n; i++) scanf("%d", &col[i]);
                    for(int i=1; i<=m; i++){
                        scanf("%lld%lld", &q[i].le, &q[i].ri);
                        q[i].id = i;
                    }

                    sort(q+1,q+1+m,cmp);
                    int pl = 1, pr = 0;
                    sum = 0;
                    for(int i=1; i<=m; i++){
                        while(pl < q[i].le) del(col[pl++]);
                        while(pl > q[i].le) add(col[--pl]);
                        while(pr < q[i].ri) add(col[++pr]);
                        while(pr > q[i].ri) del(col[pr--]);
                        if(q[i].le == q[i].ri){
                            ans[q[i].id].a = 0;
                            ans[q[i].id].b = 1;
                            continue;
                        }
                        ans[q[i].id].a = sum - (q[i].ri - q[i].le + 1);
                        ans[q[i].id].b = (q[i].ri - q[i].le + 1) * (q[i].ri - q[i].le);
                    }
                    for(int i=1; i<=m; i++){
                        ll tmp = __gcd(ans[i].a,ans[i].b);
                        if(tmp==0){
                            printf("%lld/%lld\n", ans[i].a, ans[i].b);
                        }
                        else printf("%lld/%lld\n", ans[i].a/tmp, ans[i].b/tmp);
                    }
        return 0;
}
BZOJ2038

 

posted @ 2018-08-05 22:00  ckxkexing  阅读(126)  评论(0编辑  收藏  举报