Submission #970278

# Submission time Handle Problem Language Result Execution time Memory
970278 2024-04-26T10:08:05 Z VMaksimoski008 Pilot (NOI19_pilot) C++17
89 / 100
1000 ms 122892 KB
#include <bits/stdc++.h>
using namespace std;
 
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,bmi,bmi2,lzcnt,popcnt")
 
using ll = long long;
using pii = pair<int, int>;
using Query = array<int, 4>;
 
const int maxn = 1e6 + 5;
 
int par[maxn], sz[maxn];

void init(int n) {
    for(int i=0; i<n; i++) par[i] = i, sz[i] = 1;
}

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

bool uni(int a, int b) {
    a = get(a);
    b = get(b);

    if(a == b) return 1;
    if(sz[a] < sz[b]) swap(a, b);
    sz[a] += sz[b];
    par[b] = a;

    return 1;
}

bool sameSet(int u, int v) { return get(u) == get(v); }
int getSize(int u) { return sz[get(u)]; }
 
ll C(ll x) {
    return x * (x - 1) / 2;
}

vector<int> mp[maxn+1];
 
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(qus.begin(), qus.end(), [&](Query &a, Query &b) {
        if(a[3] == b[3]) return a[0] < b[0];
        return a[3] < b[3];
    });

    vector<ll> ans(q);
    ll res = 0;
 
    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(s.begin(), s.end());
 
    int ptr = 0;
    init(n);
 
    for(Query &qu : qus) {
        while(ptr < comp.size() && comp[ptr] <= qu[3]) {
            for(int &p : mp[comp[ptr]]) res++;
            ptr++;
        }
 
        if(qu[0] == 1) {
            //if(sameSet(qu[1], qu[2])) continue;
            res -= C(getSize(qu[1]));
            res -= C(getSize(qu[2]));
            uni(qu[1], qu[2]);
            res += C(getSize(qu[1]));
        } else {
            ans[qu[1]] = res;
        }
    }
 
    for(ll &x : ans) cout << x << '\n';
    return 0;
}

Compilation message

