Submission #969915

# Submission time Handle Problem Language Result Execution time Memory
969915 2024-04-25T20:06:45 Z VMaksimoski008 Pilot (NOI19_pilot) C++17
89 / 100
1000 ms 125976 KB
#include <bits/stdc++.h>

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

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;

struct DSU {
    int n, comp;
    vector<int> par, size;

    void config(int _n) {
        n = _n + 1;
        comp = _n;
        par.resize(n+1);
        size.resize(n+1, 1);
        for(int i=1; i<=n; i++) par[i] = i;
    }

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

    bool uni(int u, int v) {
        u = get(u), v = get(v);

        if(u == v) return false;
        if(size[u] < size[v]) swap(u, v);

        size[u] += size[v];
        par[v] = u;
        comp--;

        return true;
    }

    int getComp() { return comp; }
    int getSize(int u) { return size[get(u)]; }
    bool sameSet(int u, int v) { return get(u) == get(v); }
};

struct Query {
    int t, a, b, w;
    bool operator<(Query &e) {
        if(w == e.w) return (t < e.t);
        return w < e.w;
    }
};

ll C(ll x) {
    return x * (x - 1) / 2;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cout.tie(0); cin.tie(0);
    
    int n, q, mn = 1e9;
    cin >> n >> q;

    vector<int> v(n);
    vector<Query> qus;

    for(int &x : v) cin >> x;

    for(int i=1; i<n; i++)
        qus.push_back({ 1, i-1, i, max(v[i], v[i-1]) });
    
    for(int i=0; i<q; i++) {
        int x;
        cin >> x;
        mn = min(mn, x);
        qus.push_back({ 2, i, i, x });
    }

    sort(all(qus));
    vector<ll> ans(q);
    ll res = 0;

    map<int, vector<int> > mp;
    for(int i=0; i<n; i++) mp[v[i]].push_back(i);

    set<int> s;
    for(int &x : v) s.insert(x);
    vector<int> comp(all(s));

    int ptr = 0;
    DSU dsu; dsu.config(n);

    for(Query &qu : qus) {
        while(ptr < comp.size() && comp[ptr] <= qu.w) {
            for(int &p : mp[comp[ptr]]) res++;
            ptr++;
        }

        if(qu.t == 1) {
            if(dsu.sameSet(qu.a, qu.b)) continue;
            res -= C(dsu.getSize(qu.a));
            res -= C(dsu.getSize(qu.b));
            dsu.uni(qu.a, qu.b);
            res += C(dsu.getSize(qu.a));
        } else {
            ans[qu.a] = res;
        }
    }

    for(ll &x : ans) cout << x << '\n';
    return 0;
}

Compilation message

