题目大意

有\(n\)(\(n\leq5*10^4\))个数\(a_1,a_2,...,a_n\)(\(\forall i\in[1,n], 1\leq a_i\leq n\))

\(m\)(\(m\leq5*10^4\))次询问,每次给出区间\([L,R]\),求在\(a_L,a_{L+1},...,a_R\)中随机选两个数,两数相等的概率

题解

设区间\([L,R]\)中,值为\(i\)的数的个数为\(b_i\)

那么答案就是\(\sum_{i=1}^{n}{C_{b_i}^{2}}\)

发现将区间变成\([L+1,R],[L,R+1],[L-1,R],[L,R-1]\)时,维护\(b\)数组的复杂度是\(\Theta(1)\)的

这样就可以用莫队离线解决了

代码
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define maxn 50010
#define blo 223
#define LL long long
using namespace std;
int read()
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)&&ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
void write(LL x)
{
if(x==0){putchar('0');return;}
int f=0;char ch[20];
if(x<0)putchar('-'),x=-x;
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);
return;
}
struct que{int l,r,id;}q[maxn];
int nowl,nowr,num[maxn],n,m,a[maxn];
LL ansa[maxn],ansb[maxn],nowans;
LL gcd(LL a,LL b)
{
if(a>b)swap(a,b);
if(!a)return b;
return gcd(b%a,a);
}
bool cmpl(que x,que y){return x.l<y.l;}
bool cmpr1(que x,que y){return x.r<y.r;}
bool cmpr2(que x,que y){return x.r>y.r;}
LL c2(int x){return (LL)x*(LL)(x-1)/2ll;}
int main()
{
n=read(),m=read();
rep(i,1,n)a[i]=read();
rep(i,1,m)q[i].l=read(),q[i].r=read(),q[i].id=i;
sort(q+1,q+m+1,cmpl);
for(int i=1,s=1,t=1+blo;s<=m;s=t+1,t=s+blo,i)
{
int lim=min(t,m);
if(i&1)sort(q+s,q+lim+1,cmpr1);
else sort(q+s,q+lim+1,cmpr2);
}nowl=1,nowr=0;
rep(i,1,m)
{
while(nowr<q[i].r)nowr++,nowans-=c2(num[a[nowr]]),num[a[nowr]]++,nowans+=c2(num[a[nowr]]);
while(nowl>q[i].l)nowl--,nowans-=c2(num[a[nowl]]),num[a[nowl]]++,nowans+=c2(num[a[nowl]]);
while(nowr>q[i].r)nowans-=c2(num[a[nowr]]),num[a[nowr]]--,nowans+=c2(num[a[nowr]]),nowr--;
while(nowl<q[i].l)nowans-=c2(num[a[nowl]]),num[a[nowl]]--,nowans+=c2(num[a[nowl]]),nowl++;
if(nowans==0)ansa[q[i].id]=0,ansb[q[i].id]=1;
else
{
ansa[q[i].id]=nowans,ansb[q[i].id]=c2(q[i].r-q[i].l+1);
LL gdc=gcd(ansa[q[i].id],ansb[q[i].id]);
ansa[q[i].id]/=gdc,ansb[q[i].id]/=gdc;
}
}
rep(i,1,m)write(ansa[i]),putchar('/'),write(ansb[i]),putchar('\n');
return 0;
}

并不对劲的bzoj2038:p1494:[国家集训队]小Z的袜子的更多相关文章

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

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

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

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

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

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

  4. BZOJ2038: [2009国家集训队]小Z的袜子(hose) -- 莫队算法 ,,分块

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

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

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

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

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

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

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

  8. BZOJ2038: [2009国家集训队]小Z的袜子(hose)

    Time Limit: 20 Sec  Memory Limit: 259 MB Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天, ...

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

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

随机推荐

  1. [NOIP2001] 普及组

    装箱问题 裸01背包,速刷过 #include<cstdio> #include<iostream> #include<cmath> using namespace ...

  2. C++常见函数(备忘录)

    substr(string的成员函数) 语法: basic_string substr( size_type index, size_type num = npos ); substr()返回本字符串 ...

  3. AlertDialog自定义

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  4. iOS present出一个背景为半透明的试图

    WDKChatRoomViewController *roomVC = [[WDKChatRoomViewController alloc] init]; roomVC.titleStr = [gro ...

  5. python学习之 - configparser模块

    configparser模块功能:用于生成和修改常见配置文件.基本常用方法如下: read(filename):直接读取配置文件write(filename):将修改后的配置文件写入文件中.defau ...

  6. springboot 第一个程序

    idea --> new project --> 选择Spirng Initializr --> next 傻瓜式操作  --> 添加web依赖 项目基本结构: 创建contr ...

  7. 搭建Spring+mybatis报错

    java.lang.ClassCastException: com.sun.proxy.$Proxy12 cannot be cast to com.bdqn.service.impl.UserSer ...

  8. 用Lazarus编写第一个程序Pascal版的hello world

    安装 Lazarus的过程不用多说,都是傻瓜式的. 打开Lazarus.Lazarus会自己主动新建一个窗体形式的应用程序. 你会看到五个窗体. 主窗体 这个窗体显示有标题栏.菜单条和工具栏. 对象视 ...

  9. Android 项目导入常见错误

    1.SDK版本号不正确应,你能够打开你项目中的project.properties文件,改动target=android-18(我这是18) ,将18改 为14(其它都能够),再改回18会又一次载入. ...

  10. CodeForces 404 Marathon ( 浮点数取模 -- 模拟 )

    B. Marathon time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...