pilot.cpp: In function 'int32_t main()':
pilot.cpp:85:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         while(ptr < comp.size() && comp[ptr] <= qu[3]) {
      |               ~~~~^~~~~~~~~~~~~
pilot.cpp:86:22: warning: unused variable 'p' [-Wunused-variable]
   86 |             for(int &p : mp[comp[ptr]]) res++;
      |                      ^
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27228 KB Output is correct
2 Correct 6 ms 27228 KB Output is correct
3 Correct 6 ms 27228 KB Output is correct
4 Correct 7 ms 27228 KB Output is correct
5 Correct 6 ms 27228 KB Output is correct
6 Correct 6 ms 27228 KB Output is correct
7 Correct 6 ms 27228 KB Output is correct
8 Correct 6 ms 27224 KB Output is correct
9 Correct 7 ms 27228 KB Output is correct
10 Correct 6 ms 27228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27228 KB Output is correct
2 Correct 6 ms 27228 KB Output is correct
3 Correct 6 ms 27228 KB Output is correct
4 Correct 7 ms 27228 KB Output is correct
5 Correct 6 ms 27228 KB Output is correct
6 Correct 6 ms 27228 KB Output is correct
7 Correct 6 ms 27228 KB Output is correct
8 Correct 6 ms 27224 KB Output is correct
9 Correct 7 ms 27228 KB Output is correct
10 Correct 6 ms 27228 KB Output is correct
11 Correct 7 ms 27228 KB Output is correct
12 Correct 6 ms 27228 KB Output is correct
13 Correct 6 ms 27268 KB Output is correct
14 Correct 6 ms 27228 KB Output is correct
15 Correct 6 ms 27312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27228 KB Output is correct
2 Correct 6 ms 27228 KB Output is correct
3 Correct 6 ms 27228 KB Output is correct
4 Correct 7 ms 27228 KB Output is correct
5 Correct 6 ms 27228 KB Output is correct
6 Correct 6 ms 27228 KB Output is correct
7 Correct 6 ms 27228 KB Output is correct
8 Correct 6 ms 27224 KB Output is correct
9 Correct 7 ms 27228 KB Output is correct
10 Correct 6 ms 27228 KB Output is correct
11 Correct 7 ms 27228 KB Output is correct
12 Correct 6 ms 27228 KB Output is correct
13 Correct 6 ms 27268 KB Output is correct
14 Correct 6 ms 27228 KB Output is correct
15 Correct 6 ms 27312 KB Output is correct
16 Correct 6 ms 27228 KB Output is correct
17 Correct 6 ms 27228 KB Output is correct
18 Correct 7 ms 27224 KB Output is correct
19 Correct 6 ms 27228 KB Output is correct
20 Correct 6 ms 27228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27228 KB Output is correct
2 Correct 6 ms 27228 KB Output is correct
3 Correct 6 ms 27228 KB Output is correct
4 Correct 7 ms 27228 KB Output is correct
5 Correct 6 ms 27228 KB Output is correct
6 Correct 6 ms 27228 KB Output is correct
7 Correct 6 ms 27228 KB Output is correct
8 Correct 6 ms 27224 KB Output is correct
9 Correct 7 ms 27228 KB Output is correct
10 Correct 6 ms 27228 KB Output is correct
11 Correct 7 ms 27228 KB Output is correct
12 Correct 6 ms 27228 KB Output is correct
13 Correct 6 ms 27268 KB Output is correct
14 Correct 6 ms 27228 KB Output is correct
15 Correct 6 ms 27312 KB Output is correct
16 Correct 6 ms 27228 KB Output is correct
17 Correct 6 ms 27228 KB Output is correct
18 Correct 7 ms 27224 KB Output is correct
19 Correct 6 ms 27228 KB Output is correct
20 Correct 6 ms 27228 KB Output is correct
21 Correct 7 ms 27228 KB Output is correct
22 Correct 6 ms 27228 KB Output is correct
23 Correct 7 ms 27228 KB Output is correct
24 Correct 7 ms 27224 KB Output is correct
25 Correct 6 ms 27228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 34212 KB Output is correct
2 Correct 91 ms 36808 KB Output is correct
3 Correct 56 ms 33988 KB Output is correct
4 Correct 72 ms 36040 KB Output is correct
5 Correct 54 ms 33748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 41008 KB Output is correct
2 Correct 64 ms 40572 KB Output is correct
3 Correct 77 ms 39684 KB Output is correct
4 Correct 68 ms 40644 KB Output is correct
5 Correct 71 ms 40036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 40064 KB Output is correct
2 Correct 84 ms 40588 KB Output is correct
3 Correct 82 ms 40772 KB Output is correct
4 Correct 72 ms 41152 KB Output is correct
5 Correct 72 ms 40128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27228 KB Output is correct
2 Correct 6 ms 27228 KB Output is correct
3 Correct 6 ms 27228 KB Output is correct
4 Correct 7 ms 27228 KB Output is correct
5 Correct 6 ms 27228 KB Output is correct
6 Correct 6 ms 27228 KB Output is correct
7 Correct 6 ms 27228 KB Output is correct
8 Correct 6 ms 27224 KB Output is correct
9 Correct 7 ms 27228 KB Output is correct
10 Correct 6 ms 27228 KB Output is correct
11 Correct 55 ms 34212 KB Output is correct
12 Correct 91 ms 36808 KB Output is correct
13 Correct 56 ms 33988 KB Output is correct
14 Correct 72 ms 36040 KB Output is correct
15 Correct 54 ms 33748 KB Output is correct
16 Correct 52 ms 33736 KB Output is correct
17 Correct 74 ms 36808 KB Output is correct
18 Correct 81 ms 36908 KB Output is correct
19 Correct 54 ms 33788 KB Output is correct
20 Correct 71 ms 36552 KB Output is correct
21 Correct 53 ms 33740 KB Output is correct
22 Correct 69 ms 36296 KB Output is correct
23 Correct 56 ms 34372 KB Output is correct
24 Correct 68 ms 36044 KB Output is correct
25 Correct 54 ms 34196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27228 KB Output is correct
2 Correct 6 ms 27228 KB Output is correct
3 Correct 6 ms 27228 KB Output is correct
4 Correct 7 ms 27228 KB Output is correct
5 Correct 6 ms 27228 KB Output is correct
6 Correct 6 ms 27228 KB Output is correct
7 Correct 6 ms 27228 KB Output is correct
8 Correct 6 ms 27224 KB Output is correct
9 Correct 7 ms 27228 KB Output is correct
10 Correct 6 ms 27228 KB Output is correct
11 Correct 7 ms 27228 KB Output is correct
12 Correct 6 ms 27228 KB Output is correct
13 Correct 6 ms 27268 KB Output is correct
14 Correct 6 ms 27228 KB Output is correct
15 Correct 6 ms 27312 KB Output is correct
16 Correct 6 ms 27228 KB Output is correct
17 Correct 6 ms 27228 KB Output is correct
18 Correct 7 ms 27224 KB Output is correct
19 Correct 6 ms 27228 KB Output is correct
20 Correct 6 ms 27228 KB Output is correct
21 Correct 7 ms 27228 KB Output is correct
22 Correct 6 ms 27228 KB Output is correct
23 Correct 7 ms 27228 KB Output is correct
24 Correct 7 ms 27224 KB Output is correct
25 Correct 6 ms 27228 KB Output is correct
26 Correct 55 ms 34212 KB Output is correct
27 Correct 91 ms 36808 KB Output is correct
28 Correct 56 ms 33988 KB Output is correct
29 Correct 72 ms 36040 KB Output is correct
30 Correct 54 ms 33748 KB Output is correct
31 Correct 62 ms 41008 KB Output is correct
32 Correct 64 ms 40572 KB Output is correct
33 Correct 77 ms 39684 KB Output is correct
34 Correct 68 ms 40644 KB Output is correct
35 Correct 71 ms 40036 KB Output is correct
36 Correct 71 ms 40064 KB Output is correct
37 Correct 84 ms 40588 KB Output is correct
38 Correct 82 ms 40772 KB Output is correct
39 Correct 72 ms 41152 KB Output is correct
40 Correct 72 ms 40128 KB Output is correct
41 Correct 52 ms 33736 KB Output is correct
42 Correct 74 ms 36808 KB Output is correct
43 Correct 81 ms 36908 KB Output is correct
44 Correct 54 ms 33788 KB Output is correct
45 Correct 71 ms 36552 KB Output is correct
46 Correct 53 ms 33740 KB Output is correct
47 Correct 69 ms 36296 KB Output is correct
48 Correct 56 ms 34372 KB Output is correct
49 Correct 68 ms 36044 KB Output is correct
50 Correct 54 ms 34196 KB Output is correct
51 Correct 81 ms 37104 KB Output is correct
52 Correct 104 ms 39800 KB Output is correct
53 Correct 83 ms 37316 KB Output is correct
54 Correct 99 ms 39312 KB Output is correct
55 Correct 87 ms 37520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27228 KB Output is correct
2 Correct 6 ms 27228 KB Output is correct
3 Correct 6 ms 27228 KB Output is correct
4 Correct 7 ms 27228 KB Output is correct
5 Correct 6 ms 27228 KB Output is correct
6 Correct 6 ms 27228 KB Output is correct
7 Correct 6 ms 27228 KB Output is correct
8 Correct 6 ms 27224 KB Output is correct
9 Correct 7 ms 27228 KB Output is correct
10 Correct 6 ms 27228 KB Output is correct
11 Correct 7 ms 27228 KB Output is correct
12 Correct 6 ms 27228 KB Output is correct
13 Correct 6 ms 27268 KB Output is correct
14 Correct 6 ms 27228 KB Output is correct
15 Correct 6 ms 27312 KB Output is correct
16 Correct 6 ms 27228 KB Output is correct
17 Correct 6 ms 27228 KB Output is correct
18 Correct 7 ms 27224 KB Output is correct
19 Correct 6 ms 27228 KB Output is correct
20 Correct 6 ms 27228 KB Output is correct
21 Correct 7 ms 27228 KB Output is correct
22 Correct 6 ms 27228 KB Output is correct
23 Correct 7 ms 27228 KB Output is correct
24 Correct 7 ms 27224 KB Output is correct
25 Correct 6 ms 27228 KB Output is correct
26 Correct 55 ms 34212 KB Output is correct
27 Correct 91 ms 36808 KB Output is correct
28 Correct 56 ms 33988 KB Output is correct
29 Correct 72 ms 36040 KB Output is correct
30 Correct 54 ms 33748 KB Output is correct
31 Correct 62 ms 41008 KB Output is correct
32 Correct 64 ms 40572 KB Output is correct
33 Correct 77 ms 39684 KB Output is correct
34 Correct 68 ms 40644 KB Output is correct
35 Correct 71 ms 40036 KB Output is correct
36 Correct 71 ms 40064 KB Output is correct
37 Correct 84 ms 40588 KB Output is correct
38 Correct 82 ms 40772 KB Output is correct
39 Correct 72 ms 41152 KB Output is correct
40 Correct 72 ms 40128 KB Output is correct
41 Correct 52 ms 33736 KB Output is correct
42 Correct 74 ms 36808 KB Output is correct
43 Correct 81 ms 36908 KB Output is correct
44 Correct 54 ms 33788 KB Output is correct
45 Correct 71 ms 36552 KB Output is correct
46 Correct 53 ms 33740 KB Output is correct
47 Correct 69 ms 36296 KB Output is correct
48 Correct 56 ms 34372 KB Output is correct
49 Correct 68 ms 36044 KB Output is correct
50 Correct 54 ms 34196 KB Output is correct
51 Correct 81 ms 37104 KB Output is correct
52 Correct 104 ms 39800 KB Output is correct
53 Correct 83 ms 37316 KB Output is correct
54 Correct 99 ms 39312 KB Output is correct
55 Correct 87 ms 37520 KB Output is correct
56 Execution timed out 1087 ms 122892 KB Time limit exceeded
57 Halted 0 ms 0 KB -