Submission #971375

# Submission time Handle Problem Language Result Execution time Memory
971375 2024-04-28T12:17:13 Z Wael Real Mountains (CCO23_day1problem2) C++17
16 / 25
5000 ms 165512 KB
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int const mod = 1e6 + 3;
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());

int mul(long long x, long long y) {
    while(x >= mod) x -= mod;
    while(y >= mod) y -= mod;
    return (x * y) % mod;
}

int add(long long x, long long y) {
    while(x >= mod) x -= mod;
    while(y >= mod) y -= mod;
    return (x + y) % mod;
}

int sub(long long x, long long y) {
    while(x >= mod) x -= mod;
    while(y >= mod) y -= mod;
    return (x - y + mod) % mod;
}

int power(long long x, long long y) {
    if(y == 0) return 1;
    if(y == 1) return x;
    int ret = power(x, y / 2);
    ret = mul(ret, ret);
    if(y % 2) ret = mul(ret, x);
    return ret;
}

int inv(long long x) {
    return power(x, mod - 2);
}

int divide(long long x, long long y) {
    while(x >= mod) x -= mod;
    while(y >= mod) y -= mod;
    return mul(x, inv(y));
}

int tree[4000006];
void update(int nod, int l, int r, int x, int val) {
    if (x < l || r < x) return;
    if (l == r) {
        tree[nod] = val;
        return;
    }
    int mid = (l + r) / 2;
    update(nod * 2, l, mid, x, val);
    update(nod * 2 + 1, mid + 1, r, x, val);
    tree[nod] = min(tree[nod * 2], tree[nod * 2 + 1]);
}

int get(int nod, int l, int r, int x, int y) { 
    if (r < x || y < l) return 1e9 + 1;
    if (x <= l && r <= y) return tree[nod];
    int mid = (l + r) / 2;
    return min(get(nod * 2, l, mid, x, y), get(nod * 2 + 1, mid + 1, r, x, y));
}

void solve() { 
    int n = 1e6;
    cin >> n;
    
    vector<int> h(n);
    map<int, vector<int>> indices;
    for(int i = 0; i < n; ++i) {
        h[i] = RNG() % (int)1e9 + 1;
        cin >> h[i];
        indices[h[i]].push_back(i);
        update(1, 0, n - 1, i, h[i]);
    }

    set<int> have, rem, all;
    for(int i = 0; i < n; ++i)  {
        rem.insert(i);
        all.insert(h[i]);
    }

    auto sum = [&](int l, int r) {
        return (1LL * (l + r) * (r - l + 1) / 2) % mod;
    };

    long long ans = 0;
    for(auto i : all) {
        for(auto j : indices[i]) {
            have.insert(j);
            rem.erase(j);
            update(1, 0, n - 1, j, 1e9 + 1);
        }
        if(rem.size() == 0) break;
        if(have.size() == 0) continue;
      
        while(have.size() && *have.begin() < *rem.begin())
            have.erase(have.begin());
        while(have.size() && *prev(have.end()) > *prev(rem.end()))
            have.erase(prev(have.end()));

        if(have.size() == 0) continue;
        int l = *have.begin(), r = *prev(have.end());
        int z1 = get(1, 0, n - 1, 0, l - 1);
        int z2 = get(1, 0, n - 1, r + 1, n - 1);
        int x = min({get(1, 0, n - 1, l, r), z1, z2});
        auto it = all.upper_bound(i);
        int dif = *it - i;
        if(l == r) {
            ans = add(ans, mul(z1 + z2, dif));
        }
        else {
            ans = add(ans, mul(z1 + z2, dif));
            ans = add(ans, mul(x, dif));
            ans = add(ans, sum(i + 1, i + dif));
            ans = add(ans, mul(mul((have.size() - 2), sum(i + 1, i + dif)), 2));
        }        
        ans = add(ans, mul(have.size(), sum(i, i + dif - 1)));
    }

    cout << ans << endl;
    return;
}   

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int t = 1;
    //cin >> t;
    while(t--) {
        solve();
    }

    return 0;
}

Compilation message

