QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#334385#6626. Real Mountainsjames1BadCreeper3 1ms4080kbC++143.1kb2024-02-21 20:44:232024-02-21 20:44:24

Judging History

This is the latest submission verdict.

  • [2024-02-21 20:44:24]
  • Judged
  • Verdict: 3
  • Time: 1ms
  • Memory: 4080kb
  • [2024-02-21 20:44:23]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std; 
const int N = 1e6 + 5; 
const int P = 1e6 + 3; 
const int INF = 1e9 + 1; 

int S(int l, int r) { return 1ll * (l + r) * (r - l + 1) / 2 % P; }
int n, h[N], pre[N], suf[N];  
int C[N]; 
void add(int x, int k) { for (; x <= n; x += x & -x) C[x] += k; }
int sum(int x) { int r = 0; for (; x; x -= x & -x) r += C[x]; return r; }
int kth(int k) {
    int x = 0, s = 0; 
    for (int i = 19; i >= 0; --i) {
        x += 1 << i; 
        if (x >= n || s + C[x] >= k) x -= 1 << i; 
        else s += C[x]; 
    }
    return x + 1; 
}

int T[N * 4]; 
void build(int o, int l, int r) {
    if (l == r) return T[o] = h[l], void(); 
    int mid = l + r >> 1; build(o << 1, l, mid); build(o << 1 | 1, mid + 1, r); 
    T[o] = min(T[o << 1], T[o << 1 | 1]); 
}
void update(int o, int l, int r, int x) {
    if (l == r) return T[o] = INF, void(); 
    int mid = l + r >> 1; 
    if (x <= mid) update(o << 1, l, mid, x); 
    else update(o << 1 | 1, mid + 1, r, x); 
    T[o] = min(T[o << 1], T[o << 1 | 1]); 
}
int qmin(int o, int l, int r, int x, int y) {
    if (x > y) return 0; 
    if (x <= l && r <= y) return T[o]; 
    int mid = l + r >> 1, res = INF; 
    if (x <= mid) res = qmin(o << 1, l, mid, x, y); 
    if (mid < y) res = min(res, qmin(o << 1 | 1, mid + 1, r, x, y)); 
    return res; 
}

int main(void) {
    ios::sync_with_stdio(0); 
    cin >> n; 
    for (int i = 1; i <= n; ++i) cin >> h[i], pre[i] = max(pre[i - 1], h[i]); 
    for (int i = n; i >= 1; --i) suf[i] = max(suf[i + 1], h[i]); 
    build(1, 1, n); 

    vector<pair<int, int>> vec; 
    for (int i = 1; i <= n; ++i) vec.emplace_back(h[i], i); 

    sort(vec.begin(), vec.end()); int mx = vec.back().first; 
    int l = 1, r = n, ans = 0; 
    for (int op = 0; op < vec.size(); ++op) {
        int val = vec[op].first; 
        if (val == mx) break; 
        add(vec[op].second, 1); update(1, 1, n, vec[op].second); 
        if (val == vec[op + 1].first) continue; 

        while (pre[l] <= val) add(l++, -1); 
        while (suf[r] <= val) add(r--, -1); 
        int cnt = sum(n); 
        if (cnt == 0) continue; 
        if (cnt == 1) {
            int pos = kth(1); 
            int vl = qmin(1, 1, n, 1, pos - 1), vr = qmin(1, 1, n, pos + 1, n); 
            ans = (ans + 1ll * (vl + vr) * (vec[op + 1].first - val)) % P; 
            ans = (ans + S(val, vec[op + 1].first - 1)) % P; 
            continue; 
        }
        int x = kth(1), y = kth(cnt); 
        int vl = qmin(1, 1, n, 1, x - 1), vr = qmin(1, 1, n, y + 1, n); 

        int c = (0ll + vl + vr + min(vl, vr)) % P; // 
        ans = (ans + 1ll * c * (vec[op + 1].first - val)) % P; 
        c = cnt; // 每个自己
        ans = (ans + 1ll * c * S(val, vec[op + 1].first - 1)) % P; 
        c = (2 * (cnt - 2) + 1) % P; 
        // 中间对应的两边,两边的一个对应的一个,之前算过中间对应的两边和两边对应的一个是 +1 贡献的等差数列
        ans = (ans + 1ll * c * S(val + 1, vec[op + 1].first)) % P; 
    }
    cout << ans << '\n'; 
    return 0; 
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 3
Accepted

Test #1:

score: 3
Accepted
time: 1ms
memory: 3576kb

input:

3
29 9 9

output:

0

result:

ok 1 number(s): "0"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3608kb

input:

3
62 20 71

output:

7287

result:

ok 1 number(s): "7287"

Test #3:

score: 0
Accepted
time: 1ms
memory: 3636kb

input:

10
72 33 22 22 13 49 53 57 72 85

output:

40403

result:

ok 1 number(s): "40403"

Test #4:

score: 0
Accepted
time: 1ms
memory: 3808kb

input:

5000
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 97 97 97 97 9...

output:

481053

result:

ok 1 number(s): "481053"

Test #5:

score: 0
Accepted
time: 1ms
memory: 3848kb

input:

5000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

0

result:

ok 1 number(s): "0"

Test #6:

score: 0
Accepted
time: 1ms
memory: 3864kb

input:

5000
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2...

output:

12595

result:

ok 1 number(s): "12595"

Test #7:

score: 0
Accepted
time: 1ms
memory: 3784kb

input:

5000
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100...

output:

299

result:

ok 1 number(s): "299"

Test #8:

score: 0
Accepted
time: 1ms
memory: 4036kb

input:

5000
100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

224232

result:

ok 1 number(s): "224232"

Test #9:

score: 0
Accepted
time: 1ms
memory: 3812kb

input:

5000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4...

output:

0

result:

ok 1 number(s): "0"

Test #10:

score: 0
Accepted
time: 1ms
memory: 4016kb

input:

5000
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 9...

output:

0

result:

ok 1 number(s): "0"

Test #11:

score: 0
Accepted
time: 1ms
memory: 4076kb

input:

5000
100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4...

output:

499735

result:

ok 1 number(s): "499735"

Test #12:

score: 0
Accepted
time: 1ms
memory: 4036kb

input:

5000
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 9...

output:

461407

result:

ok 1 number(s): "461407"

Subtask #2:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Test #13:

score: 0
Wrong Answer
time: 0ms
memory: 4080kb

input:

5000
37 39 93 78 85 71 59 21 57 96 61 59 23 16 57 90 13 59 85 70 62 67 78 97 16 60 8 48 28 53 4 24 1 97 97 98 57 87 96 91 74 54 100 76 86 86 46 39 100 57 70 76 73 55 84 93 64 6 84 39 75 94 30 15 3 31 11 34 27 10 6 81 30 76 60 9 4 47 1 88 17 71 61 30 19 10 4 57 79 37 22 74 84 8 91 58 15 45 7 98 32 46...

output:

217081

result:

wrong answer 1st numbers differ - expected: '216624', found: '217081'

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Skipped

Dependency #2:

0%

Subtask #6:

score: 0
Skipped

Dependency #3:

0%

Subtask #7:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%