题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2038

题目:

题意:中文题意,大家都懂。

思路:莫队入门题。不过由于要去概率,所以我们假设询问区间内有k中物品,每种物品我们假设它的数量为pi。那么我们可以进行下面一系列的公式推导:

所以我们用莫队维护该区间内某颜色的平方和,对于最简分式我们用一个gcd即可求出。

代码实现如下:

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef unsigned long long ull; #define bug printf("*********\n");
#define FIN freopen("in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 5e4 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f; int n, m, block;
ll sum;
int a[maxn], cnt[maxn]; struct node {
int l, r, id;
ll ans1, ans2;
bool operator < (const node& x) const {
return (l - ) / block == (x.l - ) / block ? r < x.r : (l - ) / block < (x.l - ) / block;
}
}ask[maxn]; void update (int p, int val) {
sum -= cnt[p] * cnt[p];
cnt[p] += val;
sum += cnt[p] * cnt[p];
} ll gcd(ll a, ll b) {
return b == ? a : gcd(b, a % b);
} int main() {
//FIN;
while(~scanf("%d%d", &n, &m)) {
block = sqrt(n);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
}
for(int i = ; i <= m; i++) {
scanf("%d%d", &ask[i].l, &ask[i].r);
ask[i].id = i;
}
sort(ask + , ask + m + );
for(int i = , l = , r = ; i <= m; i++) {
for(; r < ask[i].r; r++) update(a[r + ], );
for(; r > ask[i].r; r--) update(a[r], -);
for(; l < ask[i].l; l++) update(a[l], -);
for(; l > ask[i].l; l--) update(a[l - ], );
if(ask[i].l == ask[i].r) {
ask[ask[i].id].ans1 = , ask[ask[i].id].ans2 = ;
continue;
}
ll k1 = sum - (ask[i].r - ask[i].l + );
ll k2 = (long long) (ask[i].r - ask[i].l + ) * (ask[i].r - ask[i].l);
ll gg = gcd(k1, k2);
ask[ask[i].id].ans1 = k1 / gg;
ask[ask[i].id].ans2 = k2 / gg;
}
for(int i = ; i <= m; i++) {
if(ask[i].ans1 == ) {
printf("0/1\n");
} else {
printf("%lld/%lld\n", ask[i].ans1, ask[i].ans2);
}
}
}
return ;
}

[2009国家集训队]小Z的袜子(hose)(BZOJ2038+莫队入门题)的更多相关文章

  1. BZOJ_2038_[2009国家集训队]小Z的袜子(hose)_莫队

    BZOJ_2038_[2009国家集训队]小Z的袜子(hose)_莫队 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无 ...

  2. BZOJ 2038: [2009国家集训队]小Z的袜子(hose)【莫队算法裸题&&学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 9894  Solved: 4561[Subm ...

  3. 2038: [2009国家集训队]小Z的袜子(hose) (莫队算法)

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=2038 专题练习: http://acm.hust.edu.cn/vjudge/conte ...

  4. BZOJ2038 2009国家集训队 小Z的袜子(hose) 【莫队】

    BZOJ2038 2009国家集训队 小Z的袜子(hose) Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼 ...

  5. [bzoj2038][2009国家集训队]小Z的袜子(hose)_莫队

    小Z的袜子 hose 2009-国家集训队 bzoj-2038 题目大意:给定一个n个袜子的序列,每个袜子有一个颜色.m次询问:每次询问一段区间中每种颜色袜子个数的平方和. 注释:$1\le n,m\ ...

  6. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) 【莫队算法】

    Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...

  7. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) 【莫队算法模版】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2038 题意概括: 有 N 只袜子(分别编号为1~N),有 M 次查询 (L, R)里面随机 ...

  8. BZOJ:2038: [2009国家集训队]小Z的袜子(hose)(莫队算法模板)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2038 解题心得: 第一次接触莫队算法,很神奇,很巧妙.莫队算法主要就是用来解决多次询问时 ...

  9. BZOJ2038 [2009国家集训队]小Z的袜子(hose)(莫队算法)

    神奇的莫队算法,用来解决可离线无修改的区间查询问题: 首先对原序列进行分块,√n块每块√n个: 然后对所有查询的区间[l,r]进行排序,首先按l所在的块序号升序排序,如果一样就按r升序排序: 最后就按 ...

  10. bzoj 2038 [2009国家集训队]小Z的袜子(hose)(莫队算法)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2038 [题意] 给定一个有颜色的序列,回答若干个询问:区间内任选两个颜色相同的概率. ...

随机推荐

  1. 分布式系统理论-terms

    Distributed programming is the art of solving the same problem that you can solve on a single comput ...

  2. linux上使用J-Link调试S3C2440裸机代码

    linux上使用J-Link调试S3C2440裸机代码 工具: segger的jlink仿真器 segger的jlink for linux 交叉编译工具链里面的arm-xx-linux-xx-gdb ...

  3. 织梦dede:list标签在列表页同一文章显示两次的解决方法

    在列表页用{dede:list}标签调用文章的时候出现了同一篇文章显示两次的问题,经过一天的奋战最后终于解决了,下面CMS集中营站长简单说下我的解决过程来供各位学友参考:1.怀疑是不是每次添加都会自动 ...

  4. Python爬虫requests判断请求超时并重新发送请求

     下面是简单的一个重复请求过程,更高级更简单的请移步本博客: https://www.cnblogs.com/fanjp666888/p/9796943.html  在爬虫的执行当中,总会遇到请求连接 ...

  5. RT-thread内核之事件

    一.事件控制块:在include/rtdef.h中 #ifdef RT_USING_EVENT /** * flag defintions in event */ #define RT_EVENT_F ...

  6. Android ListView 中加入CheckBox/RadioButton 选择状态保持、全选、反选实现

    最近在一个项目中,需要在ListView的item中加入CheckBox,但是遇到的一个问题是上下滑动的时候如果有选择了的CheckBox,就会出现选择项错误的问题,下面将个人的解决方法总结如下;先说 ...

  7. Android <Android应用开发实战> 资源类型<一>

    1.字符串资源>>1.普通字符串>>2.字符串数组 <resources> <string-array name="planets_array&qu ...

  8. CF498D:Traffic Jams in the Land——题解

    https://vjudge.net/problem/CodeForces-498D http://codeforces.com/problemset/problem/498/D 题面描述: 一些国家 ...

  9. [LNOI] 相逢是问候 || 扩展欧拉函数+线段树

    原题为2017六省联考的D1T3 给出一个序列,m次操作,模数p和参数c 操作分为两种: 1.将[l,r]区间内的每个数x变为\(c^x\) 2.求[l,r]区间内数的和%p 首先,我们要了解一些数论 ...

  10. 关于EK Dicnic

    笔记--最大流  $EK$ $Dinic$ $EK$: 运用反向边可以给当前图一次反悔的机会,就是其实现在的增广路并不是最优的,然后就$bfs$找增广路即可 $Dicnic$: 我们发现其实每一次先$ ...