Submission #202911

# Submission time Handle Problem Language Result Execution time Memory
202911 2020-02-18T16:25:31 Z Mercenary Collapse (JOI18_collapse) C++14
100 / 100
4648 ms 53356 KB
#ifndef LOCAL
#include "collapse.h"
#endif // LOCAL
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair

using namespace std;
const int maxn = 1e5 + 5;
typedef pair<int,int> ii;

int n , m , q;
vector<int> Q[maxn];
int type[maxn] , x[maxn] , y[maxn];
int p[maxn] , w[maxn];
int in[maxn] , out[maxn];
int res[maxn];

struct Dsurollback{
    pair<int*,int> data[(int)2e6];
    int lab[maxn] , sz;
    int FindLab(int u){
        return lab[u] < 0 ? u : FindLab(lab[u]);
    }
    void Change(int & u , int v){
        data[sz++] = mp(&u,u);
        u = v;
    }
    void rollback(int sz1){
        while(sz > sz1){
            (*data[sz-1].first) = data[sz-1].second;
            --sz;
        }
        cnt -= cnt1;
        cnt1 = 0;
    }
    int cnt = 0;
    int cnt1 = 0;
    void Merge(int u , int v){
        u = FindLab(u);v = FindLab(v);
        if(u == v)return;
        if(lab[u] > lab[v])swap(u,v);
        Change(lab[u],lab[u]+lab[v]);
        Change(lab[v],u);
        cnt++;cnt1++;
    }
    int cur(){return cnt1 = 0 , sz;};
    void reset(){
        fill_n(lab,maxn,-1);cnt = cnt1 = 0;
        sz = 0;
    }
}Dsu;

void solve(){
    const int MAGIC = 400;
    for(int l = 0 ; l < m ; l += MAGIC){
        vector<int> sp  , norm,q;
        int r = min(m,l+MAGIC) - 1;
        for(int i = 0 ; i <= r ; ++i){
            if(i >= l)for(auto & id : Q[i])q.pb(id);
            if(out[i] < l || type[i] == 1)continue;
            if(i < l && out[i] > r)norm.pb(i);
            else sp.pb(i);
        }
        sort(norm.begin(),norm.end(),[&](const int a , const int b){return y[a] < y[b];});
        sort(q.begin(),q.end(),[&](const int a , const int b){return p[a] < p[b];});
        int qi = 0 , qe = 0;
        Dsu.reset();
        for(int i = 0 ; i < n ; ++i){
            while(qe < norm.size() && y[norm[qe]] == i)Dsu.Merge(x[norm[qe]] , y[norm[qe]]),++qe;
            int tmp = Dsu.cnt;
            while(qi < q.size() && p[q[qi]] == i){
                int id = q[qi];
                int pre = Dsu.cur();
                for(auto & c : sp){
                    if(y[c] <= i && c <= w[id] && w[id] < out[c])Dsu.Merge(x[c],y[c]);
                }
                res[id] -= Dsu.cnt;
                Dsu.rollback(pre);
                ++qi;
            }
        }
//        cerr << norm.size() << " " << Dsu.cnt << endl;
        sort(norm.begin(),norm.end(),[&](const int a , const int b){return x[a] > x[b];});
        reverse(q.begin(),q.end());
        qi = 0 , qe = 0;
        Dsu.reset();
        for(int i = n - 1 ; i >= 0 ; --i){
            while(qe < norm.size() && x[norm[qe]] == i){
                Dsu.Merge(x[norm[qe]] , y[norm[qe]]),++qe;
            }
            while(qi < q.size() && p[q[qi]] == i - 1){
                int id = q[qi];
                int pre = Dsu.cur();
                for(auto & c : sp){
                    if(x[c] >= i && c <= w[id] && w[id] < out[c]){
                        Dsu.Merge(x[c],y[c]);
                    }
                }
                res[id] -= Dsu.cnt;
                Dsu.rollback(pre);
                ++qi;
            }
        }
    }
}

vector<int> simulateCollapse(int N,vector<int> T,vector<int> X,vector<int> Y,vector<int> W,vector<int> P)
{
    n = N;m = T.size();q = W.size();
    for(int i = 0 ; i < m ; ++i){
        type[i] = T[i];
        x[i] = X[i];y[i] = Y[i];
        if(x[i] > y[i])swap(x[i],y[i]);
    }
    for(int i = 0 ; i < q ; ++i){
        p[i] =  P[i];w[i] = W[i];
        Q[w[i]].pb(i);
    }
    map<ii,int> mmap;
    for(int i = 0 ; i < m ; ++i){
        if(type[i] == 0){
            mmap[mp(x[i],y[i])] = i;
        }else{
            out[mmap[mp(x[i],y[i])]] = i;
        }
    }
    for(int i = 0 ; i < m ; ++i)if(type[i] == 0 && out[i] == 0)out[i] = m;
    solve();
    vector<int> res;
    for(int i = 0 ; i < q ; ++i)res.pb(::res[i] + n);
    return res;
}


#ifdef LOCAL
int main(int argc, char *argv[]) {
	int N, C, Q;
    freopen("A.INP","r",stdin);
    freopen("A.OUT","w",stdout);
	scanf("%d%d%d", &N, &C, &Q);
	std::vector<int> T(C), X(C), Y(C);
	for(int i = 0; i < C; i++) {
		scanf("%d%d%d", &T[i], &X[i], &Y[i]);
	}
	std::vector<int> W(Q), P(Q);
	for(int i = 0; i < Q; i++) {
		scanf("%d%d", &W[i], &P[i]);
	}
	auto res = simulateCollapse(N, T, X, Y, W, P);
	for(auto i : res) {
		printf("%d\n", i);
	}
}
#endif

Compilation message

collapse.cpp: In function 'void solve()':
collapse.cpp:70:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(qe < norm.size() && y[norm[qe]] == i)Dsu.Merge(x[norm[qe]] , y[norm[qe]]),++qe;
                   ~~~^~~~~~~~~~~~~
