Submission #410290

# Submission time Handle Problem Language Result Execution time Memory
410290 2021-05-22T12:37:36 Z doowey Collapse (JOI18_collapse) C++14
100 / 100
6967 ms 14136 KB
#include <bits/stdc++.h>
#include "collapse.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair



const int K = 410;
const int N = (int)1e5 + 5;

int n;

bool active[N];
bool change[N];

struct query{
    int type;
    int time;
    int xx;
    int id;
    bool operator< (const query &qi) const {
        if(time == qi.time)
            return type < qi.type;
        return time < qi.time;
    }
};

int par[N];
int sub[N];

vector<pii> rev;

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

int total;

void unite(int u, int v, bool keep){
    u = fin(u);
    v = fin(v);
    if(u == v) return;
    total ++ ;
    if(sub[u] > sub[v]) swap(u, v);
    sub[v] += sub[u];
    par[u] = v;
    if(keep)
        rev.push_back(mp(u, v));
}

vector<int> simulateCollapse(int _N, vector<int> T, vector<int> X, vector<int> Y, vector<int> W, vector<int> P) {
    n = _N;
    vector<query> qq;
    vector<pii> E;
    for(int i = 0 ; i < T.size(); i ++ ){
        if(X[i] > Y[i]) swap(X[i], Y[i]);
        E.push_back(mp(X[i], Y[i]));
    }
    sort(E.begin(), E.end());
    E.resize(unique(E.begin(), E.end()) - E.begin());
    int m = E.size();
    int id;
    for(int i = 0 ; i < X.size(); i ++ ){
        id = lower_bound(E.begin(), E.end(), mp(X[i], Y[i])) - E.begin();
        X[i] = id;
    }
    vector<query> qaq;
    for(int i = 0 ; i < T.size(); i ++ ){
        qaq.push_back({0, i, X[i], 0});
    }
    for(int i = 0 ; i < W.size(); i ++ ){
        qaq.push_back({1, W[i], P[i], i});
    }
    sort(qaq.begin(), qaq.end());
    vector<int> soln(W.size(), n);
    int li = 0;
    int ri;
    int pp;
    while(li < qaq.size()){
        ri = min((int)qaq.size() - 1, li + K - 1);
        for(int i = 0 ; i < m ; i ++ ){
            change[i] = false;
        }
        for(int i = 0 ; i < n; i ++ ){
            par[i] = i;
            sub[i] = 1;
        }
        for(int j = li ; j <= ri; j ++ ){
            if(qaq[j].type == 0){
                change[qaq[j].xx] = true;
            }
        }
        vector<pii> res;
        vector<int> chk;
        for(int i = 0 ; i < m ; i ++ ){
            if(change[i] == false && active[i]){
                res.push_back(mp(E[i].se, E[i].fi));
            }
            else{
                if(change[i]){
                    chk.push_back(i);
                }
            }
        }
        // !!!!!!!!!!!!!!!!!!!1
        sort(res.begin(), res.end());
        vector<pii> ord;
        for(int j = li ; j <= ri ;j ++ ){
            if(qaq[j].type == 1){
                ord.push_back(mp(qaq[j].xx, j));
            }
        }
        sort(ord.begin(), ord.end());
        pp = 0;
        total = 0;
        for(auto &x : ord){
            while(pp < res.size() && res[pp].fi <= x.fi){
                unite(res[pp].fi, res[pp].se, false);
                pp ++ ;
            }
            for(int j = li ; j < x.se; j ++ ){
                if(qaq[j].type == 0){
                    active[qaq[j].xx] ^= 1;
                }
            }
            for(auto y : chk){
                if(active[y] && E[y].se <= x.fi)
                    unite(E[y].fi, E[y].se, true);
            }
            soln[qaq[x.se].id] -= total;
            while(!rev.empty()){
                total -- ;
                sub[rev.back().se] -= sub[rev.back().fi];
                par[rev.back().fi] = rev.back().fi;
                rev.pop_back();
            }
            for(int j = li ; j < x.se; j ++ ){
                if(qaq[j].type == 0)active[qaq[j].xx] ^= 1;
            }
        }
        // #############################
        for(auto &x : res){
            swap(x.fi, x.se);
        }
        sort(res.begin(), res.end());
        reverse(res.begin(), res.end());
        reverse(ord.begin(), ord.end());
        for(int i = 0 ; i < n; i ++ ){
            par[i] = i;
            sub[i] = 1;
        }
        pp = 0;
        total = 0;
        for(auto x : ord){
            while(pp < res.size() && res[pp].fi > x.fi){
                unite(res[pp].fi, res[pp].se, false);
                pp ++ ;
            }
            for(int j = li ; j < x.se; j ++ ){
                if(qaq[j].type == 0)active[qaq[j].xx] ^= 1;
            }
            for(auto y : chk){
                if(active[y] && E[y].fi > x.fi)
                    unite(E[y].fi, E[y].se, true);
            }
            soln[qaq[x.se].id] -= total;
            while(!rev.empty()){
                total -- ;
                sub[rev.back().se] -= sub[rev.back().fi];
                par[rev.back().fi] = rev.back().fi;
                rev.pop_back();
            }
            for(int j = li ; j < x.se; j ++ ){
                if(qaq[j].type == 0)active[qaq[j].xx] ^= 1;
            }
        }
        // #############################
        for(int j = li; j <= ri; j ++ ){
            if(qaq[j].type == 0){
                active[qaq[j].xx] ^= 1;
            }
        }
        li += K;

    }
	return soln;
}

