Submission #131274

# Submission time Handle Problem Language Result Execution time Memory
131274 2019-07-16T23:01:08 Z duality Collapse (JOI18_collapse) C++11
100 / 100
6631 ms 50204 KB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------
#include "collapse.h"

map<pii,int> M;
int u[100000],v[100000],pos[100000];
vpii queries[100000];
int active[100000],change[100000],seen[100000];
vpii edges;
bool comp(pii a,pii b) {
    return a.second < b.second;
}
int ans[100000];
int parent[100000];
int find(int n) {
    if (parent[n] != n) parent[n] = find(parent[n]);
    return parent[n];
}
vi adjList[100000];
int visited[100000];
int doDFS(int u) {
    int i;
    int c = 0;
    visited[u] = 1;
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (!visited[v]) c += 1+doDFS(v);
    }
    return c;
}
vector<int> simulateCollapse(int N,vector<int> T,vector<int> X,vector<int> Y,vector<int> W,vector<int> P) {
    int i,j,k,l;
    int C = T.size(),Q = W.size(),E = 0;
    for (i = 0; i < C; i++) {
        if (X[i] > Y[i]) swap(X[i],Y[i]);
        if (!M.count(mp(X[i],Y[i]))) u[E] = X[i],v[E] = Y[i],M[mp(X[i],Y[i])] = E++;
        pos[i] = M[mp(X[i],Y[i])];
    }
    for (i = 0; i < Q; i++) queries[W[i]].pb(mp(P[i],i));
    int bs = sqrt(C)+EPS;
    for (i = 0; i < C; i += bs) {
        int e = min(i+bs,C);
        vector<pair<pii,int> > qq;
        for (j = i; j < e; j++) {
            change[pos[j]] = 1;
            for (k = 0; k < queries[j].size(); k++) qq.pb(mp(mp(queries[j][k].first,j),queries[j][k].second));
        }
        for (j = 0; j < E; j++) {
            if (active[j] && !change[j]) edges.pb(mp(u[j],v[j]));
        }
        sort(edges.begin(),edges.end(),greater<pii>());
        sort(qq.begin(),qq.end());
        for (j = 0; j < N; j++) parent[j] = j;
        k = 0;
        int c = 0;
        for (j = (int) qq.size()-1; j >= 0; j--) {
            while ((k < edges.size()) && (edges[k].first > qq[j].first.first)) {
                if (find(edges[k].first) != find(edges[k].second)) {
                    parent[find(edges[k].first)] = find(edges[k].second);
                    c++;
                }
                k++;
            }
            vi toClear;
            for (l = qq[j].first.second; l >= i; l--) {
                if (!seen[pos[l]]) {
                    seen[pos[l]] = 1;
                    if ((T[l] == 0) && (X[l] > qq[j].first.first)) {
                        int px = find(X[l]),py = find(Y[l]);
                        adjList[px].pb(py),adjList[py].pb(px);
                        toClear.pb(px),toClear.pb(py);
                    }
                }
            }
            for (l = qq[j].first.second+1; l < e; l++) {
                if (!seen[pos[l]] && active[pos[l]] && (X[l] > qq[j].first.first)) {
                    int px = find(X[l]),py = find(Y[l]);
                    adjList[px].pb(py),adjList[py].pb(px);
                    toClear.pb(px),toClear.pb(py);
                }
            }
            ans[qq[j].second] += N-qq[j].first.first-1-c;
            for (l = 0; l < toClear.size(); l++) {
                if (!visited[toClear[l]]) ans[qq[j].second] -= doDFS(toClear[l]);
            }
            for (l = 0; l < toClear.size(); l++) adjList[toClear[l]].clear(),visited[toClear[l]] = 0;
            for (l = qq[j].first.second; l >= i; l--) seen[pos[l]] = 0;
        }
        sort(edges.begin(),edges.end(),comp);
        for (j = 0; j < N; j++) parent[j] = j;
        k = c = 0;
        for (j = 0; j < qq.size(); j++) {
            while ((k < edges.size()) && (edges[k].second <= qq[j].first.first)) {
                if (find(edges[k].first) != find(edges[k].second)) {
                    parent[find(edges[k].first)] = find(edges[k].second);
                    c++;
                }
                k++;
            }
            vi toClear;
            for (l = qq[j].first.second; l >= i; l--) {
                if (!seen[pos[l]]) {
                    seen[pos[l]] = 1;
                    if ((T[l] == 0) && (Y[l] <= qq[j].first.first)) {
                        int px = find(X[l]),py = find(Y[l]);
                        adjList[px].pb(py),adjList[py].pb(px);
                        toClear.pb(px),toClear.pb(py);
                    }
                }
            }
            for (l = qq[j].first.second+1; l < e; l++) {
                if (!seen[pos[l]] && active[pos[l]] && (Y[l] <= qq[j].first.first)) {
                    int px = find(X[l]),py = find(Y[l]);
                    adjList[px].pb(py),adjList[py].pb(px);
                    toClear.pb(px),toClear.pb(py);
                }
            }
            ans[qq[j].second] += qq[j].first.first+1-c;
            for (l = 0; l < toClear.size(); l++) {
                if (!visited[toClear[l]]) ans[qq[j].second] -= doDFS(toClear[l]);
            }
            for (l = 0; l < toClear.size(); l++) adjList[toClear[l]].clear(),visited[toClear[l]] = 0;
            for (l = qq[j].first.second; l >= i; l--) seen[pos[l]] = 0;
        }
        for (j = i; j < e; j++) active[pos[j]] ^= 1,change[pos[j]] = 0;
        edges.clear();
    }
    vi ret;
    for (i = 0; i < Q; i++) ret.pb(ans[i]);
    return ret;
}

