답안 #131271

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
131271 2019-07-16T22:55:58 Z duality Collapse (JOI18_collapse) C++11
0 / 100
15000 ms 10616 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 = C;//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].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].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].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].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].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].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;
                         ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 5700 KB Output is correct
2 Runtime error 24 ms 10616 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 15070 ms 10384 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 15059 ms 9864 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 5700 KB Output is correct
2 Runtime error 24 ms 10616 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -