Submission #623557

# Submission time Handle Problem Language Result Execution time Memory
623557 2022-08-05T22:16:07 Z AlperenT Minerals (JOI19_minerals) C++17
100 / 100
473 ms 13256 KB
#include <bits/stdc++.h>
#include "minerals.h"
 
using namespace std;
 
const int N = 50000 + 5;
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
int n, ans[N], ptr, cnt, test;
 
set<int> cur;
 
int query(int x){
    if(cur.find(x) != cur.end()) cur.erase(x);
    else cur.insert(x);
 
    return cnt = Query(x);
}
 
vector<int> treevec;
 
struct SegTree{
    map<int, vector<pair<int, int>>> tree;
 
    void clear(){
        tree.clear();
    }
 
    void build(int v, int l, int r, bool flag){
        if(l > r) return;
 
        if(l == r) ans[l] = tree[v].front().second;
        else{
            // int m = l + (r - l) * 0.385;

            int m = l + (r - l) * 0.37;
 
            for(int i = l; i <= m; i++) query(treevec[i]);

            // sort(tree[v].begin(), tree[v].end());

            shuffle(tree[v].begin(), tree[v].end(), rng);
 
            for(auto p : tree[v]){
                if(p.first <= m){
                    tree[v * 2].push_back(p);
                    test++;
                }
                else if(tree[v * 2].size() == (m - l + 1)) tree[v * 2 + 1].push_back(p);
                else if(tree[v * 2 + 1].size() == (r - (m + 1) + 1)) tree[v * 2].push_back(p);
                else{
                    if(cur.find(p.second) == cur.end()){
                        int prevcnt = cnt;
 
                        query(p.second);
 
                        if(prevcnt + 1 == cnt) tree[v * 2 + flag].push_back(p);
                        else tree[v * 2 + !flag].push_back(p);
                    }
                    else{
                        int prevcnt = cnt;
 
                        query(p.second);
 
                        if(prevcnt == cnt) tree[v * 2 + !flag].push_back(p);
                        else tree[v * 2 + flag].push_back(p);
                    }
                }
            }
 
            build(v * 2, l, m, !flag);
            build(v * 2 + 1, m + 1, r, flag);
        }
    }
};
 
SegTree sgt;

void match(vector<int> a, vector<pair<int, int>> b){
    treevec = a;
 
    sgt.clear();
 
    for(auto p : b) sgt.tree[1].push_back(p);
 
    sgt.build(1, 0, (int)a.size() - 1, true);
 
    for(int i = 0; i < a.size(); i++) Answer(treevec[i], ans[i]);
}
 
void Solve(int N_) {
    n = N_;
 
    vector<int> v(n * 2);
 
    iota(v.begin(), v.end(), 1);

    shuffle(v.begin(), v.end(), rng);

    vector<int> a, c1, c2, indexes;
    vector<pair<int, int>> tmp, b;
 
    for(int i = 0; i < n; i++){
        int prevcnt = cnt;

        query(v[i]);

        if(cnt == prevcnt + 1) tmp.push_back({i, v[i]});
        else b.push_back({i, v[i]});
    }

    reverse(tmp.begin(), tmp.end());

    while(!tmp.empty()){
        pair<int, int> p = tmp.back();
        tmp.pop_back();

        int prevcnt = cnt;

        query(p.second);

        if(cnt == prevcnt - 1) c1.push_back(p.second);
        else{
            a.push_back(p.second);
            indexes.push_back(p.first);
        }
    }

    for(auto &p : b) p.first = upper_bound(indexes.begin(), indexes.end(), p.first) - indexes.begin() - 1;

    match(a, b);

    a.clear(), b.clear(), tmp.clear(), indexes.clear();

    for(int i = n; i < n * 2; i++){
        int prevcnt = cnt;

        query(v[i]);

        if(cnt == prevcnt + 1) tmp.push_back({i, v[i]});
        else b.push_back({i, v[i]});
    }

    reverse(tmp.begin(), tmp.end());

    while(!tmp.empty()){
        pair<int, int> p = tmp.back();
        tmp.pop_back();

        int prevcnt = cnt;

        query(p.second);

        if(cnt == prevcnt - 1) c2.push_back(p.second);
        else{
            a.push_back(p.second);
            indexes.push_back(p.first);
        }
    }

    for(auto &p : b) p.first = upper_bound(indexes.begin(), indexes.end(), p.first) - indexes.begin() - 1;

    match(a, b);

    a = c1;

    b.clear();

    for(auto x : c2) b.push_back({a.size(), x});

    match(a, b);
}

Compilation message

