当前位置: 首页 > 工具软件 > Vineyard-Wine > 使用案例 >

Wine Production Gym - 101879H (莫队+离散化)

丰佐
2023-12-01

Wine Production Gym - 101879H (莫队+离散化)

Problem Description

Porto is known worldwide for its wine production. These wines are produced in the outskirts of the city and then aged in oak barrels to obtain their unique flavor. The wineries must follow strict production standards to get the seal of approval that helps with sales. To attain these very high standards, the temperatures at the vineyards are measured daily and must satisfy very specific requirements, according to studies by experts.

In order for a grape to reach optimum development, it is important to known how many different temperatures it faced during its growth and in how many days each of these temperatures were repeated. Recent studies show that the quality of the wine is deeply correlated with the following number. Consider the growth period for the grape and count, in those days, how many distinct temperatures we had, and for each temperature, how many times it was repeated in the period. We say that the grapevine has quality x if we have at least x distinct temperatures that were repeated in x days during the growth of its grapes.

Your task is to compute the quality of production of each grapevine in the vineyard. You are given the temperatures measured in N days in the vineyard. Next, for each one of the Q grapevines in the vineyard, you are given the start and end days of their growth periods. For each one of these grapevines, you must compute the maximum quality of their production.

Input

The first line has two integers N and Q, the number of days of temperature measurements in the vineyard and the number of grapevines, respectively. The second line has N blank-separated integers. The ith integer is the temperature measured on day i. Finally, the next Q lines describe each one of the grapevines. The ith of these lines has two integers, ℓi and ri, the starting and ending day of growth of the grapes in the ith grapevine.

Constraints

1≤N,Q≤3⋅1e4
The temperature each day is between −1e9 and 1e9
1≤ℓi≤ri≤N
Output
Print Q lines, the ith of which is the maximum quality of grapevine i.

Example

Input

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

Output

2   

1   

2   

Note

In the first grapevine we had 3 distinct temperatures. Only one gets repeated at least three times and two of them are repeated twice, so the maximum quality of the grapevine is 2.

In the second grapevine the temperatures were all distinct, so the maximum quality is 1. Finally, the maximum quality of the third grapevine is 2, since we had two temperatures that were repeated twice.

题意

输入两个整数N、Q,分别代表有N个整数和Q次询问
每次询问一个闭区间,问区间中最多有多少个不同的整数m,满足其出现的次数不小于m

1<=N、Q<=3*1e4
-1e9<=ai<=1e9

比如
5 4
1 2 1 2 3
1 4——>2 有两个不同的整数,出现次数都为2
1 3——>1 有两个不同的整数,但是出现次数不满足都>=2,所以答案为1
1 5——>2 有三个不同的整数,但是1、2、3出现的次数分别为3、2、1,不满足次数>=3
而整数1、2两个整数,出现的次数满足>=2,故答案为1
2 3——>1 有两个不同的整数,但是出现的次数都为1,不满足>=种数,故最大的种数是1

思路

虽然每个数的范围很大【-1e9,1e9】,但是N比单个数的范围小
所以可以离散化一下
之后用莫队即可

AC代码

#include <bits/stdc++.h>
#define ms(x) memset(x, 0, sizeof(x))
#define ll long long
using namespace std;
const int N = 30103;
int n, m, pos[N];  //pos数组用于方便分块,排序
int c[N], sub[N];  //两个数组,用于对C[N]离散化
int s[N], ans, num[N];  //s[i]代表i出现的次数,num[i]代表出现次数为i的有多少个不同的数
struct node {
    int l, r, id;  //id用来存一开始询问的顺序,因为莫队是离线输出
    int ans;       //存询问的答案
}q[N];
bool cmp(node a, node b) {  //按块排序
    if(pos[a.l] == pos[b.l]) {
        return a.r < b.r;
    }
    return a.l < b.l;
}
bool cmpId(node a, node b) {
    return a.id < b.id;
}
int uni, idx;
void Update(int index, int tp) {  //  s[index] index出现次数  
    int x = c[index];             //  num[x] 出现次数为x的数有多少次
    if(tp==-1){
        num[s[x]]--;
        s[x]--;
        if(num[idx]<ans){  //每次去掉一个数,很明显,对于答案的影响最多就是 -1
            ans--;
            idx--;
        }
    }
    else{
        s[x]++;
        num[s[x]]++;
        if(num[s[x]]>=s[x]&&s[x]>ans){  //判断是否要更新满足题目要求的最大值
            ans=s[x];
            idx=s[x];
        }
    }
    //printf("s[x]:%d   num[s[x]]:%d\n", s[x], num[s[x]]);

}
void solve() {  
    for(int i=1, l=1,r=0; i<=m; i++) {
        for( ; r<q[i].r; r++) {
            Update(r+1, 1);
        }
        for( ; r>q[i].r; r--) {
            Update(r, -1);
        }
        for( ; l<q[i].l; l++) {
            Update(l, -1);
        }
        for( ; l>q[i].l; l--) {
            Update(l-1, 1);
        }
        q[i].ans = ans;
    }
}
int main() {
    while(scanf("%d%d", &n,&m)!=EOF) {
        memset(num,0,sizeof(num));  //初始化
        memset(s, 0, sizeof(s)), ans = 0, idx=-1;  //初始化
        int block = (int)sqrt(n*1.0);
        for(int i=1; i<=n; i++) scanf("%d", &c[i]), pos[i] = (i-1)/block + 1,sub[i] = c[i];
        for(int i=1; i<=m; i++) {
            scanf("%d%d", &q[i].l, &q[i].r);
            q[i].id = i;  
        }
        sort(sub+1, sub+n+1);  //和下面的for循环一起,实现离散化
        int Size = unique(sub+1, sub+n+1) - (sub+1);  
        for(int i=1; i<=n; i++) {
            c[i] = upper_bound(sub+1, sub+Size+1, c[i]) - (sub+1);
        }
        sort(q+1, q+1+m, cmp);  //对询问排序
        solve();  //莫队
        sort(q+1, q+1+m, cmpId);  
        for(int i=1; i<=m; i++) {
            printf("%d\n",q[i].ans);
        }
    }
    return 0;
}
//下面是自己随便出的样例
/*
6 3
1 2 3 1 2 1
1 6
2 4
1 5

5 6
1 2 1 2 3
1 5
1 4
2 4
1 3
1 4
2 3
*/
 类似资料: