답안 #321154

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
321154 2020-11-11T08:25:23 Z arujbansal Pilot (NOI19_pilot) C++17
100 / 100
750 ms 63076 KB
#include <bits/stdc++.h>

#define FAST_IO ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define setIO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int) (x).size()
#define lc(i) 2*i
#define rc(i) 2*i+1
#define int long long
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vii = vector<ii>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;

const int N = 1e5 + 5, MOD = 1e9 + 7, INF = 1e9 + 5;

struct UFDS {
    int n, validFlights;
    vector<int> par, s;
    vector<bool> active;

    void init(int x) {
        n = x;
        validFlights = 0;
        par.resize(n);
        iota(all(par), 0);
        s.assign(n, 1);
        active.assign(n, false);
    }

    int get(int x) {
        if (par[x] == x) return x;
        return par[x] = get(par[x]);
    }

    bool unite(int x, int y) {
        x = get(x);
        y = get(y);

        if (!active[x]) {
            active[x] = true;
            validFlights += s[x];
        }

        if (!active[y] || x == y) return false;

        if (s[x] < s[y]) swap(x, y);

        validFlights -= flights(s[x]) + flights(s[y]);


        s[x] += s[y];
        par[y] = x;

        validFlights += flights(s[x]);

        return true;
    }

    int getSz(int x) {
        x = get(x);
        return s[x];
    }

    int flights(int x) {
        return x * (x + 1) / 2;
    }
};

signed main() {
    FAST_IO;
#ifdef arujbansal_local
    setIO("input.txt", "output.txt");
#endif

    int n, q;
    cin >> n >> q;

    vii h(n);
    for (int i = 0; i < n; i++) {
        cin >> h[i].first;
        h[i].second = i;
    }

    sort(all(h));

    vii queries(q);
    for (int i = 0; i < q; i++) {
        cin >> queries[i].first;
        queries[i].second = i;
    }

    sort(all(queries));

    UFDS mountains;
    mountains.init(n);

    int ans[q];

    int j = 0;
    for (const auto &[plane, idx] : queries) {
        while (j < n && h[j].first <= plane) {
            int cur = h[j].second;
            int prev = cur - 1;
            int next = cur + 1;

            if (prev >= 0) mountains.unite(cur, prev);
            if (next < n) mountains.unite(cur, next);
            j++;
        }

        ans[idx] = mountains.validFlights;
    }

    for (const auto &x : ans) cout << x << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 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
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 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 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 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 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 404 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 3940 KB Output is correct
2 Correct 25 ms 4076 KB Output is correct
3 Correct 24 ms 3840 KB Output is correct
4 Correct 23 ms 3812 KB Output is correct
5 Correct 24 ms 3812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 7396 KB Output is correct
2 Correct 45 ms 7524 KB Output is correct
3 Correct 46 ms 7524 KB Output is correct
4 Correct 45 ms 7652 KB Output is correct
5 Correct 44 ms 7396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 7524 KB Output is correct
2 Correct 45 ms 7396 KB Output is correct
3 Correct 47 ms 7524 KB Output is correct
4 Correct 46 ms 7524 KB Output is correct
5 Correct 48 ms 7780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 30 ms 3940 KB Output is correct
12 Correct 25 ms 4076 KB Output is correct
13 Correct 24 ms 3840 KB Output is correct
14 Correct 23 ms 3812 KB Output is correct
15 Correct 24 ms 3812 KB Output is correct
16 Correct 22 ms 3820 KB Output is correct
17 Correct 21 ms 4068 KB Output is correct
18 Correct 21 ms 4076 KB Output is correct
19 Correct 19 ms 3692 KB Output is correct
20 Correct 22 ms 4076 KB Output is correct
21 Correct 23 ms 3820 KB Output is correct
22 Correct 21 ms 3812 KB Output is correct
23 Correct 23 ms 4068 KB Output is correct
24 Correct 19 ms 3820 KB Output is correct
25 Correct 22 ms 3940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 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 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 404 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 30 ms 3940 KB Output is correct
27 Correct 25 ms 4076 KB Output is correct
28 Correct 24 ms 3840 KB Output is correct
29 Correct 23 ms 3812 KB Output is correct
30 Correct 24 ms 3812 KB Output is correct
31 Correct 44 ms 7396 KB Output is correct
32 Correct 45 ms 7524 KB Output is correct
33 Correct 46 ms 7524 KB Output is correct
34 Correct 45 ms 7652 KB Output is correct
35 Correct 44 ms 7396 KB Output is correct
36 Correct 46 ms 7524 KB Output is correct
37 Correct 45 ms 7396 KB Output is correct
38 Correct 47 ms 7524 KB Output is correct
39 Correct 46 ms 7524 KB Output is correct
40 Correct 48 ms 7780 KB Output is correct
41 Correct 22 ms 3820 KB Output is correct
42 Correct 21 ms 4068 KB Output is correct
43 Correct 21 ms 4076 KB Output is correct
44 Correct 19 ms 3692 KB Output is correct
45 Correct 22 ms 4076 KB Output is correct
46 Correct 23 ms 3820 KB Output is correct
47 Correct 21 ms 3812 KB Output is correct
48 Correct 23 ms 4068 KB Output is correct
49 Correct 19 ms 3820 KB Output is correct
50 Correct 22 ms 3940 KB Output is correct
51 Correct 57 ms 7524 KB Output is correct
52 Correct 54 ms 7268 KB Output is correct
53 Correct 56 ms 7524 KB Output is correct
54 Correct 54 ms 7268 KB Output is correct
55 Correct 55 ms 7524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 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 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 404 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 30 ms 3940 KB Output is correct
27 Correct 25 ms 4076 KB Output is correct
28 Correct 24 ms 3840 KB Output is correct
29 Correct 23 ms 3812 KB Output is correct
30 Correct 24 ms 3812 KB Output is correct
31 Correct 44 ms 7396 KB Output is correct
32 Correct 45 ms 7524 KB Output is correct
33 Correct 46 ms 7524 KB Output is correct
34 Correct 45 ms 7652 KB Output is correct
35 Correct 44 ms 7396 KB Output is correct
36 Correct 46 ms 7524 KB Output is correct
37 Correct 45 ms 7396 KB Output is correct
38 Correct 47 ms 7524 KB Output is correct
39 Correct 46 ms 7524 KB Output is correct
40 Correct 48 ms 7780 KB Output is correct
41 Correct 22 ms 3820 KB Output is correct
42 Correct 21 ms 4068 KB Output is correct
43 Correct 21 ms 4076 KB Output is correct
44 Correct 19 ms 3692 KB Output is correct
45 Correct 22 ms 4076 KB Output is correct
46 Correct 23 ms 3820 KB Output is correct
47 Correct 21 ms 3812 KB Output is correct
48 Correct 23 ms 4068 KB Output is correct
49 Correct 19 ms 3820 KB Output is correct
50 Correct 22 ms 3940 KB Output is correct
51 Correct 57 ms 7524 KB Output is correct
52 Correct 54 ms 7268 KB Output is correct
53 Correct 56 ms 7524 KB Output is correct
54 Correct 54 ms 7268 KB Output is correct
55 Correct 55 ms 7524 KB Output is correct
56 Correct 728 ms 61540 KB Output is correct
57 Correct 725 ms 61220 KB Output is correct
58 Correct 677 ms 58084 KB Output is correct
59 Correct 695 ms 59312 KB Output is correct
60 Correct 750 ms 63076 KB Output is correct