0
点赞
收藏
分享

微信扫一扫

BZOJ-2038: [2009国家集训队]小Z的袜子(hose)(莫队算法+分块)

三分梦_0bc3 2022-09-16 阅读 131

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

Time Limit: 20 Sec  Memory Limit: 259 MB

Submit: 11364  Solved: 5074

[​​Submit​​​][​​Status​​​][​​Discuss​​]

Description

作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿。终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……
具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两只袜子是不是完整的一双,甚至不在意两只袜子是否一左一右,他却很在意袜子的颜色,毕竟穿两只不同色的袜子会很尴尬。
你的任务便是告诉小Z,他有多大的概率抽到两只颜色相同的袜子。当然,小Z希望这个概率尽量高,所以他可能会询问多个(L,R)以方便自己选择。

Input

输入文件第一行包含两个正整数N和M。N为袜子的数量,M为小Z所提的询问的数量。接下来一行包含N个正整数Ci,其中Ci表示第i只袜子的颜色,相同的颜色用相同的数字表示。再接下来M行,每行两个正整数L,R表示一个询问。

Output

包含M行,对于每个询问在一行中输出分数A/B表示从该询问的区间[L,R]中随机抽出两只袜子颜色相同的概率。若该概率为0则输出0/1,否则输出的A/B必须为最简分数。(详见样例)

Sample Input

6 4
1 2 3 3 3 2
2 6
1 3
3 5
1 6

Sample Output

2/5
0/1
1/1
4/15
【样例解释】
询问1:共C(5,2)=10种可能,其中抽出两个2有1种可能,抽出两个3有3种可能,概率为(1+3)/10=4/10=2/5。
询问2:共C(3,2)=3种可能,无法抽到颜色相同的袜子,概率为0/3=0/1。
询问3:共C(3,2)=3种可能,均为抽出两个3,概率为3/3=1/1。
注:上述C(a, b)表示组合数,组合数C(a, b)等价于在a个不同的物品中选取b个的选取方案数。
【数据规模和约定】
30%的数据中 N,M ≤ 5000;
60%的数据中 N,M ≤ 25000;
100%的数据中 N,M ≤ 50000,1 ≤ L < R ≤ N,Ci ≤ N。

HINT

 

Source

​​版权所有者:莫涛​​

 

BZOJ-2038: [2009国家集训队]小Z的袜子(hose)(莫队算法+分块)_i++

 

卿学姐说莫队是可以背板子的,我就背板子咯~

1 #include "bits/stdc++.h"
2 using namespace std;
3 typedef long long LL;
4 const int MAX=50005;
5 int n,m;
6 LL num[MAX],an1[MAX],an2[MAX],pos[MAX];
7 struct Ask{
8 int l,r;
9 int id;
10 bool operator < (const Ask &tt) const {
11 if (pos[l]==pos[tt.l])
12 return pos[r]<pos[tt.r];
13 return pos[l]<pos[tt.l];
14 }
15 }stu[MAX];
16 bool cmp(Ask rr,Ask tt){
17 if (pos[rr.l]==pos[tt.l])
18 return pos[rr.r]<pos[tt.r];
19 return pos[rr.l]<pos[tt.l];
20 }
21 int a[MAX];
22 inline int read(){
23 int an=0,x=1;char c=getchar();
24 while (c<'0' || c>'9'){if (c=='-') x=-1;c=getchar();}
25 while (c>='0' && c<='9') {an=an*10+c-'0';c=getchar();}
26 return an*x;
27 }
28 LL ans;
29 void update(int x,int d){
30 ans-=num[a[x]]*num[a[x]];
31 num[a[x]]+=d;
32 ans+=num[a[x]]*num[a[x]];
33 }
34 LL gcd(LL x,LL y){
35 if (y==0) return x;
36 return gcd(y,x%y);
37 }
38 int main(){
39 freopen ("hose.in","r",stdin);
40 freopen ("hose.out","w",stdout);
41 int i,j;
42 memset(num,0,sizeof(num));
43 n=read();m=read();
44 LL bas=(LL)sqrt(n*1.0);
45 for (i=1;i<=n;i++){
46 a[i]=read();
47 pos[i]=i/bas;
48 }
49 for (i=1;i<=m;i++){
50 stu[i].l=read();
51 stu[i].r=read();
52 stu[i].id=i;
53 }
54 sort(stu+1,stu+m+1);
55 LL L=1,R=0,aa,bb,cc;
56 for (i=1;i<=m;i++){
57 if (stu[i].l==stu[i].r){
58 an1[stu[i].id]=0;
59 an2[stu[i].id]=1;
60 continue;
61 }
62 while (R<stu[i].r){
63 R++;
64 update(R,1);
65 }
66 while (R>stu[i].r){
67 update(R,-1);
68 R--;
69 }
70 while (L<stu[i].l){
71 update(L,-1);
72 L++;
73 }
74 while (L>stu[i].l){
75 L--;
76 update(L,1);
77 }
78 aa=ans-(stu[i].r-stu[i].l+1);
79 bb=(LL)(stu[i].r-stu[i].l+1)*(LL)(stu[i].r-stu[i].l);
80 cc=gcd(aa,bb);
81 an1[stu[i].id]=aa/cc;
82 an2[stu[i].id]=bb/cc;
83 }
84 for (i=1;i<=m;i++){
85 printf("%lld/%lld\n",an1[i],an2[i]);
86 }
87 return 0;
88

 

第二次写 小Z的袜子,粘上代码qwq

1 #include "bits/stdc++.h"
2 using namespace std;
3 typedef long long LL;
4 const int MAX=5e4+5;
5 int n,m;
6 LL a[MAX],num[MAX],pos[MAX],bas,ans;
7 LL an1[MAX],an2[MAX];
8 struct Node{
9 int id;
10 int l,r;
11 bool operator < (const Node &tt) const {
12 if (pos[l]!=pos[tt.l])
13 return pos[l]<pos[tt.l];
14 return r<tt.r;
15 }
16 }que[MAX];
17 LL gcd(LL x,LL y){return (y==0?x:gcd(y,x%y));}
18 inline LL read(){
19 LL an=0,x=1;char c=getchar();
20 while (c<'0' || c>'9') {if (c=='-') x=-1;c=getchar();}
21 while (c>='0' && c<='9') {an=an*10+c-'0';c=getchar();}
22 return an*x;
23 }
24 void update(int x,int y){
25 ans-=num[a[x]]*num[a[x]];
26 num[a[x]]+=y;
27 ans+=num[a[x]]*num[a[x]];
28 }
29 int main(){
30 freopen ("hose.in","r",stdin);
31 freopen ("hose.out","w",stdout);
32 int i,j;
33 n=read(),m=read();
34 bas=(LL)sqrt(n*1.0);
35 for (i=1;i<=n;i++) a[i]=read(),pos[i]=(LL)i/bas;
36 for (i=1;i<=m;i++){
37 que[i].id=i;
38 que[i].l=read();que[i].r=read();
39 }
40 sort(que+1,que+m+1);
41 int L=1,R=0;
42 LL aa,bb,cc;
43 for (i=1;i<=m;i++){
44 if (que[i].l==que[i].r){an1[que[i].id]=0;an1[que[i].id]=1;continue;}
45 while (R<que[i].r){R++;update(R,1);}
46 while (R>que[i].r){update(R,-1);R--;}
47 while (L>que[i].l){L--;update(L,1);}
48 while (L<que[i].l){update(L,-1);L++;}
49 aa=ans-(que[i].r-que[i].l+1);
50 bb=(LL)(que[i].r-que[i].l+1)*(LL)(que[i].r-que[i].l);
51 cc=gcd(aa,bb);
52 an1[que[i].id]=aa/cc;
53 an2[que[i].id]=bb/cc;
54 }
55 for (i=1;i<=m;i++){
56 printf("%lld/%lld\n",an1[i],an2[i]);
57 }
58 return 0;
59

 

举报

相关推荐

0 条评论