【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 一道很经典的莫队模板题,然而每道莫队题的大体轮廓都差不多. 首先莫队是一种基于分块的算法,它的显著特点就是: 能 ...
随机推荐
- TOJ 2939 解救小Q
描述 小Q被邪恶的大魔王困在了迷宫里,love8909决定去解救她.迷宫里面有一些陷阱,一旦走到陷阱里,就会被困身亡:(,迷宫里还有一些古老的传送阵,一旦走到传送阵上,会强制被传送到传送阵的另一头. ...
- Flask 编写http接口api及接口自动化测试
片言 此文中代码都是笔者工作中源码,所以不会很完整,主要摘常见场景的api片段用以举例说明 另:此文主要针对自动化测试人员,尤其有python基础阅读更佳. 笔者使用 python3.6 + post ...
- css三角块
html: <div class="angle"></div> css: .angle{ width: 0px; height: 0px; border-b ...
- Spring mvc框架下使用kaptcha生成验证码
1.下载jar包并导入. kaptcha-2.3.2.jar 2.spring 配置文件 applicationContext.xml. <bean id="captchaProduc ...
- hibernate注解JPA
1.JPA与hibernate 什么是JPA ? java persistence api :java持久化api,同一的ORM规范,是由sun公司指定的规范接口,hibernate实现了JPA规范. ...
- vue分页
1.依赖文件 <link href="/css/index.css" rel="stylesheet" type="text/css" ...
- Introduction of Servlet Filter(了解Servlet之Filter)
API文档中介绍了public Interface Filter(公共接口过滤器) Servlet API文档中是这样介绍的: ‘A filter is an object that performs ...
- sublime常用设置
原文地址 https://segmentfault.com/a/1190000002596724 前言 Sublime Text3 在文中简称为ST. ST是个不错的编辑器,我用了有段时间了,所以我觉 ...
- 【数据库】5.0 MySQL入门学习(五)——MySQL源码了解及MySQL初始化设置
1.0 MySQL源码目录主要包括:客户端代码.服务端代码.测试工具.其他库文件.当然,看懂源代码得有一定的C语言基础. BUILD:各种平台的编译脚本,可以用来制作各平台的二进制版本 client: ...
- 在Android Studio上进行OpenCV 3.1开发环境配置
开发环境: Windows 7 x 64 家庭版 Android Studio 1.5.1(Gradle版本2.8) JDK 1.8.0 Android 6.0(API 23) OpenCV 3.1. ...