collapse.cpp:72:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(qi < q.size() && p[q[qi]] == i){
                   ~~~^~~~~~~~~~
collapse.cpp:71:17: warning: unused variable 'tmp' [-Wunused-variable]
             int tmp = Dsu.cnt;
                 ^~~
collapse.cpp:89:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(qe < norm.size() && x[norm[qe]] == i){
                   ~~~^~~~~~~~~~~~~
collapse.cpp:92:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(qi < q.size() && p[q[qi]] == i - 1){
                   ~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 33 ms 35064 KB Output is correct
2 Correct 27 ms 34680 KB Output is correct
3 Correct 26 ms 34680 KB Output is correct
4 Correct 28 ms 34680 KB Output is correct
5 Correct 36 ms 34928 KB Output is correct
6 Correct 48 ms 35320 KB Output is correct
7 Correct 25 ms 34680 KB Output is correct
8 Correct 26 ms 34680 KB Output is correct
9 Correct 38 ms 35192 KB Output is correct
10 Correct 55 ms 35192 KB Output is correct
11 Correct 70 ms 35320 KB Output is correct
12 Correct 61 ms 35320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 38760 KB Output is correct
2 Correct 57 ms 39012 KB Output is correct
3 Correct 309 ms 45944 KB Output is correct
4 Correct 85 ms 39176 KB Output is correct
5 Correct 430 ms 46328 KB Output is correct
6 Correct 327 ms 39820 KB Output is correct
7 Correct 2458 ms 53024 KB Output is correct
8 Correct 479 ms 49012 KB Output is correct
9 Correct 62 ms 39272 KB Output is correct
10 Correct 73 ms 39396 KB Output is correct
11 Correct 402 ms 39544 KB Output is correct
12 Correct 599 ms 49140 KB Output is correct
13 Correct 1892 ms 50684 KB Output is correct
14 Correct 4390 ms 52880 KB Output is correct
15 Correct 3187 ms 52600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 38760 KB Output is correct
2 Correct 65 ms 38952 KB Output is correct
3 Correct 79 ms 39080 KB Output is correct
4 Correct 96 ms 39164 KB Output is correct
5 Correct 382 ms 39356 KB Output is correct
6 Correct 357 ms 39812 KB Output is correct
7 Correct 1599 ms 48756 KB Output is correct
8 Correct 3377 ms 51816 KB Output is correct
9 Correct 76 ms 39272 KB Output is correct
10 Correct 519 ms 39488 KB Output is correct
11 Correct 3545 ms 51760 KB Output is correct
12 Correct 4602 ms 51608 KB Output is correct
13 Correct 3726 ms 51780 KB Output is correct
14 Correct 4617 ms 51720 KB Output is correct
15 Correct 3663 ms 51544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 35064 KB Output is correct
2 Correct 27 ms 34680 KB Output is correct
3 Correct 26 ms 34680 KB Output is correct
4 Correct 28 ms 34680 KB Output is correct
5 Correct 36 ms 34928 KB Output is correct
6 Correct 48 ms 35320 KB Output is correct
7 Correct 25 ms 34680 KB Output is correct
8 Correct 26 ms 34680 KB Output is correct
9 Correct 38 ms 35192 KB Output is correct
10 Correct 55 ms 35192 KB Output is correct
11 Correct 70 ms 35320 KB Output is correct
12 Correct 61 ms 35320 KB Output is correct
13 Correct 56 ms 38760 KB Output is correct
14 Correct 57 ms 39012 KB Output is correct
15 Correct 309 ms 45944 KB Output is correct
16 Correct 85 ms 39176 KB Output is correct
17 Correct 430 ms 46328 KB Output is correct
18 Correct 327 ms 39820 KB Output is correct
19 Correct 2458 ms 53024 KB Output is correct
20 Correct 479 ms 49012 KB Output is correct
21 Correct 62 ms 39272 KB Output is correct
22 Correct 73 ms 39396 KB Output is correct
23 Correct 402 ms 39544 KB Output is correct
24 Correct 599 ms 49140 KB Output is correct
25 Correct 1892 ms 50684 KB Output is correct
26 Correct 4390 ms 52880 KB Output is correct
27 Correct 3187 ms 52600 KB Output is correct
28 Correct 57 ms 38760 KB Output is correct
29 Correct 65 ms 38952 KB Output is correct
30 Correct 79 ms 39080 KB Output is correct
31 Correct 96 ms 39164 KB Output is correct
32 Correct 382 ms 39356 KB Output is correct
33 Correct 357 ms 39812 KB Output is correct
34 Correct 1599 ms 48756 KB Output is correct
35 Correct 3377 ms 51816 KB Output is correct
36 Correct 76 ms 39272 KB Output is correct
37 Correct 519 ms 39488 KB Output is correct
38 Correct 3545 ms 51760 KB Output is correct
39 Correct 4602 ms 51608 KB Output is correct
40 Correct 3726 ms 51780 KB Output is correct
41 Correct 4617 ms 51720 KB Output is correct
42 Correct 3663 ms 51544 KB Output is correct
43 Correct 474 ms 48700 KB Output is correct
44 Correct 2497 ms 52396 KB Output is correct
45 Correct 562 ms 49012 KB Output is correct
46 Correct 3297 ms 52740 KB Output is correct
47 Correct 77 ms 39140 KB Output is correct
48 Correct 84 ms 39268 KB Output is correct
49 Correct 442 ms 39700 KB Output is correct
50 Correct 631 ms 41032 KB Output is correct
51 Correct 756 ms 49400 KB Output is correct
52 Correct 1597 ms 50172 KB Output is correct
53 Correct 1431 ms 49868 KB Output is correct
54 Correct 2333 ms 51080 KB Output is correct
55 Correct 2004 ms 50764 KB Output is correct
56 Correct 2594 ms 51348 KB Output is correct
57 Correct 3180 ms 52216 KB Output is correct
58 Correct 3863 ms 52608 KB Output is correct
59 Correct 3539 ms 53356 KB Output is correct
60 Correct 4648 ms 53128 KB Output is correct