首页 技术 正文
技术 2022年11月14日
0 收藏 481 点赞 4,365 浏览 1405 个字

题意

给出包含n个数字的序列,和m个查询。每次查询问区间[l,r]中挑选出两个数字,大小相同的概率为多少。

分析

莫队的入门题吧。代码是非常好写,关键是时间复杂度的证明。O(n*sqrt(n))。我还有点迷糊,等我再做几个题再说···

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath> using namespace std;
typedef long long LL;
const int maxn=+;
int val[maxn],belong[maxn],f[maxn];
int n,m,cnt,block;
LL gcd(LL a,LL b){
if(!b)return a;
return gcd(b,a%b);
} struct Node{
int L,R,id;
bool operator<(const Node& rhs)const{
return belong[L]<belong[rhs.L]||(belong[L]==belong[rhs.L]&&R<rhs.R);
}
LL a,b;
void solve(){
LL r=gcd(a,b);
a/=r,b/=r;
}
}ask[maxn];
int cmp(Node a,Node b){
return a.id<b.id;
}
LL ans;
void update(int p,int addv){
ans=ans+*addv*f[val[p]]+;
f[val[p]]+=addv;
} int main(){
scanf("%d%d",&n,&m);
memset(f,,sizeof(f));
for(int i=;i<=n;i++)
scanf("%d",&val[i]);
block=sqrt(n);
cnt=n/block;
if(n%block)cnt++;
for(int i=;i<=n;i++){
belong[i]=(i-)/block+;
}
for(int i=;i<=m;i++){
scanf("%d%d",&ask[i].L,&ask[i].R);
ask[i].id=i;
} sort(ask+,ask++m);
ans=;
int l=,r=;
for(int i=;i<=m;i++){
if(r<ask[i].R){
for(r=r+;r<ask[i].R;r++){
update(r,);
}
update(r,);
}
if(l>ask[i].L){
for(l=l-;l>ask[i].L;l--){
update(l,);
}
update(l,);
}
if(r>ask[i].R){
for(;r>ask[i].R;r--){
update(r,-);
}
}
if(l<ask[i].L){
for(;l<ask[i].L;l++){
update(l,-);
}
}
if(ask[i].L==ask[i].R){
ask[i].a=,ask[i].b=;
continue;
}
ask[i].a=ans-(ask[i].R-ask[i].L+),ask[i].b=(LL)(ask[i].R-ask[i].L+)*(ask[i].R-ask[i].L);
ask[i].solve();
}
sort(ask+,ask++m,cmp);
for(int i=;i<=m;i++){
printf("%lld/%lld\n",ask[i].a,ask[i].b);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905