洛谷P1494 [国家集训队]小Z的袜子
Code:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
# define REP(i,a,n) for(int i=a;i<=n;++i)
# define CLR(d,a)memset(d,a,sizeof(d)); using namespace std; void SetIO(string a){
string in=a+".in";
freopen(in.c_str(),"r",stdin);
} const int maxn=60000+5; int n,m,col[maxn]; struct Asks{
int l,r;
Asks(int l=0,int r=0):l(l),r(r){}
}asks[maxn]; void Read(){
scanf("%d%d",&n,&m); REP(i,1,n)
scanf("%d",&col[i]); REP(i,1,m){
int a,b;
scanf("%d%d",&a,&b);
asks[i]=Asks(a,b);
}
} int block; int belong[maxn], ranking[maxn]; int get_belong(int i){
return (i-1)/block+1;
} bool cmp(int i,int j){
if(belong[asks[i].l]==belong[asks[j].l])
return asks[i].r<asks[j].r;
else
return belong[asks[i].l]<belong[asks[j].l];
} void Build(){
block=sqrt(n); REP(i,1,n)
belong[i]=get_belong(i); REP(i,1,m)
ranking[i]=i; sort(ranking+1,ranking+1+m, cmp);
} long long square[maxn], ans[maxn]; long long cnt[maxn]; long long sum_square=0; void update(int t, int delta){
sum_square-=square[t];
cnt[t]+=delta;
square[t]=cnt[t]*cnt[t];
sum_square+=square[t];
} void Work(){ int l=asks[ranking[1]].l, r=asks[ranking[1]].r; REP(i,l,r)
++cnt[col[i]]; REP(i,1,n){
square[i]=cnt[i]*cnt[i];
sum_square+=square[i];
} REP(i,1,m){ ans[ranking[i]]=sum_square; if(i==m)break; int l2=asks[ranking[i+1]].l;
int r2=asks[ranking[i+1]].r; if(r<r2)
REP(i,r+1,r2) update(col[i],1);
else
for(int i=r;i>r2;--i) update(col[i],-1); if(l>l2)
for(int i=l-1;i>=l2;--i)update(col[i],1);
else
REP(i,l,l2-1)update(col[i],-1); l = l2;
r = r2;
}
} long long gcd(long long a,long long b){
return b==0?a:gcd(b,a%b);
} long long up[maxn], down[maxn]; void Print(){
REP(i,1,m){
int l=asks[i].l;
int r=asks[i].r; if(l==r){
up[i]=0;
down[i]=1;
} else{
int length=r-l+1;
up[i]=ans[i]-length;
down[i]=(long long)length*(length-1); if(up[i]==0){
up[i]=0;
down[i]=1;
continue;
} long long k=gcd(up[i],down[i]);
up[i]/=k;
down[i]/=k;
}
} REP(i,1,m)
printf("%lld/%lld\n",up[i],down[i]);
} int main(){
SetIO("input");
Read();
Build();
Work();
Print();
return 0;
}
洛谷P1494 [国家集训队]小Z的袜子的更多相关文章
- 洛谷 P1494 [国家集训队] 小Z的袜子
题目概述: 小Z把N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两只袜子是不是完整的一双,甚至不在意两只袜子是否一左一右,他却很在意袜子的颜色,毕竟穿两只不同色的袜子会很尴尬. 你的任务 ...
- 洛谷 P1494 [国家集训队]小Z的袜子(莫队)
题目链接:https://www.luogu.com.cn/problem/P1494 一道很经典的莫队模板题,然而每道莫队题的大体轮廓都差不多. 首先莫队是一种基于分块的算法,它的显著特点就是: 能 ...
- 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的袜子(莫队)
题目链接:https://www.luogu.org/problemnew/show/P1494 题目大意:中文题目 具体思路:计算概率的时候,每一次是区间的移动,每一次移动,记得先将原来的记录的影响 ...
- 【luogu P1494 [国家集训队]小Z的袜子】 题解
题目链接:https://www.luogu.org/problemnew/show/P1494 #include <cstdio> #include <algorithm> ...
- luogu P1494 [国家集训队]小Z的袜子 ( 普 通 )
题目: 链接:https://www.luogu.org/problemnew/show/P1494 题意:一些袜子排成一排,每个袜子有固定的颜色. ...
- Luogu P1494 [国家集训队]小Z的袜子
比较简单的莫队题,主要是为了熟练板子. 先考虑固定区间时我们怎么计算,假设区间\([l,r]\)内颜色为\(i\)的袜子有\(cnt_i\)只,那么对于颜色\(i\)来说,凑齐一双的情况个数为: \( ...
随机推荐
- VC工程里,如何编译汇编语言的文件
最近老是有朋友问,VC工程里,如何编译汇编语言的文件,接下来就说一下步骤: 1.将汇编语言文件,加入到工程里: 2.在Solution Explorer窗口中右键单击Visual C++项目,选择Bu ...
- 顺序容器之vector
最近因为需要,在看C++ primer,哇,感觉这本书真不错,讲的细而且到位,而且大量的练习题,不愧为C++学习的经典书籍.今天看了顺序容器方面的内容,现在汇报一下: 一.什么是vector vect ...
- eclipse搭建golang for windows
用惯了eclipse,所以.... golang windows开发环境 参考文档:http://golang.org/doc/install 1.下载go安装包http://code.google. ...
- Spring Batch 高级-
spring batch / 并行处理 / 多线程 分区 1. 并行处理,多线程,分区 http://blog.csdn.net/github_36849773/article/details/692 ...
- day21 模块
目录 模块 import 与 from...import 循环导入问题 解决方案一 解决方案二 Python文件的两种用途 从普通的面条型代码,到函数型代码,其实是在做什么? 封装代码,一个函数差不多 ...
- 《Exception》第八次团队作业:Alpha冲刺(第三天)
一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.掌握软件测试基础技术.2.学习迭代式增 ...
- BZOJ 1016 最小生成树计数(矩阵树定理)
我们把边从小到大排序,然后依次插入一种权值的边,然后把每一个联通块合并. 然后当一次插入的边不止一条时做矩阵树定理就行了.算出有多少种生成树就行了. 剩下的交给乘法原理. 实现一不小心就会让程序变得很 ...
- IntelliJ IDEA could not autowire no beans of 'Decoder'
IntelliJ IDEA could not autowire no beans of 'Decoder' 学习了:http://blog.csdn.net/u012453843/article/ ...
- poj2528 Mayor's posters(线段树,离散化)
离散化的思想: 对于这样的数据 (3,10000). (9,1000000). (5.100000), (1,1000). (7,1000000) 我们能够将其处理为 (2,7). (5,9). (3 ...
- Python中常见的文件对象内建函数
文件对象内建方法列表 文件对象的方法 操作 file.close() 关闭文件 file.fileno() 返回文件的描写叙述符(file descriptor.FD,整数值) file.flush( ...