Compilation message

collapse.cpp: In function 'int doDFS(int)':
collapse.cpp:79:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
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:100:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (k = 0; k < queries[j].size(); k++) qq.pb(mp(mp(queries[j][k].first,j),queries[j][k].second));
                         ~~^~~~~~~~~~~~~~~~~~~
collapse.cpp:111:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while ((k < edges.size()) && (edges[k].first > qq[j].first.first)) {
                     ~~^~~~~~~~~~~~~~
collapse.cpp:137:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (l = 0; l < toClear.size(); l++) {
                         ~~^~~~~~~~~~~~~~~~
collapse.cpp:140:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (l = 0; l < toClear.size(); l++) adjList[toClear[l]].clear(),visited[toClear[l]] = 0;
                         ~~^~~~~~~~~~~~~~~~
collapse.cpp:146:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j = 0; j < qq.size(); j++) {
                     ~~^~~~~~~~~~~
collapse.cpp:147:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while ((k < edges.size()) && (edges[k].second <= qq[j].first.first)) {
                     ~~^~~~~~~~~~~~~~
collapse.cpp:173:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (l = 0; l < toClear.size(); l++) {
                         ~~^~~~~~~~~~~~~~~~
collapse.cpp:176:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (l = 0; l < toClear.size(); l++) adjList[toClear[l]].clear(),visited[toClear[l]] = 0;
                         ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 5496 KB Output is correct
2 Correct 8 ms 5240 KB Output is correct
3 Correct 9 ms 5324 KB Output is correct
4 Correct 10 ms 5240 KB Output is correct
5 Correct 23 ms 5440 KB Output is correct
6 Correct 57 ms 5880 KB Output is correct
7 Correct 9 ms 5368 KB Output is correct
8 Correct 10 ms 5496 KB Output is correct
9 Correct 23 ms 5752 KB Output is correct
10 Correct 35 ms 5888 KB Output is correct
11 Correct 71 ms 6272 KB Output is correct
12 Correct 67 ms 6264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 9708 KB Output is correct
2 Correct 48 ms 9024 KB Output is correct
3 Correct 506 ms 13472 KB Output is correct
4 Correct 64 ms 9664 KB Output is correct
5 Correct 672 ms 15660 KB Output is correct
6 Correct 258 ms 10612 KB Output is correct
7 Correct 4255 ms 24940 KB Output is correct
8 Correct 811 ms 20440 KB Output is correct
9 Correct 60 ms 10856 KB Output is correct
10 Correct 62 ms 9972 KB Output is correct
11 Correct 174 ms 11000 KB Output is correct
12 Correct 945 ms 22348 KB Output is correct
13 Correct 2332 ms 24204 KB Output is correct
14 Correct 4887 ms 28084 KB Output is correct
15 Correct 3862 ms 27848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 9680 KB Output is correct
2 Correct 59 ms 8812 KB Output is correct
3 Correct 73 ms 9032 KB Output is correct
4 Correct 77 ms 9152 KB Output is correct
5 Correct 134 ms 8872 KB Output is correct
6 Correct 305 ms 9648 KB Output is correct
7 Correct 3288 ms 19896 KB Output is correct
8 Correct 5847 ms 36028 KB Output is correct
9 Correct 73 ms 10856 KB Output is correct
10 Correct 184 ms 10736 KB Output is correct
11 Correct 5746 ms 50132 KB Output is correct
12 Correct 6584 ms 38188 KB Output is correct
13 Correct 6034 ms 46712 KB Output is correct
14 Correct 6631 ms 38216 KB Output is correct
15 Correct 5988 ms 50204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 5496 KB Output is correct
2 Correct 8 ms 5240 KB Output is correct
3 Correct 9 ms 5324 KB Output is correct
4 Correct 10 ms 5240 KB Output is correct
5 Correct 23 ms 5440 KB Output is correct
6 Correct 57 ms 5880 KB Output is correct
7 Correct 9 ms 5368 KB Output is correct
8 Correct 10 ms 5496 KB Output is correct
9 Correct 23 ms 5752 KB Output is correct
10 Correct 35 ms 5888 KB Output is correct
11 Correct 71 ms 6272 KB Output is correct
12 Correct 67 ms 6264 KB Output is correct
13 Correct 44 ms 9708 KB Output is correct
14 Correct 48 ms 9024 KB Output is correct
15 Correct 506 ms 13472 KB Output is correct
16 Correct 64 ms 9664 KB Output is correct
17 Correct 672 ms 15660 KB Output is correct
18 Correct 258 ms 10612 KB Output is correct
19 Correct 4255 ms 24940 KB Output is correct
20 Correct 811 ms 20440 KB Output is correct
21 Correct 60 ms 10856 KB Output is correct
22 Correct 62 ms 9972 KB Output is correct
23 Correct 174 ms 11000 KB Output is correct
24 Correct 945 ms 22348 KB Output is correct
25 Correct 2332 ms 24204 KB Output is correct
26 Correct 4887 ms 28084 KB Output is correct
27 Correct 3862 ms 27848 KB Output is correct
28 Correct 43 ms 9680 KB Output is correct
29 Correct 59 ms 8812 KB Output is correct
30 Correct 73 ms 9032 KB Output is correct
31 Correct 77 ms 9152 KB Output is correct
32 Correct 134 ms 8872 KB Output is correct
33 Correct 305 ms 9648 KB Output is correct
34 Correct 3288 ms 19896 KB Output is correct
35 Correct 5847 ms 36028 KB Output is correct
36 Correct 73 ms 10856 KB Output is correct
37 Correct 184 ms 10736 KB Output is correct
38 Correct 5746 ms 50132 KB Output is correct
39 Correct 6584 ms 38188 KB Output is correct
40 Correct 6034 ms 46712 KB Output is correct
41 Correct 6631 ms 38216 KB Output is correct
42 Correct 5988 ms 50204 KB Output is correct
43 Correct 846 ms 18436 KB Output is correct
44 Correct 4694 ms 25276 KB Output is correct
45 Correct 1081 ms 19248 KB Output is correct
46 Correct 5846 ms 36196 KB Output is correct
47 Correct 69 ms 10960 KB Output is correct
48 Correct 73 ms 10124 KB Output is correct
49 Correct 206 ms 11012 KB Output is correct
50 Correct 636 ms 12912 KB Output is correct
51 Correct 1249 ms 21436 KB Output is correct
52 Correct 2457 ms 23272 KB Output is correct
53 Correct 2200 ms 31916 KB Output is correct
54 Correct 3313 ms 24484 KB Output is correct
55 Correct 2932 ms 31408 KB Output is correct
56 Correct 3881 ms 36452 KB Output is correct
57 Correct 5089 ms 44404 KB Output is correct
58 Correct 5408 ms 31016 KB Output is correct
59 Correct 5500 ms 43540 KB Output is correct
60 Correct 6566 ms 37920 KB Output is correct