minerals.cpp: In member function 'void SegTree::build(int, int, int, bool)':
minerals.cpp:50:44: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   50 |                 else if(tree[v * 2].size() == (m - l + 1)) tree[v * 2 + 1].push_back(p);
      |                         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
minerals.cpp:51:48: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   51 |                 else if(tree[v * 2 + 1].size() == (r - (m + 1) + 1)) tree[v * 2].push_back(p);
      |                         ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
minerals.cpp: In function 'void match(std::vector<int>, std::vector<std::pair<int, int> >)':
minerals.cpp:89:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |     for(int i = 0; i < a.size(); i++) Answer(treevec[i], ans[i]);
      |                    ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 464 KB Output is correct
2 Correct 11 ms 848 KB Output is correct
3 Correct 24 ms 1344 KB Output is correct
4 Correct 57 ms 2520 KB Output is correct
5 Correct 123 ms 4520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 5 ms 464 KB Output is correct
6 Correct 11 ms 848 KB Output is correct
7 Correct 24 ms 1344 KB Output is correct
8 Correct 57 ms 2520 KB Output is correct
9 Correct 123 ms 4520 KB Output is correct
10 Correct 5 ms 592 KB Output is correct
11 Correct 76 ms 3052 KB Output is correct
12 Correct 129 ms 4572 KB Output is correct
13 Correct 130 ms 4652 KB Output is correct
14 Correct 122 ms 4584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 5 ms 464 KB Output is correct
6 Correct 11 ms 848 KB Output is correct
7 Correct 24 ms 1344 KB Output is correct
8 Correct 57 ms 2520 KB Output is correct
9 Correct 123 ms 4520 KB Output is correct
10 Correct 5 ms 592 KB Output is correct
11 Correct 76 ms 3052 KB Output is correct
12 Correct 129 ms 4572 KB Output is correct
13 Correct 130 ms 4652 KB Output is correct
14 Correct 122 ms 4584 KB Output is correct
15 Correct 402 ms 11736 KB Output is correct
16 Correct 398 ms 11736 KB Output is correct
17 Correct 392 ms 11620 KB Output is correct
18 Correct 390 ms 11712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 5 ms 464 KB Output is correct
6 Correct 11 ms 848 KB Output is correct
7 Correct 24 ms 1344 KB Output is correct
8 Correct 57 ms 2520 KB Output is correct
9 Correct 123 ms 4520 KB Output is correct
10 Correct 5 ms 592 KB Output is correct
11 Correct 76 ms 3052 KB Output is correct
12 Correct 129 ms 4572 KB Output is correct
13 Correct 130 ms 4652 KB Output is correct
14 Correct 122 ms 4584 KB Output is correct
15 Correct 402 ms 11736 KB Output is correct
16 Correct 398 ms 11736 KB Output is correct
17 Correct 392 ms 11620 KB Output is correct
18 Correct 390 ms 11712 KB Output is correct
19 Correct 425 ms 11928 KB Output is correct
20 Correct 414 ms 11968 KB Output is correct
21 Correct 412 ms 12100 KB Output is correct
22 Correct 409 ms 11788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 5 ms 464 KB Output is correct
6 Correct 11 ms 848 KB Output is correct
7 Correct 24 ms 1344 KB Output is correct
8 Correct 57 ms 2520 KB Output is correct
9 Correct 123 ms 4520 KB Output is correct
10 Correct 5 ms 592 KB Output is correct
11 Correct 76 ms 3052 KB Output is correct
12 Correct 129 ms 4572 KB Output is correct
13 Correct 130 ms 4652 KB Output is correct
14 Correct 122 ms 4584 KB Output is correct
15 Correct 402 ms 11736 KB Output is correct
16 Correct 398 ms 11736 KB Output is correct
17 Correct 392 ms 11620 KB Output is correct
18 Correct 390 ms 11712 KB Output is correct
19 Correct 425 ms 11928 KB Output is correct
20 Correct 414 ms 11968 KB Output is correct
21 Correct 412 ms 12100 KB Output is correct
22 Correct 409 ms 11788 KB Output is correct
23 Correct 430 ms 12220 KB Output is correct
24 Correct 421 ms 12256 KB Output is correct
25 Correct 417 ms 12300 KB Output is correct
26 Correct 410 ms 12100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 5 ms 464 KB Output is correct
6 Correct 11 ms 848 KB Output is correct
7 Correct 24 ms 1344 KB Output is correct
8 Correct 57 ms 2520 KB Output is correct
9 Correct 123 ms 4520 KB Output is correct
10 Correct 5 ms 592 KB Output is correct
11 Correct 76 ms 3052 KB Output is correct
12 Correct 129 ms 4572 KB Output is correct
13 Correct 130 ms 4652 KB Output is correct
14 Correct 122 ms 4584 KB Output is correct
15 Correct 402 ms 11736 KB Output is correct
16 Correct 398 ms 11736 KB Output is correct
17 Correct 392 ms 11620 KB Output is correct
18 Correct 390 ms 11712 KB Output is correct
19 Correct 425 ms 11928 KB Output is correct
20 Correct 414 ms 11968 KB Output is correct
21 Correct 412 ms 12100 KB Output is correct
22 Correct 409 ms 11788 KB Output is correct
23 Correct 430 ms 12220 KB Output is correct
24 Correct 421 ms 12256 KB Output is correct
25 Correct 417 ms 12300 KB Output is correct
26 Correct 410 ms 12100 KB Output is correct
27 Correct 441 ms 12508 KB Output is correct
28 Correct 440 ms 12620 KB Output is correct
29 Correct 430 ms 12612 KB Output is correct
30 Correct 440 ms 12604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 5 ms 464 KB Output is correct
6 Correct 11 ms 848 KB Output is correct
7 Correct 24 ms 1344 KB Output is correct
8 Correct 57 ms 2520 KB Output is correct
9 Correct 123 ms 4520 KB Output is correct
10 Correct 5 ms 592 KB Output is correct
11 Correct 76 ms 3052 KB Output is correct
12 Correct 129 ms 4572 KB Output is correct
13 Correct 130 ms 4652 KB Output is correct
14 Correct 122 ms 4584 KB Output is correct
15 Correct 402 ms 11736 KB Output is correct
16 Correct 398 ms 11736 KB Output is correct
17 Correct 392 ms 11620 KB Output is correct
18 Correct 390 ms 11712 KB Output is correct
19 Correct 425 ms 11928 KB Output is correct
20 Correct 414 ms 11968 KB Output is correct
21 Correct 412 ms 12100 KB Output is correct
22 Correct 409 ms 11788 KB Output is correct
23 Correct 430 ms 12220 KB Output is correct
24 Correct 421 ms 12256 KB Output is correct
25 Correct 417 ms 12300 KB Output is correct
26 Correct 410 ms 12100 KB Output is correct
27 Correct 441 ms 12508 KB Output is correct
28 Correct 440 ms 12620 KB Output is correct
29 Correct 430 ms 12612 KB Output is correct
30 Correct 440 ms 12604 KB Output is correct
31 Correct 454 ms 12932 KB Output is correct
32 Correct 445 ms 12928 KB Output is correct
33 Correct 453 ms 12992 KB Output is correct
34 Correct 438 ms 12852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 5 ms 464 KB Output is correct
6 Correct 11 ms 848 KB Output is correct
7 Correct 24 ms 1344 KB Output is correct
8 Correct 57 ms 2520 KB Output is correct
9 Correct 123 ms 4520 KB Output is correct
10 Correct 5 ms 592 KB Output is correct
11 Correct 76 ms 3052 KB Output is correct
12 Correct 129 ms 4572 KB Output is correct
13 Correct 130 ms 4652 KB Output is correct
14 Correct 122 ms 4584 KB Output is correct
15 Correct 402 ms 11736 KB Output is correct
16 Correct 398 ms 11736 KB Output is correct
17 Correct 392 ms 11620 KB Output is correct
18 Correct 390 ms 11712 KB Output is correct
19 Correct 425 ms 11928 KB Output is correct
20 Correct 414 ms 11968 KB Output is correct
21 Correct 412 ms 12100 KB Output is correct
22 Correct 409 ms 11788 KB Output is correct
23 Correct 430 ms 12220 KB Output is correct
24 Correct 421 ms 12256 KB Output is correct
25 Correct 417 ms 12300 KB Output is correct
26 Correct 410 ms 12100 KB Output is correct
27 Correct 441 ms 12508 KB Output is correct
28 Correct 440 ms 12620 KB Output is correct
29 Correct 430 ms 12612 KB Output is correct
30 Correct 440 ms 12604 KB Output is correct
31 Correct 454 ms 12932 KB Output is correct
32 Correct 445 ms 12928 KB Output is correct
33 Correct 453 ms 12992 KB Output is correct
34 Correct 438 ms 12852 KB Output is correct
35 Correct 473 ms 13256 KB Output is correct
36 Correct 467 ms 13192 KB Output is correct
37 Correct 459 ms 13252 KB Output is correct
38 Correct 460 ms 13116 KB Output is correct