↓ Skip to main content
  1. Posts/

(莫队算法的学习)bzoj 2038 [2009国家集训队]小Z的袜子(hose)

·1501 words·3 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

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

Time Limit: 20 Sec Memory Limit: 259 MB Submit: 5327 Solved: 2461 [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。

中文题目,不解释了。 思路:分块+莫队算法。http://blog.csdn.net/bossup/article/details/39236275 这篇博客讲得很清楚。看完之后自己写的。。调了大概两个小时。。。?感觉有些理解了。 注意的是,如果最后分子为0,那么分母要赋值成1,而且分子为0的时候不要去求gcd…

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年02月10日 星期三 15时06分20秒
  4File Name :code/bzoj/2038.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define pi pair < int ,int >
 26#define MP make_pair
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=5E4+7;
 34int n,m;
 35int col[N];
 36int pos[N];  //pos[i]表示第i个袜子在第几块
 37struct node
 38{
 39    int l,r,id;
 40
 41    bool operator <(node b)const
 42    {
 43	if (pos[l]==pos[b.l])
 44	    return r<b.r;
 45	return pos[l]<pos[b.l];
 46    }
 47
 48}q[N];
 49
 50
 51struct node2
 52{
 53    LL fz,fm;
 54}ans[N];
 55LL num[N];
 56LL sum;
 57
 58
 59void update ( int x,int delta)
 60{
 61
 62    sum-=LL(num[col[x]]*num[col[x]]);
 63  //  cout<<"sum:::"<<sum<<endl;
 64    num[col[x]]+=delta;
 65    sum+=LL(num[col[x]]*num[col[x]]);
 66
 67   // cout<<x<<" "<<delta<<" "<<"sum::"<<sum<<endl;
 68
 69}
 70
 71LL gcd( LL a,LL b)
 72{
 73    if (a<b) return gcd(b,a);
 74    if (a%b==0) return b;
 75    return gcd(b,a%b);
 76}
 77int main()
 78{
 79	#ifndef  ONLINE_JUDGE
 80	freopen("code/in.txt","r",stdin);
 81  #endif
 82
 83	cin>>n>>m;
 84	ms(num,0LL);
 85	int siz = int(sqrt(n));
 86//	cout<<"siz:"<<siz<<endl;
 87	for ( int i =  1 ; i  <= n ; i++)
 88	{
 89	    scanf("%d",&col[i]);
 90	    pos[i] = (i-1)/siz;
 91	}
 92//	cout<<"aaa"<<endl;
 93	for ( int i = 1 ; i <= m ; i++)
 94	{
 95	    scanf("%d %d",&q[i].l,&q[i].r);
 96	    q[i].id = i ;  //离线处理,记得记录id
 97	}
 98	sort(q+1,q+m+1);
 99	//for ( int i = 1 ; i <= m ; i++) cout<<"q[i].l:"<<q[i].l<<" q[i].r:"<<q[i].r<<endl;
100//	cout<<"bbb"<<endl;
101
102	int prel=1,prer=0;
103	sum=0LL;
104	for ( int i = 1 ; i <= m ; i++)
105	{
106	    int id = q[i].id;
107
108//	    cout<<"q[i].l:"<<q[i].l<<" q[i].r:"<<q[i].r<<endl;
109	    if (q[i].l==q[i].r)  //只有一直袜子的话是不可能配成一对的。
110	    {
111		ans[id].fz = 0LL;
112		ans[id].fm = 1LL;
113		continue;
114	    }
115	  //  cout<<"prel:"<<prel<<" prer:"<<prer<<endl;
116	    if (q[i].r>prer)
117	    {
118		for ( int j = prer +1 ; j <=q[i].r ; j++) update(j,1);
119//		cout<<"sumaa:"<<sum<<endl;
120	    }
121	    else
122	    {
123		for (int j = prer ; j > q[i].r ; j--) update(j,-1);
124	    }
125
126	    prer = q[i].r;
127
128	    if (q[i].l<prel)
129	    {
130		for ( int j = q[i].l ; j < prel  ;j++) update(j,1);
131	    }
132	    else
133	    {
134//		cout<<"sum1:"<<sum<<endl;
135		for ( int j = q[i].l -1 ; j>=prel  ; j--) update(j,-1);
136//		cout<<"sum2:"<<sum<<endl;
137	    }
138	    prel = q[i].l;
139//	    cout<<q[i].l<<"   "<<q[i].r<<"   "<<sum<<endl;
140	    LL fz = sum +(-1LL)*(q[i].r-q[i].l+1);
141	    LL fm = 1LL*(q[i].r-q[i].l)*1LL*(q[i].r-q[i].l+1);
142//	    cout<<"fz:"<<fz<<" fm:"<<fm<<endl;
143	    if (fz==0)
144	    {
145		fm = 1;
146	    }
147	    else
148	    {
149		LL GCD = gcd(fz,fm);
150		fz/=GCD;
151		fm/=GCD;
152	    }
153	    ans[id].fz = fz;
154	    ans[id].fm = fm;
155
156//	    cout<<endl;
157	}
158
159	for ( int i = 1 ; i <= m ; i++)
160	    printf("%lld/%lld\n",ans[i].fz,ans[i].fm);
161
162  #ifndef ONLINE_JUDGE
163  fclose(stdin);
164  #endif
165    return 0;
166}

Related

hdoj4391 Paint The Wall

·670 words·2 mins
http://acm.hdu.edu.cn/showproblem.php?pid=4391 题意:有 n 个点,每个点有一种颜色(可能相同),两种操作:1、将区间 [a,b] 染成颜色 c ; 2、询问区间 [a,b] 中颜色为 c 的点有多少个。 思路:因为颜色种类很多。。。没办法通过建很多棵线段树解决。我们用分块的办法。。。

hdoj 1754 I hate it

·581 words·2 mins
http://acm.hdu.edu.cn/showproblem.php?pid=1754 题意:给定一个区间,有m组操作,操作可以是改变单点,或者查询区间最大值。对于每组查询,输出。 思路:分块。这篇博客说得很不错。http://www.cnblogs.com/sweetsc/archive/2012/08/15/2639395.html

codeforces #319 div 2 E C. Points on Plane (分块)

·453 words·1 min
初识分快. 引一段题解: Let’s split rectangle 106 × 106 by vertical lines into 1000 rectangles 103 × 106. Let’s number them from left to right. We’re going to pass through points rectangle by rectangle. Inside the rectangle we’re going to pass the points in increasing order of y-coordinate if the number of rectangle is even and in decreasing if it’s odd.

codeforces #342 div 2 D. Finals in arithmetic

·555 words·2 mins
http://codeforces.com/contest/625/problem/D 题意:问能否找到一个s,满足s+s的反转=k 思路:如果是回文数。。。那么显然满足。除以2就可以得到答案。 代码实现 1 如果不是回文数。。那么考虑进位的情况。 2 要么从后一位进1,要么从前一位退10回来。 3 需要特殊考虑1开头的。 4 5 6 7/* *********************************************** 8Author :111qqz 9Created Time :2016年02月07日 星期日 18时28分39秒 10File Name :code/cf/#342/D.cpp 11************************************************ */ 12 13#include <cstdio> 14#include <cstring> 15#include <iostream> 16#include <algorithm> 17#include <vector> 18#include <queue> 19#include <set> 20#include <map> 21#include <string> 22#include <cmath> 23#include <cstdlib> 24#include <ctime> 25#include <sstream> 26#define fst first 27#define sec second 28#define lson l,m,rt<<1 29#define rson m+1,r,rt<<1|1 30#define ms(a,x) memset(a,x,sizeof(a)) 31typedef long long LL; 32#define pi pair < int ,int > 33#define MP make_pair 34 35using namespace std; 36const double eps = 1E-8; 37const int dx4[4]={1,0,0,-1}; 38const int dy4[4]={0,-1,1,0}; 39const int inf = 0x3f3f3f3f; 40const int N=1E5+7; 41bool vis[N]; 42 43void pre() 44{ 45 ms(vis,false); 46 for ( int i = 10 ; i <12000 ; i++) 47 { 48 int vala = i ; 49 stringstream ss; 50 ss<<vala; 51 string tmp = ss.str(); 52 53 reverse(tmp.begin(),tmp.end()); 54 55 int valb; 56 sscanf(tmp.c_str(),"%d",&valb); 57// cout<<i<<" "<<vala+valb<<endl; 58 if (vala+valb<N) vis[vala+valb] = true; 59 60 } 61 62 for ( int i = 1 ; i < N ; i++) 63 { 64 if (vis[i]) cout<<i<<endl; 65 } 66} 67char ans[N],s[N]; 68int n; 69int sum[N]; 70 71bool ok() 72{ 73 // cout<<"n:"<<n<<endl; 74 for ( int i = 0 ; i < n/2 ;) 75 { 76 int l = i ; 77 int r = n-1-i; 78 if (sum[l]==sum[r]) i++; 79 else if (sum[l]==sum[r]+1||sum[l]==sum[r]+11) 80 { 81 sum[l]--; 82 sum[l+1]+=10; 83 } 84 else if (sum[l]==sum[r]+10) 85 { 86 sum[r-1]--; 87 sum[r]+=10; 88 89 } 90 else return false; 91 } 92 93 if (n%2==1) 94 { 95 if (sum[n/2]%2==1) return false; 96 if (sum[n/2]>18||sum[n/2]<0) return false; 97 ans[n/2] = char(sum[n/2]/2 +'0'); 98// cout<<"ASDHJKASD"<<endl; 99 } 100 for ( int i = 0 ; i < n/2 ; i++) 101 { 102 if (sum[i]<0||sum[i]>18) return false; 103 ans[i] =(sum[i]+1)/2+'0'; 104 ans[n-1-i] =sum[i]/2+'0'; 105 } 106 return ans[0]>'0'; 107} 108int main() 109{ 110 #ifndef ONLINE_JUDGE 111 freopen("code/in.txt","r",stdin); 112 #endif 113 114 //pre(); 115 // 116 117 scanf("%s",s); 118 n = strlen(s); 119 for ( int i = 0 ; i < n ; i++) sum[i] = s[i] - '0'; 120 if (ok()) 121 { 122 cout<<ans<<endl; 123// cout<<"aaa"<<endl; 124 return 0; 125 } 126 127 if (s[0]=='1'&&n>1) 128 { 129 for ( int i = 0 ; i < n ; i++) 130 sum[i] = s[i+1]-'0'; 131 sum[0]+=10; 132 n--; 133 if (ok()) 134 { 135 cout<<ans<<endl; 136 return 0; 137 } 138 else 139 puts("0"); 140 } 141 else puts("0"); 142 143 144 #ifndef ONLINE_JUDGE 145 fclose(stdin); 146 #endif 147 return 0; 148}