标签:mat 最大的 端点 mem pre char lin dig for
题意:给出一个不降序列,有多个询问,询问[l,r]中出现次数最多的数的出现次数
对于序列-1 -1 1 1 1 1 3 10 10 10
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
inline void chmax(int &x, int y) {if (x < y) x = y;}
inline void chmin(int &x, int y) {if (x > y) x = y;}
const int MAXN = 1e5+7;
int n, m, st[21][MAXN], a[MAXN], len[MAXN], belong[MAXN], lef[MAXN], righ[MAXN], tot, cnt[MAXN], Pow[21], logg[MAXN];
inline int read() {
int c = getchar(), f = 1, x = 0;
while(!isdigit(c)) (c=='-')&&(f=-f), c = getchar();
while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
return x * f;
}
inline int RMQ(int l, int r) {
int k = logg[r-l+1];
return max(st[k][l], st[k][r-Pow[k]+1]);
}
int main(void) {
for(int i = 2; i <= 100000; ++i) logg[i] = logg[i>>1] + 1;
for(int i = 0; i <= 20; ++i) Pow[i] = (1<<i);
while((n = read())) {
tot = 0; memset(cnt, 0, n*4);
m = read();
for(int i = 1; i <= n; ++i) a[i] = read();
for(int i = 1; i <= n; ++i) {
if (a[i] != a[i-1]) lef[i] = i, belong[i] = ++tot;
else lef[i] = lef[i-1], belong[i] = tot;
++cnt[tot];
}
for(int i = n; i >= 1; --i) {
if (a[i] != a[i+1]) righ[i] = i;
else righ[i] = righ[i+1];
}
for(int i = 1; i <= tot; ++i) st[0][i] = cnt[i];
// for(int i = 1; i <= n; ++i) cout << lef[i] << " \n"[i==n];
// for(int i = 1; i <= n; ++i) cout << righ[i]<< " \n"[i==n];
// for(int i = 1; i <= n; ++i) cout << belong[i]<<" \n"[i==n];
// for(int i = 1; i <= tot; ++i) cout << cnt[i] << " \n"[i==n];
int mm = logg[n];
for(int i = 1; i <= mm; ++i)
for(int j = 1; j <= tot; ++j)
st[i][j] = max(st[i-1][j], st[i-1][j+Pow[i-1]]);
while(m--) {
int l = read(), r = read();
if (belong[l] == belong[r]) printf("%d\n", r-l+1);
else {
int ans = min(r, righ[l]) - l + 1;
chmax(ans, r - max(l, lef[r]) + 1);
if (belong[l] + 1 <= belong[r] - 1) chmax(ans, RMQ(belong[l]+1, belong[r] - 1));
printf("%d\n", ans);
}
}
}
return 0;
}
[POJ3368][UVA11235] Frequent values[ST表]
标签:mat 最大的 端点 mem pre char lin dig for
原文地址:https://www.cnblogs.com/storz/p/10191551.html