Main.cpp:126:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  126 | main() {
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 3 ms 604 KB Output is correct
6 Correct 3 ms 832 KB Output is correct
7 Correct 3 ms 600 KB Output is correct
8 Correct 3 ms 604 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 3 ms 604 KB Output is correct
11 Correct 3 ms 604 KB Output is correct
12 Correct 3 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 3 ms 604 KB Output is correct
6 Correct 3 ms 832 KB Output is correct
7 Correct 3 ms 600 KB Output is correct
8 Correct 3 ms 604 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 3 ms 604 KB Output is correct
11 Correct 3 ms 604 KB Output is correct
12 Correct 3 ms 604 KB Output is correct
13 Correct 4 ms 604 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 604 KB Output is correct
17 Correct 4 ms 604 KB Output is correct
18 Correct 4 ms 604 KB Output is correct
19 Correct 4 ms 604 KB Output is correct
20 Correct 3 ms 604 KB Output is correct
21 Correct 3 ms 600 KB Output is correct
22 Correct 3 ms 604 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 3 ms 604 KB Output is correct
25 Correct 3 ms 604 KB Output is correct
26 Correct 3 ms 604 KB Output is correct
27 Correct 3 ms 684 KB Output is correct
28 Correct 3 ms 844 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 3 ms 604 KB Output is correct
6 Correct 3 ms 832 KB Output is correct
7 Correct 3 ms 600 KB Output is correct
8 Correct 3 ms 604 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 3 ms 604 KB Output is correct
11 Correct 3 ms 604 KB Output is correct
12 Correct 3 ms 604 KB Output is correct
13 Correct 4 ms 604 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 604 KB Output is correct
17 Correct 4 ms 604 KB Output is correct
18 Correct 4 ms 604 KB Output is correct
19 Correct 4 ms 604 KB Output is correct
20 Correct 3 ms 604 KB Output is correct
21 Correct 3 ms 600 KB Output is correct
22 Correct 3 ms 604 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 3 ms 604 KB Output is correct
25 Correct 3 ms 604 KB Output is correct
26 Correct 3 ms 604 KB Output is correct
27 Correct 3 ms 684 KB Output is correct
28 Correct 3 ms 844 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 8 ms 1376 KB Output is correct
36 Correct 10 ms 1628 KB Output is correct
37 Correct 7 ms 1504 KB Output is correct
38 Correct 7 ms 1604 KB Output is correct
39 Correct 7 ms 1372 KB Output is correct
40 Correct 3 ms 604 KB Output is correct
41 Correct 3 ms 604 KB Output is correct
42 Correct 3 ms 604 KB Output is correct
43 Correct 6 ms 1368 KB Output is correct
44 Correct 7 ms 1372 KB Output is correct
45 Correct 6 ms 1372 KB Output is correct
46 Correct 8 ms 1372 KB Output is correct
47 Correct 7 ms 1368 KB Output is correct
48 Correct 7 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 3 ms 604 KB Output is correct
6 Correct 3 ms 832 KB Output is correct
7 Correct 3 ms 600 KB Output is correct
8 Correct 3 ms 604 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 3 ms 604 KB Output is correct
11 Correct 3 ms 604 KB Output is correct
12 Correct 3 ms 604 KB Output is correct
13 Correct 4 ms 604 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 604 KB Output is correct
17 Correct 4 ms 604 KB Output is correct
18 Correct 4 ms 604 KB Output is correct
19 Correct 4 ms 604 KB Output is correct
20 Correct 3 ms 604 KB Output is correct
21 Correct 3 ms 600 KB Output is correct
22 Correct 3 ms 604 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 3 ms 604 KB Output is correct
25 Correct 3 ms 604 KB Output is correct
26 Correct 3 ms 604 KB Output is correct
27 Correct 3 ms 684 KB Output is correct
28 Correct 3 ms 844 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 8 ms 1376 KB Output is correct
36 Correct 10 ms 1628 KB Output is correct
37 Correct 7 ms 1504 KB Output is correct
38 Correct 7 ms 1604 KB Output is correct
39 Correct 7 ms 1372 KB Output is correct
40 Correct 3 ms 604 KB Output is correct
41 Correct 3 ms 604 KB Output is correct
42 Correct 3 ms 604 KB Output is correct
43 Correct 6 ms 1368 KB Output is correct
44 Correct 7 ms 1372 KB Output is correct
45 Correct 6 ms 1372 KB Output is correct
46 Correct 8 ms 1372 KB Output is correct
47 Correct 7 ms 1368 KB Output is correct
48 Correct 7 ms 1372 KB Output is correct
49 Correct 12 ms 1624 KB Output is correct
50 Correct 11 ms 1372 KB Output is correct
51 Correct 12 ms 1628 KB Output is correct
52 Correct 11 ms 1624 KB Output is correct
53 Correct 11 ms 1624 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 3 ms 844 KB Output is correct
56 Correct 3 ms 600 KB Output is correct
57 Correct 10 ms 1628 KB Output is correct
58 Correct 10 ms 1372 KB Output is correct
59 Correct 10 ms 1372 KB Output is correct
60 Correct 16 ms 1504 KB Output is correct
61 Correct 13 ms 1624 KB Output is correct
62 Correct 11 ms 1584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 3 ms 604 KB Output is correct
6 Correct 3 ms 832 KB Output is correct
7 Correct 3 ms 600 KB Output is correct
8 Correct 3 ms 604 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 3 ms 604 KB Output is correct
11 Correct 3 ms 604 KB Output is correct
12 Correct 3 ms 604 KB Output is correct
13 Correct 4 ms 604 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 604 KB Output is correct
17 Correct 4 ms 604 KB Output is correct
18 Correct 4 ms 604 KB Output is correct
19 Correct 4 ms 604 KB Output is correct
20 Correct 3 ms 604 KB Output is correct
21 Correct 3 ms 600 KB Output is correct
22 Correct 3 ms 604 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 3 ms 604 KB Output is correct
25 Correct 3 ms 604 KB Output is correct
26 Correct 3 ms 604 KB Output is correct
27 Correct 3 ms 684 KB Output is correct
28 Correct 3 ms 844 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1882 ms 67924 KB Output is correct
36 Correct 1819 ms 67864 KB Output is correct
37 Correct 1842 ms 67924 KB Output is correct
38 Correct 1885 ms 67904 KB Output is correct
39 Correct 1884 ms 67972 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 925 ms 65852 KB Output is correct
43 Correct 905 ms 65764 KB Output is correct
44 Correct 887 ms 65732 KB Output is correct
45 Correct 804 ms 68000 KB Output is correct
46 Correct 782 ms 67824 KB Output is correct
47 Correct 750 ms 67820 KB Output is correct
48 Correct 984 ms 67948 KB Output is correct
49 Correct 962 ms 67912 KB Output is correct
50 Correct 1014 ms 67824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 3 ms 604 KB Output is correct
6 Correct 3 ms 832 KB Output is correct
7 Correct 3 ms 600 KB Output is correct
8 Correct 3 ms 604 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 3 ms 604 KB Output is correct
11 Correct 3 ms 604 KB Output is correct
12 Correct 3 ms 604 KB Output is correct
13 Correct 4 ms 604 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 604 KB Output is correct
17 Correct 4 ms 604 KB Output is correct
18 Correct 4 ms 604 KB Output is correct
19 Correct 4 ms 604 KB Output is correct
20 Correct 3 ms 604 KB Output is correct
21 Correct 3 ms 600 KB Output is correct
22 Correct 3 ms 604 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 3 ms 604 KB Output is correct
25 Correct 3 ms 604 KB Output is correct
26 Correct 3 ms 604 KB Output is correct
27 Correct 3 ms 684 KB Output is correct
28 Correct 3 ms 844 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 8 ms 1376 KB Output is correct
36 Correct 10 ms 1628 KB Output is correct
37 Correct 7 ms 1504 KB Output is correct
38 Correct 7 ms 1604 KB Output is correct
39 Correct 7 ms 1372 KB Output is correct
40 Correct 3 ms 604 KB Output is correct
41 Correct 3 ms 604 KB Output is correct
42 Correct 3 ms 604 KB Output is correct
43 Correct 6 ms 1368 KB Output is correct
44 Correct 7 ms 1372 KB Output is correct
45 Correct 6 ms 1372 KB Output is correct
46 Correct 8 ms 1372 KB Output is correct
47 Correct 7 ms 1368 KB Output is correct
48 Correct 7 ms 1372 KB Output is correct
49 Correct 1882 ms 67924 KB Output is correct
50 Correct 1819 ms 67864 KB Output is correct
51 Correct 1842 ms 67924 KB Output is correct
52 Correct 1885 ms 67904 KB Output is correct
53 Correct 1884 ms 67972 KB Output is correct
54 Correct 1 ms 344 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 925 ms 65852 KB Output is correct
57 Correct 905 ms 65764 KB Output is correct
58 Correct 887 ms 65732 KB Output is correct
59 Correct 804 ms 68000 KB Output is correct
60 Correct 782 ms 67824 KB Output is correct
61 Correct 750 ms 67820 KB Output is correct
62 Correct 984 ms 67948 KB Output is correct
63 Correct 962 ms 67912 KB Output is correct
64 Correct 1014 ms 67824 KB Output is correct
65 Execution timed out 5104 ms 165512 KB Time limit exceeded
66 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 3 ms 604 KB Output is correct
6 Correct 3 ms 832 KB Output is correct
7 Correct 3 ms 600 KB Output is correct
8 Correct 3 ms 604 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 3 ms 604 KB Output is correct
11 Correct 3 ms 604 KB Output is correct
12 Correct 3 ms 604 KB Output is correct
13 Correct 4 ms 604 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 604 KB Output is correct
17 Correct 4 ms 604 KB Output is correct
18 Correct 4 ms 604 KB Output is correct
19 Correct 4 ms 604 KB Output is correct
20 Correct 3 ms 604 KB Output is correct
21 Correct 3 ms 600 KB Output is correct
22 Correct 3 ms 604 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 3 ms 604 KB Output is correct
25 Correct 3 ms 604 KB Output is correct
26 Correct 3 ms 604 KB Output is correct
27 Correct 3 ms 684 KB Output is correct
28 Correct 3 ms 844 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 8 ms 1376 KB Output is correct
36 Correct 10 ms 1628 KB Output is correct
37 Correct 7 ms 1504 KB Output is correct
38 Correct 7 ms 1604 KB Output is correct
39 Correct 7 ms 1372 KB Output is correct
40 Correct 3 ms 604 KB Output is correct
41 Correct 3 ms 604 KB Output is correct
42 Correct 3 ms 604 KB Output is correct
43 Correct 6 ms 1368 KB Output is correct
44 Correct 7 ms 1372 KB Output is correct
45 Correct 6 ms 1372 KB Output is correct
46 Correct 8 ms 1372 KB Output is correct
47 Correct 7 ms 1368 KB Output is correct
48 Correct 7 ms 1372 KB Output is correct
49 Correct 12 ms 1624 KB Output is correct
50 Correct 11 ms 1372 KB Output is correct
51 Correct 12 ms 1628 KB Output is correct
52 Correct 11 ms 1624 KB Output is correct
53 Correct 11 ms 1624 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 3 ms 844 KB Output is correct
56 Correct 3 ms 600 KB Output is correct
57 Correct 10 ms 1628 KB Output is correct
58 Correct 10 ms 1372 KB Output is correct
59 Correct 10 ms 1372 KB Output is correct
60 Correct 16 ms 1504 KB Output is correct
61 Correct 13 ms 1624 KB Output is correct
62 Correct 11 ms 1584 KB Output is correct
63 Correct 1882 ms 67924 KB Output is correct
64 Correct 1819 ms 67864 KB Output is correct
65 Correct 1842 ms 67924 KB Output is correct
66 Correct 1885 ms 67904 KB Output is correct
67 Correct 1884 ms 67972 KB Output is correct
68 Correct 1 ms 344 KB Output is correct
69 Correct 1 ms 348 KB Output is correct
70 Correct 925 ms 65852 KB Output is correct
71 Correct 905 ms 65764 KB Output is correct
72 Correct 887 ms 65732 KB Output is correct
73 Correct 804 ms 68000 KB Output is correct
74 Correct 782 ms 67824 KB Output is correct
75 Correct 750 ms 67820 KB Output is correct
76 Correct 984 ms 67948 KB Output is correct
77 Correct 962 ms 67912 KB Output is correct
78 Correct 1014 ms 67824 KB Output is correct
79 Execution timed out 5104 ms 165512 KB Time limit exceeded
80 Halted 0 ms 0 KB -