pilot.cpp: In function 'int32_t main()':
pilot.cpp:105:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |         while(ptr < comp.size() && comp[ptr] <= qu.w) {
      |               ~~~~^~~~~~~~~~~~~
pilot.cpp:106:22: warning: unused variable 'p' [-Wunused-variable]
  106 |             for(int &p : mp[comp[ptr]]) res++;
      |                      ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 13388 KB Output is correct
2 Correct 89 ms 18740 KB Output is correct
3 Correct 67 ms 12936 KB Output is correct
4 Correct 78 ms 17356 KB Output is correct
5 Correct 68 ms 12680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 22724 KB Output is correct
2 Correct 85 ms 22284 KB Output is correct
3 Correct 88 ms 22812 KB Output is correct
4 Correct 80 ms 23488 KB Output is correct
5 Correct 85 ms 21856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 23036 KB Output is correct
2 Correct 80 ms 22916 KB Output is correct
3 Correct 82 ms 21840 KB Output is correct
4 Correct 82 ms 23488 KB Output is correct
5 Correct 82 ms 22988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 69 ms 13388 KB Output is correct
12 Correct 89 ms 18740 KB Output is correct
13 Correct 67 ms 12936 KB Output is correct
14 Correct 78 ms 17356 KB Output is correct
15 Correct 68 ms 12680 KB Output is correct
16 Correct 77 ms 12744 KB Output is correct
17 Correct 101 ms 18648 KB Output is correct
18 Correct 98 ms 18984 KB Output is correct
19 Correct 66 ms 12748 KB Output is correct
20 Correct 85 ms 18336 KB Output is correct
21 Correct 67 ms 12744 KB Output is correct
22 Correct 110 ms 17780 KB Output is correct
23 Correct 70 ms 13772 KB Output is correct
24 Correct 79 ms 17356 KB Output is correct
25 Correct 72 ms 13312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 69 ms 13388 KB Output is correct
27 Correct 89 ms 18740 KB Output is correct
28 Correct 67 ms 12936 KB Output is correct
29 Correct 78 ms 17356 KB Output is correct
30 Correct 68 ms 12680 KB Output is correct
31 Correct 78 ms 22724 KB Output is correct
32 Correct 85 ms 22284 KB Output is correct
33 Correct 88 ms 22812 KB Output is correct
34 Correct 80 ms 23488 KB Output is correct
35 Correct 85 ms 21856 KB Output is correct
36 Correct 79 ms 23036 KB Output is correct
37 Correct 80 ms 22916 KB Output is correct
38 Correct 82 ms 21840 KB Output is correct
39 Correct 82 ms 23488 KB Output is correct
40 Correct 82 ms 22988 KB Output is correct
41 Correct 77 ms 12744 KB Output is correct
42 Correct 101 ms 18648 KB Output is correct
43 Correct 98 ms 18984 KB Output is correct
44 Correct 66 ms 12748 KB Output is correct
45 Correct 85 ms 18336 KB Output is correct
46 Correct 67 ms 12744 KB Output is correct
47 Correct 110 ms 17780 KB Output is correct
48 Correct 70 ms 13772 KB Output is correct
49 Correct 79 ms 17356 KB Output is correct
50 Correct 72 ms 13312 KB Output is correct
51 Correct 93 ms 17012 KB Output is correct
52 Correct 125 ms 21492 KB Output is correct
53 Correct 94 ms 17184 KB Output is correct
54 Correct 115 ms 21444 KB Output is correct
55 Correct 100 ms 17688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 69 ms 13388 KB Output is correct
27 Correct 89 ms 18740 KB Output is correct
28 Correct 67 ms 12936 KB Output is correct
29 Correct 78 ms 17356 KB Output is correct
30 Correct 68 ms 12680 KB Output is correct
31 Correct 78 ms 22724 KB Output is correct
32 Correct 85 ms 22284 KB Output is correct
33 Correct 88 ms 22812 KB Output is correct
34 Correct 80 ms 23488 KB Output is correct
35 Correct 85 ms 21856 KB Output is correct
36 Correct 79 ms 23036 KB Output is correct
37 Correct 80 ms 22916 KB Output is correct
38 Correct 82 ms 21840 KB Output is correct
39 Correct 82 ms 23488 KB Output is correct
40 Correct 82 ms 22988 KB Output is correct
41 Correct 77 ms 12744 KB Output is correct
42 Correct 101 ms 18648 KB Output is correct
43 Correct 98 ms 18984 KB Output is correct
44 Correct 66 ms 12748 KB Output is correct
45 Correct 85 ms 18336 KB Output is correct
46 Correct 67 ms 12744 KB Output is correct
47 Correct 110 ms 17780 KB Output is correct
48 Correct 70 ms 13772 KB Output is correct
49 Correct 79 ms 17356 KB Output is correct
50 Correct 72 ms 13312 KB Output is correct
51 Correct 93 ms 17012 KB Output is correct
52 Correct 125 ms 21492 KB Output is correct
53 Correct 94 ms 17184 KB Output is correct
54 Correct 115 ms 21444 KB Output is correct
55 Correct 100 ms 17688 KB Output is correct
56 Execution timed out 1059 ms 125976 KB Time limit exceeded
57 Halted 0 ms 0 KB -