poj3368 rmq变形

    xiaoxiao2021-03-25  106

    You are given a sequence of n integers a1 , a2 , … , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integers ai , … , aj.

    Input The input consists of several test cases. Each test case starts with a line containing two integers n and q (1 ≤ n, q ≤ 100000). The next line contains n integers a1 , … , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, …, n}) separated by spaces. You can assume that for each i ∈ {1, …, n-1}: ai ≤ ai+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the query.

    The last test case is followed by a line containing a single 0.

    Output For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.

    Sample Input 10 3 -1 -1 1 1 1 1 3 10 10 10 2 3 1 10 5 10 0 Sample Output 1 4 3

    这个题给的是不降的那就很简单了 先把重复的给离散成一样的 记录个数 和位置以及对应重复的第几个 特判一下出结果

    #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<map> #include<cstring> using namespace std; int tu[100001],n,m,q,w,da= -0x7fffffff,f[100001][20],fx[50001][20],tt[100001]; map<int, int>mp,lp; int zu[100001]; void rmq(int c) { int cc = log2(c); for (int a = 1;a <= cc;a++) { for (int b = 1;b <= c;b++) { if (b + (1 << a) - 1 <= c) { f[b][a] = max(f[b][a - 1],f[b + (1 << (a - 1))][a - 1]); // fx[b][a] = min(fx[b][a - 1], fx[b + (1 << (a - 1))][a - 1]); } } } } int xunwenda(int zuo, int you) { int k = log2(you - zuo + 1); return max(f[zuo][k], f[you - (1 << k) + 1][k]); } int xunwenxiao(int zuo, int you) { int k = log2(you - zuo + 1); return min(fx[zuo][k], fx[you - (1 << k) + 1][k]); } int main() { while (cin >>n) { if (n == 0)break; mp.clear(); memset(f, 0, sizeof(f)); memset(tu, 0, sizeof(tu)); memset(zu, 0, sizeof(zu)); memset(tt, 0, sizeof(tt)); cin >> m; for (int a = 1;a <= n;a++)scanf("%d",&tu[a]); sort(tu + 1, tu + n + 1); for (int a = 1;a <= n;a++) { if (!mp[tu[a]])mp[tu[a]] = mp.size(); tu[a] = mp[tu[a]]; } for (int a = 1;a <= n;a++) { if (tu[a] != tu[a - 1]) { zu[a] = 1; tt[tu[a]] = 1; continue; } zu[a] = zu[a - 1] + 1; tt[tu[a]] = max(tt[tu[a]], zu[a]); } int chang = mp.size(); for (int a = 1;a <= chang;a++)f[a][0] = tt[a]; rmq(chang); for (int a = 1;a <= m;a++) { scanf("%d%d", &q, &w); if (tu[q] == tu[w]) { printf("%d\n",(zu[w] - zu[q]+1)); continue; } if (tu[w] == tu[q] + 1) { printf("%d\n", max(tt[tu[q]] - zu[q] + 1, zu[w])); continue; } printf("%d\n", max(max(tt[tu[q]] - zu[q] + 1, zu[w]), xunwenda(tu[q] + 1, tu[w] - 1))); } } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-10821.html

    最新回复(0)