acwing 251 小Z的袜子 [莫队]

131 篇文章 0 订阅

题目

作为一个生活散漫的人,小 ZZ 每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿。

终于有一天,小 ZZ 再也无法忍受这恼人的找袜子过程,于是他决定听天由命。

具体来说,小 ZZ 把这 NN 只袜子从 11 到 NN 编号,然后从编号 LL 到 RR 的袜子中随机选出两只来穿。

尽管小 ZZ 并不在意两只袜子是不是完整的一双,甚至不在意两只袜子是否一左一右,他却很在意袜子的颜色,毕竟穿两只不同色的袜子会很尴尬。

你的任务便是告诉小 ZZ,他有多大的概率抽到两只颜色相同的袜子。

当然,小 ZZ 希望这个概率尽量高,所以他可能会询问多个 (L,R)(L,R) 以方便自己选择。

输入格式

第一行包含两个正整数 NN 和 MM,NN 为袜子的数量,MM 为小 ZZ 所提的询问的数量。

接下来一行包含 NN 个正整数 CiCi,其中 CiCi 表示第 ii 只袜子的颜色,相同的颜色用相同的数字表示。

再接下来 MM 行,每行两个正整数 L,RL,R 表示一个询问。

输出格式

包含 MM 行,对于每个询问在一行中输出分数 A/BA/B 表示从该询问的区间 [L,R][L,R] 中随机抽出两只袜子颜色相同的概率。

若该概率为 00 则输出 0/10/1,否则输出的 A/BA/B 必须为最简分数。

数据范围

N,M≤50000N,M≤50000,
1≤L<R≤N1≤L<R≤N,
Ci≤NCi≤N

输入样例:
6 4
1 2 3 3 3 2
2 6
1 3
3 5
1 6
输出样例:
2/5
0/1
1/1
4/15

解释与代码

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <stack>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb          push_back
#define ppb         pop_back
#define lbnd        lower_bound
#define ubnd        upper_bound
#define endl        '\n'
#define mll         map<ll,ll>
#define msl         map<string,ll>
#define mls         map<ll, string>
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define repr(i,a,b) for(ll i=b-1;i>=a;i--)
#define trav(a, x)  for(auto& a : x)
#define pll         pair<ll,ll>
#define vl          vector<ll>
#define vll         vector<pair<ll, ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define DEBUG       cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x)  trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x)  cerr << #x << " is " << x << endl;
#define ini(a)      memset(a,0,sizeof(a))
#define ini2(a,b)   memset(a,b,sizeof(a))
//#define rep(i,a,b)  for(int i=a;i<=b;i++)
#define case        ll T;read(T);for(ll Q=1;Q<=T;Q++)
#define lowbit(x)   x&(-x)
#define pr          printf
#define sc          scanf
//#define _           0
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \
    (void)(cout << "L" << __LINE__ \
    << ": " << #x << " = " << (x) << '\n')
#define TIE \
    cin.tie(0);cout.tie(0);\
    ios::sync_with_stdio(false);
//#define long long int

//using namespace __gnu_pbds;

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI    = acos(-1.0);
const double eps   = 1e-6;
const int    INF   = 0x3f3f3f3f;
const ll     LLINF = 0x3f3f3f3f3f3f3f3f;

template <typename T>
void read(T &x) {
    x = 0;
    int f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch ^ 48);
        ch = getchar();
    }
    x *= f;
    return;
}

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

const ll     LN    = 5;
const int    maxn  = 50009;
const int    N     = 1050;

ll arr[maxn], pos[maxn], L[maxn], R[maxn], num[maxn];
struct Node {
	ll l, r, id, x, y;
	bool operator < (const Node &t)const {
        if (pos[l] == pos[t.l]) return r<t.r;
        return pos[l]<pos[t.l];
    }
}q[maxn];

ll gcd (ll a, ll b) {
	return b?gcd(b, a%b):a;
}

bool cmp (Node a, Node b) {
	return a.id < b.id;
}

void solve() {
	ll n, m, t;
	read(n), read(m);
	//分块操作
	t = sqrt(n);
	for (int i=1; i<=t; i++) {
		L[i] = (i-1)*t + 1;
		R[i] = i*t;
	}
	if (R[t] < n) t++, L[t] = R[t-1] + 1, R[t] = n;
	for (int i=1; i<=t; i++) {
		for (int j=L[i]; j<=R[i]; j++) {
			pos[j] = i;
		}
	}
	
	
	for (int i=1; i<=n; i++) read(arr[i]);
	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 LL = 1, RR = 0;
	ll fz = 0, fm = 0;//分子,分母 
	
	for (int i=1; i<=m; i++) {
		if (q[i].l == q[i].r) {
			q[i].x = 0, q[i].y = 1;
			continue;
		}
		//下面四个循环用来调整LL和RR
		while (RR < q[i].r) {
			RR++;
			//原本公式是(num[arr[i]] * (num[arr[i] - 1)) / 2, 因为小Z要在num[arr[i]]里面挑两个,所以就是组合数学里的 $C_n^2$
			//为什么这里没有除以2,因为/2最后还是要约掉,可以不写
			fz -= (num[arr[RR]] * (num[arr[RR]]-1));
			num[arr[RR]]++;
			fz += (num[arr[RR]] * (num[arr[RR]]-1));
		}
		while (RR > q[i].r) {
            fz -= num[arr[RR]] * (num[arr[RR]]-1);
            num[arr[RR]]--;
            fz += num[arr[RR]] * (num[arr[RR]]-1);
            RR--;
        }
        while (LL < q[i].l) {
            fz -= num[arr[LL]] * (num[arr[LL]]-1);
            num[arr[LL]]--;
            fz += num[arr[LL]] * (num[arr[LL]]-1);
            LL++;
        }
        while (LL > q[i].l) {
            LL--;
            fz -= num[arr[LL]] * (num[arr[LL]]-1);
            num[arr[LL]]++;
            fz += num[arr[LL]] * (num[arr[LL]]-1);
        }
        fm = (RR-LL+1)*(RR-LL);
        ll gcdnum = gcd(fz, fm);//除以最大公约数得到最简
        q[i].x = fz/gcdnum;
        q[i].y = fm/gcdnum;
	}
	sort(q+1, q+1+m, cmp);//按照id排序
	for (int i=1; i<=m; i++) {
		write(q[i].x), putchar('/'), write(q[i].y), putchar('\n');
	}
}

int main()
{
//	TIE;
//    #ifndef ONLINE_JUDGE
//    freopen ("input.txt","r",stdin);
//    #else
//    #endif
	solve();
//    case{solve();}
//    case{cout<<"Case "<<Q<<":"<<endl;solve();}
//	return ~~(0^_^0);
}



参考:https://www.cnblogs.com/l999q/p/11371546.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值