bzoj4241 historical research block

Topic Portal

https://lydsy.com/JudgeOnline/problem.php?id=4241

answer

This question is out of the required number with the right to a public range.

Recall how the general public seeking Interval:

Block, each prompt \ (l, r \) , if the \ (l, r \) not in the same block, then the answer must be in the middle piece of the mode, or both sides of the remaining number.

This pretreatment can be a thing \ (f [i] [j ] \) represents from \ (I \) block to the \ (J \) a mode would be a single \ (\ sqrt n \) to obtain a.

For the remaining number of sides, the number may be pretreated sequence occurs each position of each value, the number of occurrences of each number obtained in the above two points.


You can find the above conclusions can still be used with a number of public weights.

However, solving the problem of the large range of data that have \ (10 ^ 5 \) , it is necessary to \ (O (1) \) the number of occurrences determined in a certain number of full segment. Because the whole, i.e. the beginning to the end block are included, and it may be pretreated a prefix \ (s [i] [j ] \) represents the former \ (I \) block \ (J \) of occurrences can order out.


Sad, \ (O ((n-Q +) \ n-sqrt) \) is \ (O ((n + q ) \ sqrt n \ log n) \) beating and hanging.

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
    int f = 0, c;
    while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    x = c & 15;
    while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    f ? x = -x : 0;
}

const int N = 1e5 + 7;
const int B = 316 + 7;

#define bl(x) (((x) - 1) / blo + 1)
#define st(x) (((x) - 1) * blo + 1)
#define ed(x) std::min((x) * blo, n)

int n, dis, Q, blo;
int a[N], b[N], s[B][N], cnt[N];
ll f[B][N];

inline void ycl() {
    for (int i = 1; i <= bl(n); ++i)
        for (int j = st(i); j <= ed(i); ++j) ++s[i][a[j]];
    for (int i = 1; i <= bl(n); ++i)
        for (int j = 1; j <= dis; ++j) s[i][j] += s[i - 1][j];
    for (int i = 1; i <= bl(n); ++i) {
        memset(cnt, 0, sizeof(int) * (dis + 1));
        int x = st(i);
        ll mx = 0;
        for (int j = x; j <= n; ++j) {
            smax(mx, (ll)b[a[j]] * ++cnt[a[j]]);
            f[i][j] = mx;
        }
    }
    memset(cnt, 0, sizeof(int) * (dis + 1));
}

inline ll qry(int l, int r) {
    ll ans = 0;
    if (bl(l) == bl(r)) {
        for (int i = l; i <= r; ++i) smax(ans, (ll)b[a[i]] * ++cnt[a[i]]);
        for (int i = l; i <= r; ++i) cnt[a[i]] = 0;
        return ans;
    }
    ans = f[bl(l) + 1][r];
    int sl = bl(l) + 1, sr = bl(r) - 1;
    for (int i = l; i <= ed(bl(l)); ++i) smax(ans, (ll)b[a[i]] * (++cnt[a[i]] + s[sr][a[i]] - s[sl - 1][a[i]]));
    for (int i = st(bl(r)); i <= r; ++i) smax(ans, (ll)b[a[i]] * (++cnt[a[i]] + s[sr][a[i]] - s[sl - 1][a[i]]));
    for (int i = l; i <= ed(bl(l)); ++i) cnt[a[i]] = 0;
    for (int i = st(bl(r)); i <= r; ++i) cnt[a[i]] = 0;
    return ans;
}

inline void lsh() {
    std::sort(b + 1, b + n + 1);
    dis = std::unique(b + 1, b + n + 1) - b - 1;
    for (int i = 1; i <= n; ++i) a[i] = std::lower_bound(b + 1, b + dis + 1, a[i]) - b;
}

inline void work() {
    lsh();
    ycl();
    while (Q--) {
        int l, r;
        read(l), read(r);
        printf("%lld\n", qry(l, r));
    }
}

inline void init() {
    read(n), read(Q);
    blo = sqrt(n);
    for (int i = 1; i <= n; ++i) read(a[i]), b[i] = a[i];
}

int main() {
#ifdef hzhkk
    freopen("hkk.in", "r", stdin);
#endif
    init();
    work();
    fclose(stdin), fclose(stdout);
    return 0;
}

Guess you like

Origin www.cnblogs.com/hankeke/p/bzoj4241.html