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

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
*/
数据治理是确保数据准确性、可靠性、安全性、可用性和完整性的体系和框架。它定义了组织内部如何使用、存储、保护和共享数据的规则和流程。数据治理的重要性随着数字化转型的加速而日益凸显,它能够提高决策效率、增强业务竞争力、降低风险,并促进业务创新。有效的数据治理体系可以确保数据在采集、存储、处理、共享和保护等环节的合规性和有效性。 数据质量管理是数据治理中的关键环节,它涉及数据质量评估、数据清洗、标准化和监控。高质量的数据能够提升业务决策的准确性,优化业务流程,并挖掘潜在的商业价值。随着大数据和人工智能技术的发展,数据质量管理在确保数据准确性和可靠性方面的作用愈发重要。企业需要建立完善的数据质量管理和校验机制,并通过数据清洗和标准化提高数据质量。 数据安全与隐私保护是数据治理中的另一个重要领域。随着数据量的快速增长和互联网技术的迅速发展,数据安全与隐私保护面临前所未有的挑战。企业需要加强数据安全与隐私保护的法律法规和技术手段,采用数据加密、脱敏和备份恢复等技术手段,以及加强培训和教育,提高安全意识和技能水平。 数据流程管理与监控是确保数据质量、提高数据利用率、保护数据安全的重要环节。有效的数据流程管理可以确保数据流程的合规性和高效性,而实时监控则有助于及时发现并解决潜在问题。企业需要设计合理的数据流程架构,制定详细的数据管理流程规范,并运用数据审计和可视化技术手段进行监控。 数据资产管理是将数据视为组织的重要资产,通过有效的管理和利用,为组织带来经济价值。数据资产管理涵盖数据的整个生命周期,包括数据的创建、存储、处理、共享、使用和保护。它面临的挑战包括数据量的快速增长、数据类型的多样化和数据更新的迅速性。组织需要建立完善的数据管理体系,提高数据处理和分析能力,以应对这些挑战。同时,数据资产的分类与评估、共享与使用规范也是数据资产管理的重要组成部分,需要制定合理的标准和规范,确保数据共享的安全性和隐私保护,以及建立合理的利益分配和权益保障机制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值