不知道分块错哪了,求调,悬一关

P3793 由乃救爷爷

Stevehim @ 2023-07-24 10:16:04

rt,样例能过qaq

#include <bits/stdc++.h>
#define maxn 20000010
#define ll long long
using namespace std;
namespace GenHelper {
    unsigned z1, z2, z3, z4, b;
    unsigned rand_() {
        b = ((z1 << 6)^z1) >> 13;
        z1 = ((z1 & 4294967294U) << 18)^b;
        b = ((z2 << 2)^z2) >> 27;
        z2 = ((z2 & 4294967288U) << 2)^b;
        b = ((z3 << 13)^z3) >> 21;
        z3 = ((z3 & 4294967280U) << 7)^b;
        b = ((z4 << 3)^z4) >> 12;
        z4 = ((z4 & 4294967168U) << 13)^b;
        return (z1 ^ z2 ^ z3 ^ z4);
    }
}
void srand(unsigned x) {
    using namespace GenHelper;
    z1 = x;
    z2 = (~x) ^ 0x233333333U;
    z3 = x ^ 0x1234598766U;
    z4 = (~x) + 51;
}
int read() {
    using namespace GenHelper;
    int a = rand_() & 32767;
    int b = rand_() & 32767;
    return a * 32768 + b;
}
ll _read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
int n, blo;
int bl[5000]; //最多只有4400+个块
ll v[maxn];
vector<ll> ve[5000]; //用来排序
unsigned ll sum = 0;
ll query(int l, int r) {
    ll ans = 0;
    for (int i = l; i <= min(bl[l] * blo, r); i++) {
        ans = max(ans, v[i]);
    }
    if (bl[l] != bl[r]) {
        for (int i = (bl[r] - 1) * blo + 1; i <= r; i++) {
            ans = max(ans, v[i]);
        }
    }
    for (int i = bl[l] + 1; i <= bl[r] - 1; i++) {
        ans = max(ans, ve[i][ve[i].size() - 1]); //取出最大子
    }
    return ans;
}
int m,s;
int main() {
    n = _read();blo = sqrt(n);
    cout << blo << endl;
    m = _read(),s =_read();
    srand(s);
    for(int i = 1; i <= n; i++){
        v[i] =read();
    }
    for(int i = 1; i <= n; i++){
        bl[i] = (i - 1) / blo + 1;
        ve[bl[i]].push_back(v[i]);
    }
    for(int i = 1; i <= bl[n]; i++)
        sort(ve[i].begin(),ve[i].end());
    for(int i = 1; i <= m; i++){
        int l = read() % n + 1,r = read() % n + 1;
        if(l > r) swap(l,r);
        sum += query(l,r);
    }
    cout << sum;
    return 0;
}

|