Submission #330313

# Submission time Handle Problem Language Result Execution time Memory
330313 2020-11-24T18:11:51 Z jungsnow Pilot (NOI19_pilot) C++14
89 / 100
1000 ms 251296 KB
#include<bits/stdc++.h>
#define int long long
#define x first
#define y second

using namespace std;
using ll = long long;

typedef pair<int, int> ii;

const int N = 1000100;

int n, q, h[N], y[N];
int rmq[N][21], Log[N];
int p[N], s[N];

bool has[N];

ll res, query[N];

vector <ii> v, a;

int get(int l, int r) {
    int _lg = Log[r - l + 1];
    return max(rmq[l][_lg], rmq[r - (1 << _lg) + 1][_lg]);
}

void solve1() {
    for (int k = 1; k <= q; ++k) {
        int l = 0;
        int ans = 0;
        for (int i = 1; i <= n; ++i) {
            while (l < i && get(l + 1, i) > y[k]) ++l;
            ans += (i - l);
        }
        cout<<ans<<'\n';
    }
}

void solve2() {
    for (int k = 1; k <= q; ++k) {
        int lo = 1, hi = n, pos = 0;
        while (lo <= hi) {
            int mi = (lo + hi) >> 1;
            if (h[mi] <= y[k]) {
                pos = mi;
                lo = mi + 1;
            } else
                hi = mi - 1;
        }
        ll ans;
        ans = 1ll * pos * (pos - 1) / 2 + pos;
        cout<<ans<<'\n';
    }
}

void solve3() {
    ll ans = 0; int l = 0;
    for (int i = 1; i <= n; ++i) {
        while (l < i && get(l + 1, i) > y[1]) ++l;
        ans += (i - l);
    }
    cout<<ans<<'\n';
}

int anc(int u) {
    return p[u] == u ? u : p[u] = anc(p[u]);
}

void merge(int u, int v) {
    u = anc(u); v = anc(v);
    if (u == v) return;
    if (s[u] < s[v]) swap(u, v);
    p[v] = u;
    res += s[u] * s[v];
    s[u] += s[v];
}

void solve() {
    sort(v.begin(), v.end()); sort(a.begin(), a.end());
    int lst = 0;
    for (int i = 0; i < q; ++i) {
        int val = v[i].x, id = v[i].y;
        int lo = lst, hi = n - 1, pos = -1;
        while (lo <= hi) {
            int mi = (lo + hi) / 2;
            if (a[mi].x <= val) {
                pos = mi;
                lo = mi + 1;
            } else {
                hi = mi - 1;
            }
        }
        for (int j = lst; j <= pos; ++j) {
            int p = a[j].y;
            if (has[p]) continue;
            has[p] = 1;
            ++res;
            if (p > 1 && has[p - 1]) merge(p, p - 1);
            if (p < n && has[p + 1]) merge(p, p + 1);
        }
        lst = max(lst, pos + 1);
        query[id] = res;
    }
    for (int i = 1; i <= q; ++i) cout << query[i] << '\n';
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
//    freopen("pilot.in", "r", stdin);
//    freopen("pilot.out", "w", stdout);
    cin >> n >> q;
    bool ok = 1;
    for (int i = 1; i <= n; ++i) {
        cin >> h[i];
        a.push_back(ii(h[i], i));
        p[i] = i;
        s[i] = 1;
        rmq[i][0] = h[i];
    }
    for (int i = 2; i <= n; ++i) {
        if (h[i] <= h[i - 1]) ok = 0;
        Log[i] = Log[i / 2] + 1;
    }
    for (int j = 1; j <= 20; ++j) {
        for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
            rmq[i][j] = max(rmq[i][j - 1], rmq[i + (1 << j - 1)][j - 1]);
        }
    }
    for (int i = 1; i <= q; ++i) {
        cin >> y[i];
        v.push_back(ii(y[i], i));
    }
    if (max(n, q) <= 1000) solve1();
    else if (ok) solve2();
    else if (q == 1) solve3();
    else solve();
}

Compilation message

pilot.cpp: In function 'int main()':
pilot.cpp:128:60: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
  128 |             rmq[i][j] = max(rmq[i][j - 1], rmq[i + (1 << j - 1)][j - 1]);
      |                                                          ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 544 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 544 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 7 ms 620 KB Output is correct
