题目链接: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的袜子】 题解的更多相关文章

  1. luogu P1494 [国家集训队]小Z的袜子 ( 普 通 )

    题目:    链接:https://www.luogu.org/problemnew/show/P1494 题意:一些袜子排成一排,每个袜子有固定的颜色.                        ...

  2. Luogu P1494 [国家集训队]小Z的袜子

    比较简单的莫队题,主要是为了熟练板子. 先考虑固定区间时我们怎么计算,假设区间\([l,r]\)内颜色为\(i\)的袜子有\(cnt_i\)只,那么对于颜色\(i\)来说,凑齐一双的情况个数为: \( ...

  3. P1494 [国家集训队]小Z的袜子

    题目 P1494 [国家集训队]小Z的袜子 解析 在区间\([l,r]\)内, 任选两只袜子,有 \[r-l+1\choose2\] \[=\frac{(r-l+1)!}{2!(r-l-1)!}\] ...

  4. P1494 [国家集训队]小Z的袜子/莫队学习笔记(误

    P1494 [国家集训队]小Z的袜子 题目描述 作为一个生活散漫的人,小\(Z\)每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小\(Z\)再也无法忍受这恼人的找袜子过程,于是他 ...

  5. P1494 [国家集训队]小Z的袜子(luogu)

    P1494 小Z的袜子 终于了解了莫队算法(更专业的名称Square Root Decomposition of Queries) 莫队算法: 一般来说解决静态(实际上也有修改的但复杂度更高)的离线( ...

  6. 洛谷 P1494 [国家集训队] 小Z的袜子

    题目概述: 小Z把N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两只袜子是不是完整的一双,甚至不在意两只袜子是否一左一右,他却很在意袜子的颜色,毕竟穿两只不同色的袜子会很尴尬. 你的任务 ...

  7. BZOJ2038:[2009国家集训队]小Z的袜子——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找 ...

  8. P1494 [国家集训队]小Z的袜子(莫队)

    题目链接:https://www.luogu.org/problemnew/show/P1494 题目大意:中文题目 具体思路:计算概率的时候,每一次是区间的移动,每一次移动,记得先将原来的记录的影响 ...

  9. 洛谷 P1494 [国家集训队]小Z的袜子(莫队)

    题目链接:https://www.luogu.com.cn/problem/P1494 一道很经典的莫队模板题,然而每道莫队题的大体轮廓都差不多. 首先莫队是一种基于分块的算法,它的显著特点就是: 能 ...

随机推荐

  1. java中构造函数的特点

    构造函数的名字必须和类名完全相同,构造函数不能有 返回值,就是void 也不要写,构造函数不可以被子类继承 构造函数可以重载但是不可以被子类覆盖. 简单的例子 class A{ A(){ } A(in ...

  2. params传递任意参数

    namespace 传递任意参数{ class Program { static void Main(string[] args) { //可传递任意数量参数 Test(1, 2, "sas ...

  3. PlayMaker入门介绍

    http://www.jianshu.com/p/ce791bef66bb   PlayMaker是什么? PlayMaker是Unity3D的一款 可视化 的 有限元状态机(Finite-state ...

  4. 常用shell脚本(持续更新中)

    1. 写一个shell脚本来得到当前的日期,时间,用户名和当前工作目录. 答案 : 输出用户名,当前日期和时间,以及当前工作目录的命令就是logname,date,who i am和pwd. #!/b ...

  5. 【代码笔记】XML深入学习:DTD约束与DTD语法(2)

    DTD语法之定义实体(了解即可) 实体分为一般实体和参数实体. 定义实体:定义变量 引用实体:使用变量 一般实体:定义实体在DTD中,实体引用在xml中. 参数实体:定义实体在DTD中,实体引用在DT ...

  6. canvas玩转微信红包

    CSS3相关属性: <!DOCTYPE html> <html> <head lang='en'> <meta charset='UTF-8'/> &l ...

  7. matlab 中“newff” 函数的参数设置

    matlab 中"newff" 函数的使用方法技巧|和各参数的意义 先来一个简单的源程序让大家练习一下: % Here input P and targets T define a ...

  8. SharePoint中遇到Timeout

    使用SharePoint时会遇到不止一种的timeout(即超时)错误. 如果遇到了timeout, 该怎么区分呢? 大致上SharePoint可以控制和影响的timeout地方如下: 1. Shar ...

  9. js事件学习的小demo

    直接上代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> ...

  10. GBase数据库——常用命令

    针对GBase 8a 版本 1数据库操作与维护 1.1数据库启停 [root@OMMB-66-V10-001 ~]# service gcware stop Stopping GCMonit succ ...