Submission #481311

# Submission time Handle Problem Language Result Execution time Memory
481311 2021-10-20T11:01:55 Z vishesh312 Pilot (NOI19_pilot) C++17
100 / 100
627 ms 52200 KB
#include "bits/stdc++.h"
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
*/

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sz(x) (int)(x).size()

using ll = long long;
const int mod = 1e9+7;

struct UFDS {
    vector<int> link, siz;
    UFDS (int n) {
        siz.resize(n, 1);
        link.resize(n);
        iota(all(link), 0);
    }
    int find(int x) {
        if (x == link[x]) {
            return x;
        }
        return link[x] = find(link[x]);
    }
    ll ans(int x) {
        ll ret = siz[find(x)];
        return (ret*(ret+1))/2;
    }
    void unite(int a, int b) {
        a = find(a), b = find(b);
        if (siz[a] < siz[b])
            swap(a, b);
        siz[a] += siz[b];
        link[b] = a;
    }
};

void solve(int tc) {
    int n, q;
    cin >> n >> q;
    vector<pair<int, int>> h(n), y(q);
    vector<bool> used(n);
    vector<ll> ans(q);
    UFDS uf(n);
    for (int i = 0; i < n; ++i) {
        cin >> h[i].first;
        h[i].second = i;
    }
    for (int i = 0; i < q; ++i) {
        cin >> y[i].first;
        y[i].second = i;
    }
    sort(all(h)); sort(all(y));
    int p = 0;
    ll cur = 0;
    for (int i = 0; i < q; ++i) {
        while (p < n and h[p].first <= y[i].first) {
            if (h[p].second+1 < n and used[h[p].second+1]) {
                cur -= uf.ans(h[p].second+1);
                uf.unite(h[p].second, h[p].second+1);
            }
            if (h[p].second-1 >= 0 and used[h[p].second-1]) {
                cur -= uf.ans(h[p].second-1);
                uf.unite(h[p].second, h[p].second-1);
            }
            cur += uf.ans(h[p].second);
            used[h[p].second] = true;
            ++p;
        }
        ans[y[i].second] = cur;
    }
    for (auto x : ans)
        cout << x << '\n';
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int tc = 1;
    //cin >> tc;
    for (int i = 1; i <= tc; ++i) solve(i);
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 324 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 328 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 2380 KB Output is correct
2 Correct 23 ms 2496 KB Output is correct
3 Correct 24 ms 2304 KB Output is correct
4 Correct 20 ms 2252 KB Output is correct
5 Correct 24 ms 2372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 5244 KB Output is correct
2 Correct 45 ms 5432 KB Output is correct
3 Correct 39 ms 5268 KB Output is correct
4 Correct 42 ms 5352 KB Output is correct
5 Correct 42 ms 5220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 5496 KB Output is correct
2 Correct 51 ms 5396 KB Output is correct
3 Correct 46 ms 5440 KB Output is correct
4 Correct 40 ms 5436 KB Output is correct
5 Correct 41 ms 5588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 23 ms 2380 KB Output is correct
12 Correct 23 ms 2496 KB Output is correct
13 Correct 24 ms 2304 KB Output is correct
14 Correct 20 ms 2252 KB Output is correct
15 Correct 24 ms 2372 KB Output is correct
16 Correct 25 ms 2292 KB Output is correct
17 Correct 18 ms 2536 KB Output is correct
18 Correct 20 ms 2508 KB Output is correct
19 Correct 19 ms 2252 KB Output is correct
20 Correct 19 ms 2368 KB Output is correct
21 Correct 29 ms 2268 KB Output is correct
22 Correct 27 ms 2380 KB Output is correct
23 Correct 22 ms 2388 KB Output is correct
24 Correct 17 ms 2380 KB Output is correct
25 Correct 23 ms 2380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 324 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 328 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 23 ms 2380 KB Output is correct
27 Correct 23 ms 2496 KB Output is correct
28 Correct 24 ms 2304 KB Output is correct
29 Correct 20 ms 2252 KB Output is correct
30 Correct 24 ms 2372 KB Output is correct
31 Correct 41 ms 5244 KB Output is correct
32 Correct 45 ms 5432 KB Output is correct
33 Correct 39 ms 5268 KB Output is correct
34 Correct 42 ms 5352 KB Output is correct
35 Correct 42 ms 5220 KB Output is correct
36 Correct 42 ms 5496 KB Output is correct
37 Correct 51 ms 5396 KB Output is correct
38 Correct 46 ms 5440 KB Output is correct
39 Correct 40 ms 5436 KB Output is correct
40 Correct 41 ms 5588 KB Output is correct
41 Correct 25 ms 2292 KB Output is correct
42 Correct 18 ms 2536 KB Output is correct
43 Correct 20 ms 2508 KB Output is correct
44 Correct 19 ms 2252 KB Output is correct
45 Correct 19 ms 2368 KB Output is correct
46 Correct 29 ms 2268 KB Output is correct
47 Correct 27 ms 2380 KB Output is correct
48 Correct 22 ms 2388 KB Output is correct
49 Correct 17 ms 2380 KB Output is correct
50 Correct 23 ms 2380 KB Output is correct
51 Correct 55 ms 5252 KB Output is correct
52 Correct 66 ms 5252 KB Output is correct
53 Correct 53 ms 5188 KB Output is correct
54 Correct 52 ms 5104 KB Output is correct
55 Correct 50 ms 5188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 324 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 328 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 23 ms 2380 KB Output is correct
27 Correct 23 ms 2496 KB Output is correct
28 Correct 24 ms 2304 KB Output is correct
29 Correct 20 ms 2252 KB Output is correct
30 Correct 24 ms 2372 KB Output is correct
31 Correct 41 ms 5244 KB Output is correct
32 Correct 45 ms 5432 KB Output is correct
33 Correct 39 ms 5268 KB Output is correct
34 Correct 42 ms 5352 KB Output is correct
35 Correct 42 ms 5220 KB Output is correct
36 Correct 42 ms 5496 KB Output is correct
37 Correct 51 ms 5396 KB Output is correct
38 Correct 46 ms 5440 KB Output is correct
39 Correct 40 ms 5436 KB Output is correct
40 Correct 41 ms 5588 KB Output is correct
41 Correct 25 ms 2292 KB Output is correct
42 Correct 18 ms 2536 KB Output is correct
43 Correct 20 ms 2508 KB Output is correct
44 Correct 19 ms 2252 KB Output is correct
45 Correct 19 ms 2368 KB Output is correct
46 Correct 29 ms 2268 KB Output is correct
47 Correct 27 ms 2380 KB Output is correct
48 Correct 22 ms 2388 KB Output is correct
49 Correct 17 ms 2380 KB Output is correct
50 Correct 23 ms 2380 KB Output is correct
51 Correct 55 ms 5252 KB Output is correct
52 Correct 66 ms 5252 KB Output is correct
53 Correct 53 ms 5188 KB Output is correct
54 Correct 52 ms 5104 KB Output is correct
55 Correct 50 ms 5188 KB Output is correct
56 Correct 600 ms 51160 KB Output is correct
57 Correct 627 ms 50492 KB Output is correct
58 Correct 581 ms 47864 KB Output is correct
59 Correct 601 ms 48964 KB Output is correct
60 Correct 627 ms 52200 KB Output is correct