Compilation message

collapse.cpp: In function 'std::vector<int> simulateCollapse(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
collapse.cpp:63:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |     for(int i = 0 ; i < T.size(); i ++ ){
      |                     ~~^~~~~~~~~~
collapse.cpp:71:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |     for(int i = 0 ; i < X.size(); i ++ ){
      |                     ~~^~~~~~~~~~
collapse.cpp:76:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |     for(int i = 0 ; i < T.size(); i ++ ){
      |                     ~~^~~~~~~~~~
collapse.cpp:79:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |     for(int i = 0 ; i < W.size(); i ++ ){
      |                     ~~^~~~~~~~~~
collapse.cpp:87:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |     while(li < qaq.size()){
      |           ~~~^~~~~~~~~~~~
collapse.cpp:125:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |             while(pp < res.size() && res[pp].fi <= x.fi){
      |                   ~~~^~~~~~~~~~~~
collapse.cpp:163:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  163 |             while(pp < res.size() && res[pp].fi > x.fi){
      |                   ~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 972 KB Output is correct
2 Correct 7 ms 568 KB Output is correct
3 Correct 8 ms 600 KB Output is correct
4 Correct 8 ms 588 KB Output is correct
5 Correct 18 ms 972 KB Output is correct
6 Correct 38 ms 972 KB Output is correct
7 Correct 7 ms 588 KB Output is correct
8 Correct 8 ms 588 KB Output is correct
9 Correct 21 ms 1036 KB Output is correct
10 Correct 35 ms 1036 KB Output is correct
11 Correct 49 ms 1036 KB Output is correct
12 Correct 43 ms 1036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 4416 KB Output is correct
2 Correct 127 ms 4388 KB Output is correct
3 Correct 284 ms 10524 KB Output is correct
4 Correct 128 ms 4492 KB Output is correct
5 Correct 412 ms 10788 KB Output is correct
6 Correct 297 ms 4328 KB Output is correct
7 Correct 5367 ms 12664 KB Output is correct
8 Correct 549 ms 11436 KB Output is correct
9 Correct 168 ms 5308 KB Output is correct
10 Correct 175 ms 5408 KB Output is correct
11 Correct 204 ms 5684 KB Output is correct
12 Correct 647 ms 11816 KB Output is correct
13 Correct 2447 ms 12736 KB Output is correct
14 Correct 5619 ms 14100 KB Output is correct
15 Correct 4528 ms 13800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 126 ms 4412 KB Output is correct
2 Correct 131 ms 4352 KB Output is correct
3 Correct 128 ms 4148 KB Output is correct
4 Correct 129 ms 4152 KB Output is correct
5 Correct 150 ms 4152 KB Output is correct
6 Correct 314 ms 4972 KB Output is correct
7 Correct 3840 ms 11004 KB Output is correct
8 Correct 6438 ms 12840 KB Output is correct
9 Correct 180 ms 5300 KB Output is correct
10 Correct 217 ms 5548 KB Output is correct
11 Correct 5884 ms 13784 KB Output is correct
12 Correct 6967 ms 14020 KB Output is correct
13 Correct 6381 ms 14136 KB Output is correct
14 Correct 6890 ms 13836 KB Output is correct
15 Correct 6114 ms 14036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 972 KB Output is correct
2 Correct 7 ms 568 KB Output is correct
3 Correct 8 ms 600 KB Output is correct
4 Correct 8 ms 588 KB Output is correct
5 Correct 18 ms 972 KB Output is correct
6 Correct 38 ms 972 KB Output is correct
7 Correct 7 ms 588 KB Output is correct
8 Correct 8 ms 588 KB Output is correct
9 Correct 21 ms 1036 KB Output is correct
10 Correct 35 ms 1036 KB Output is correct
11 Correct 49 ms 1036 KB Output is correct
12 Correct 43 ms 1036 KB Output is correct
13 Correct 120 ms 4416 KB Output is correct
14 Correct 127 ms 4388 KB Output is correct
15 Correct 284 ms 10524 KB Output is correct
16 Correct 128 ms 4492 KB Output is correct
17 Correct 412 ms 10788 KB Output is correct
18 Correct 297 ms 4328 KB Output is correct
19 Correct 5367 ms 12664 KB Output is correct
20 Correct 549 ms 11436 KB Output is correct
21 Correct 168 ms 5308 KB Output is correct
22 Correct 175 ms 5408 KB Output is correct
23 Correct 204 ms 5684 KB Output is correct
24 Correct 647 ms 11816 KB Output is correct
25 Correct 2447 ms 12736 KB Output is correct
26 Correct 5619 ms 14100 KB Output is correct
27 Correct 4528 ms 13800 KB Output is correct
28 Correct 126 ms 4412 KB Output is correct
29 Correct 131 ms 4352 KB Output is correct
30 Correct 128 ms 4148 KB Output is correct
31 Correct 129 ms 4152 KB Output is correct
32 Correct 150 ms 4152 KB Output is correct
33 Correct 314 ms 4972 KB Output is correct
34 Correct 3840 ms 11004 KB Output is correct
35 Correct 6438 ms 12840 KB Output is correct
36 Correct 180 ms 5300 KB Output is correct
37 Correct 217 ms 5548 KB Output is correct
38 Correct 5884 ms 13784 KB Output is correct
39 Correct 6967 ms 14020 KB Output is correct
40 Correct 6381 ms 14136 KB Output is correct
41 Correct 6890 ms 13836 KB Output is correct
42 Correct 6114 ms 14036 KB Output is correct
43 Correct 601 ms 11048 KB Output is correct
44 Correct 5704 ms 12468 KB Output is correct
45 Correct 685 ms 11448 KB Output is correct
46 Correct 6496 ms 12728 KB Output is correct
47 Correct 174 ms 5428 KB Output is correct
48 Correct 176 ms 5408 KB Output is correct
49 Correct 220 ms 5684 KB Output is correct
50 Correct 544 ms 6716 KB Output is correct
51 Correct 820 ms 11804 KB Output is correct
52 Correct 1899 ms 12336 KB Output is correct
53 Correct 1662 ms 12376 KB Output is correct
54 Correct 3043 ms 13228 KB Output is correct
55 Correct 2652 ms 12832 KB Output is correct
56 Correct 3899 ms 12972 KB Output is correct
57 Correct 5140 ms 13680 KB Output is correct
58 Correct 5674 ms 13656 KB Output is correct
59 Correct 5634 ms 13748 KB Output is correct
60 Correct 6957 ms 13792 KB Output is correct