【luogu P1494 [国家集训队]小Z的袜子】 题解
题目链接:https://www.luogu.org/problemnew/show/P1494
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
const int maxn = 50000+10;
inline long long read()
{
long long k=0;
char c;
c=getchar();
while(!isdigit(c))c=getchar();
while(isdigit(c)){k=(k<<3)+(k<<1)+c-'0';c=getchar();}
return k;
}
long long n, m, bl, answer, curL = 1, curR = 0, cnt[maxn], a[maxn], gg;
struct query{
long long p, l, r;
}e[maxn];
struct answers{
long long x, y;
}ans[maxn], now;
long long gcd(long long x, long long y)
{
if(x%y == 0) return y;
else return gcd(y,x%y);
}
bool cmp(query a, query b)
{
return (a.l/bl) == (b.l/bl) ? a.r < b.r : a.l < b.l;
}
void add(long long pos)
{
cnt[a[pos]]++;
if(cnt[a[pos]] > 1)
now.x = now.x + cnt[a[pos]]*(cnt[a[pos]]-1)-(cnt[a[pos]]-1)*(cnt[a[pos]]-2);
}
void remove(long long pos)
{
cnt[a[pos]]--;
if(cnt[a[pos]] > 0)
now.x = now.x + cnt[a[pos]]*(cnt[a[pos]]-1)-(cnt[a[pos]]+1)*cnt[a[pos]];
}
int main()
{
n = read(); m = read();
bl = sqrt(n);
for(long long i = 1; i <= n; i++)
a[i] = read();
for(long long i = 1; i <= m; i++)
{
e[i].l = read(); e[i].r = read(); e[i].p = i;
}
sort(e+1,e+1+m,cmp);
for(long long i = 1; i <= m; i++)
{
long long L = e[i].l, R = e[i].r;
while(curL < L)
remove(curL++);
while(curL > L)
add(--curL);
while(curR < R)
add(++curR);
while(curR > R)
remove(curR--);
now.y = (e[i].r-e[i].l+1)*(e[i].r-e[i].l);
if (!now.x)
{
now.x=0;
now.y=1;
}
gg = gcd(now.x, now.y);
ans[e[i].p].x = now.x/gg;
ans[e[i].p].y = now.y/gg;
}
for(long long i = 1; i <= m; i++)
printf("%lld/%lld\n",ans[i].x,ans[i].y);
return 0;
}
【luogu P1494 [国家集训队]小Z的袜子】 题解的更多相关文章
- luogu P1494 [国家集训队]小Z的袜子 ( 普 通 )
题目: 链接:https://www.luogu.org/problemnew/show/P1494 题意:一些袜子排成一排,每个袜子有固定的颜色. ...
- Luogu P1494 [国家集训队]小Z的袜子
比较简单的莫队题,主要是为了熟练板子. 先考虑固定区间时我们怎么计算,假设区间\([l,r]\)内颜色为\(i\)的袜子有\(cnt_i\)只,那么对于颜色\(i\)来说,凑齐一双的情况个数为: \( ...
- P1494 [国家集训队]小Z的袜子
题目 P1494 [国家集训队]小Z的袜子 解析 在区间\([l,r]\)内, 任选两只袜子,有 \[r-l+1\choose2\] \[=\frac{(r-l+1)!}{2!(r-l-1)!}\] ...
- P1494 [国家集训队]小Z的袜子/莫队学习笔记(误
P1494 [国家集训队]小Z的袜子 题目描述 作为一个生活散漫的人,小\(Z\)每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小\(Z\)再也无法忍受这恼人的找袜子过程,于是他 ...
- P1494 [国家集训队]小Z的袜子(luogu)
P1494 小Z的袜子 终于了解了莫队算法(更专业的名称Square Root Decomposition of Queries) 莫队算法: 一般来说解决静态(实际上也有修改的但复杂度更高)的离线( ...
- 洛谷 P1494 [国家集训队] 小Z的袜子
题目概述: 小Z把N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两只袜子是不是完整的一双,甚至不在意两只袜子是否一左一右,他却很在意袜子的颜色,毕竟穿两只不同色的袜子会很尴尬. 你的任务 ...
- BZOJ2038:[2009国家集训队]小Z的袜子——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找 ...
- P1494 [国家集训队]小Z的袜子(莫队)
题目链接:https://www.luogu.org/problemnew/show/P1494 题目大意:中文题目 具体思路:计算概率的时候,每一次是区间的移动,每一次移动,记得先将原来的记录的影响 ...
- 洛谷 P1494 [国家集训队]小Z的袜子(莫队)
题目链接:https://www.luogu.com.cn/problem/P1494 一道很经典的莫队模板题,然而每道莫队题的大体轮廓都差不多. 首先莫队是一种基于分块的算法,它的显著特点就是: 能 ...
随机推荐
- c#输入方法名来调用方法(反射)
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...
- python制作 whl 源文件,并制作本地pip源
制作whl 1.创建用于存放wheel文件目录 mkdir wheels 2.安装wheel库 pip install wheel 3.进入wheels目录 cd wheels 4.使用pip wh ...
- 九度oj题目1181:遍历链表
题目1181:遍历链表 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2600 解决:1125 题目描述: 建立一个升序链表并遍历输出. 输入: 输入的每个案例中第一行包括1个整数:n(1 ...
- angular 首屏优化
前一段时间把公司的一个angular项目做了一次大的优化,记录一下过程. 起因: 起因是用户反映网站加载时间过长,从loading画面显示到页面可响应要13s,对于一般的页面恐怕没有用户愿意等待这么久 ...
- HRBUST 1161——Leyni——————【线段树单点更新,区间查询】
Leyni Time Limit: 3000 MS Memory Limit: 65536 KB 64-bit integer IO format: %lld , %llu Java class na ...
- Javascript模块化编程(三)require.js的用法及功能介绍
这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战.我采用的是一个非常流行的库require.js感兴趣的朋友可以了解下啊 我采用的是一个非常流行的 ...
- C# 获取电脑硬盘剩余空间
获取本地硬盘的所有剩余空间: 主要应用到System.IO类库的:Driveinfo.Directory,将model转换成json需要用到Newtonsoft.Json.JsonConvert.Se ...
- 键盘按键keyCode大全,js页面快捷键
字母和数字键的键码值(keyCode) 按键 键码 按键 键码 按键 键码 按键 键码 A 65 J 74 S 83 1 49 B 66 K 75 T 84 2 50 C 67 L 76 U 85 3 ...
- Vector 、ArrayList、LinkedList比较
这三者都可以随机访问,也就是支持通过索引查找数据. 都是有序(可以实现元素怎么进怎么出) Vector和ArrayList比较 相同之处 1 它们都是List 它们都继承于AbstractList,并 ...
- 30 Excellent WordPress Video Tutorials
http://sixrevisions.com/wordpress/30-excellent-wordpress-video-tutorials/ WordPress是一种使用PHP语言开发的博客平台 ...