22 Correct 10 ms 620 KB Output is correct
23 Correct 8 ms 620 KB Output is correct
24 Correct 10 ms 620 KB Output is correct
25 Correct 7 ms 620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 21476 KB Output is correct
2 Correct 40 ms 21964 KB Output is correct
3 Correct 37 ms 20836 KB Output is correct
4 Correct 37 ms 20196 KB Output is correct
5 Correct 37 ms 20580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 24932 KB Output is correct
2 Correct 67 ms 24804 KB Output is correct
3 Correct 77 ms 24420 KB Output is correct
4 Correct 67 ms 25444 KB Output is correct
5 Correct 66 ms 24292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 25444 KB Output is correct
2 Correct 72 ms 24880 KB Output is correct
3 Correct 71 ms 24420 KB Output is correct
4 Correct 74 ms 26340 KB Output is correct
5 Correct 73 ms 25064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 38 ms 21476 KB Output is correct
12 Correct 40 ms 21964 KB Output is correct
13 Correct 37 ms 20836 KB Output is correct
14 Correct 37 ms 20196 KB Output is correct
15 Correct 37 ms 20580 KB Output is correct
16 Correct 39 ms 20712 KB Output is correct
17 Correct 41 ms 21860 KB Output is correct
18 Correct 42 ms 22116 KB Output is correct
19 Correct 37 ms 20452 KB Output is correct
20 Correct 38 ms 21476 KB Output is correct
21 Correct 35 ms 20452 KB Output is correct
22 Correct 38 ms 20708 KB Output is correct
23 Correct 40 ms 22116 KB Output is correct
24 Correct 37 ms 20452 KB Output is correct
25 Correct 39 ms 21476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 544 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 7 ms 620 KB Output is correct
22 Correct 10 ms 620 KB Output is correct
23 Correct 8 ms 620 KB Output is correct
24 Correct 10 ms 620 KB Output is correct
25 Correct 7 ms 620 KB Output is correct
26 Correct 38 ms 21476 KB Output is correct
27 Correct 40 ms 21964 KB Output is correct
28 Correct 37 ms 20836 KB Output is correct
29 Correct 37 ms 20196 KB Output is correct
30 Correct 37 ms 20580 KB Output is correct
31 Correct 66 ms 24932 KB Output is correct
32 Correct 67 ms 24804 KB Output is correct
33 Correct 77 ms 24420 KB Output is correct
34 Correct 67 ms 25444 KB Output is correct
35 Correct 66 ms 24292 KB Output is correct
36 Correct 71 ms 25444 KB Output is correct
37 Correct 72 ms 24880 KB Output is correct
38 Correct 71 ms 24420 KB Output is correct
39 Correct 74 ms 26340 KB Output is correct
40 Correct 73 ms 25064 KB Output is correct
41 Correct 39 ms 20712 KB Output is correct
42 Correct 41 ms 21860 KB Output is correct
43 Correct 42 ms 22116 KB Output is correct
44 Correct 37 ms 20452 KB Output is correct
45 Correct 38 ms 21476 KB Output is correct
46 Correct 35 ms 20452 KB Output is correct
47 Correct 38 ms 20708 KB Output is correct
48 Correct 40 ms 22116 KB Output is correct
49 Correct 37 ms 20452 KB Output is correct
50 Correct 39 ms 21476 KB Output is correct
51 Correct 86 ms 25908 KB Output is correct
52 Correct 85 ms 25700 KB Output is correct
53 Correct 85 ms 26212 KB Output is correct
54 Correct 83 ms 25444 KB Output is correct
55 Correct 85 ms 26340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 544 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 7 ms 620 KB Output is correct
22 Correct 10 ms 620 KB Output is correct
23 Correct 8 ms 620 KB Output is correct
24 Correct 10 ms 620 KB Output is correct
25 Correct 7 ms 620 KB Output is correct
26 Correct 38 ms 21476 KB Output is correct
27 Correct 40 ms 21964 KB Output is correct
28 Correct 37 ms 20836 KB Output is correct
29 Correct 37 ms 20196 KB Output is correct
30 Correct 37 ms 20580 KB Output is correct
31 Correct 66 ms 24932 KB Output is correct
32 Correct 67 ms 24804 KB Output is correct
33 Correct 77 ms 24420 KB Output is correct
34 Correct 67 ms 25444 KB Output is correct
35 Correct 66 ms 24292 KB Output is correct
36 Correct 71 ms 25444 KB Output is correct
37 Correct 72 ms 24880 KB Output is correct
38 Correct 71 ms 24420 KB Output is correct
39 Correct 74 ms 26340 KB Output is correct
40 Correct 73 ms 25064 KB Output is correct
41 Correct 39 ms 20712 KB Output is correct
42 Correct 41 ms 21860 KB Output is correct
43 Correct 42 ms 22116 KB Output is correct
44 Correct 37 ms 20452 KB Output is correct
45 Correct 38 ms 21476 KB Output is correct
46 Correct 35 ms 20452 KB Output is correct
47 Correct 38 ms 20708 KB Output is correct
48 Correct 40 ms 22116 KB Output is correct
49 Correct 37 ms 20452 KB Output is correct
50 Correct 39 ms 21476 KB Output is correct
51 Correct 86 ms 25908 KB Output is correct
52 Correct 85 ms 25700 KB Output is correct
53 Correct 85 ms 26212 KB Output is correct
54 Correct 83 ms 25444 KB Output is correct
55 Correct 85 ms 26340 KB Output is correct
56 Execution timed out 1014 ms 251296 KB Time limit exceeded
57 Halted 0 ms 0 KB -