两道莫队板题

bzoj2038(小z的袜子)

     

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

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

        对询问分块排序,用个cnt数组记录个数,袜子x被抽到的种数为C(2,cnt[x]),cnt[x]>=2,每次移动时修改ans即可。。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int A[100010],k;
long long ans,cnt[100010],B[100010],C[100010];
struct Q{
    int l,r,id;
}q[100010];

int cmp(Q a,Q b){
    if(a.l/k==b.l/k) return a.r<b.r;
    else return a.l<b.l;
}
long long gcd(long long a,long long b){
    if(a%b==0) return b;
    else return gcd(b,a%b);
}
void add(int x){
     cnt[x]++;
     if(cnt[x]==2) ans++;
     else if(cnt[x]>2) ans=ans-(cnt[x]-1)*(cnt[x]-2)/2+(cnt[x]-1)*cnt[x]/2;
}
void del(int x){
     cnt[x]--;
     if(cnt[x]==1) ans--;
     else if(cnt[x]>1) ans=ans-(cnt[x]+1)*cnt[x]/2+(cnt[x]-1)*cnt[x]/2;
}
int main(){
    int n,m,i;
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(cnt,0,sizeof(cnt));
        for(i=1;i<=n;i++) scanf("%d",&A[i]);
        k=sqrt(n);
        for(i=1;i<=m;i++){
            scanf("%d%d",&q[i].l,&q[i].r);
            q[i].id=i;
        }
        sort(q+1,q+1+m,cmp);
        int L=1,R=0;
        ans=0;
        for(i=1;i<=m;i++){
            while(R<q[i].r){
                R++;
                add(A[R]);
            }
            while(L>q[i].l){
                L--;
                add(A[L]);
            }
            while(L<q[i].l){
                del(A[L]);
                L++;
            }
            while(R>q[i].r){
                del(A[R]);
                R--;
            }
            B[q[i].id]=ans;
            long long x=q[i].r-q[i].l;
            C[q[i].id]=x*(x+1)/2;
        }
        for(i=1;i<=m;i++){
            if(B[i]==0||C[i]==0) printf("0/1\n");
            else{
                long long a=gcd(B[i],C[i]);
                printf("%lld/%lld\n",B[i]/a,C[i]/a);
            }
        }
    }
    return 0;
}

hdoj4638   group

Group

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3485    Accepted Submission(s): 1716


 

Problem Description

There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i-1 are friends, Whose ID is i and i+1 are friends. These n men stand in line. Now we select an interval of men to make some group. K men in a group can create K*K value. The value of an interval is sum of these value of groups. The people of same group's id must be continuous. Now we chose an interval of men and want to know there should be how many groups so the value of interval is max.

 

Input

First line is T indicate the case number.
For each case first line is n, m(1<=n ,m<=100000) indicate there are n men and m query.
Then a line have n number indicate the ID of men from left to right.
Next m line each line has two number L,R(1<=L<=R<=n),mean we want to know the answer of [L,R].

 

Output

For every query output a number indicate there should be how many group so that the sum of value is max.

 

Sample Input

 

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

 

Sample Output

 

1 2

           

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<map>
using namespace std;
int A[100010],B[100010],k,cnt[100010],ans;
struct Q{
     int l,r,id;
}q[100010];
int cmp(Q a,Q b){
    if(a.l/k==b.l/k) return a.r<b.r;
    else return a.l<b.l;
}
void add(int x){
    if(cnt[A[x]-1]&&cnt[A[x]+1]) ans--;
    else if(cnt[A[x]-1]==0&&cnt[A[x]+1]==0) ans++;
    cnt[A[x]]++;
}
void del(int x){
    if(cnt[A[x]-1]&&cnt[A[x]+1]) ans++;
    else if(cnt[A[x]-1]==0&&cnt[A[x]+1]==0) ans--;
    cnt[A[x]]--;
}
int main(){
    int t,i,n,m;
    scanf("%d",&t);
    while(t--){
         memset(cnt,0,sizeof(cnt));
         scanf("%d%d",&n,&m);
         k=sqrt(n);
         for(i=1;i<=n;i++) scanf("%d",&A[i]);
         for(i=1;i<=m;i++){
              scanf("%d%d",&q[i].l,&q[i].r);
              q[i].id=i;
         }
         sort(q+1,q+m+1,cmp);
         int L=1,R=0;
         ans=0;
         for(i=1;i<=m;i++){
            while(R<q[i].r){
                R++;
                add(R);
            }
            while(L>q[i].l){
                L--;
                add(L);
            }
            while(L<q[i].l){
                del(L);
                L++;
            }
            while(R>q[i].r){
                del(R);
                R--;
            }
            B[q[i].id]=ans;
         }
         for(i=1;i<=m;i++) printf("%d\n",B[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/guogai13/article/details/81880671