답안 #363282

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
363282 2021-02-05T12:27:01 Z buyolitsez Collapse (JOI18_collapse) C++17
35 / 100
627 ms 25692 KB
#include "collapse.h"
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5 + 15;
int p[MAXN];
int rang[MAXN];
vector <int> ans;
int need = -1;
int cntComp;

int GetPar(int v) {
    if (p[v] == -1) {
        return v;
    }
    return GetPar(p[v]);
}

void Comeback(int wasCnt, vector <tuple <int, int, int>> wasDSU) {
    reverse(wasDSU.begin(), wasDSU.end());
    cntComp = wasCnt;
    for (auto [a, b, c] : wasDSU) {
        p[a] = b;
        rang[a] = c;
    }
}

void Func(int l, int r, vector <tuple <int, int, int, int>> edges, vector <pair <int, int>> querys) {
    int m = l + (r - l) / 2;
    vector <tuple <int, int, int, int>> edgesL, edgesR;
    vector <pair <int, int>> querysL, querysR;
    vector <tuple <int, int, int>> wasDSU;
    int wasCnt = cntComp;
    for (auto [a, b, x, y] : edges) {
        if (min(a, b) <= need && max(a, b) >= need + 1) {continue;}
        if (x <= l && r <= y) {
            a = GetPar(a);
            b = GetPar(b);
            if (a == b) {continue;}
            wasDSU.emplace_back(a, p[a], rang[a]);
            wasDSU.emplace_back(b, p[b], rang[b]);
            if (rang[a] > rang[b]) {swap(a, b);}
            --cntComp;
            rang[b] += rang[a];
            p[a] = b;
        } else {
            if (!(y < l || x > m)) {
                edgesL.emplace_back(a, b, x, y);
            }
            if (!(m + 1 > y || r < x)) {
                edgesR.emplace_back(a, b, x, y);
            }
        }
    }
    if (l == r) {
        for (auto [pos, num] : querys) {
            ans[num] = cntComp;
        }
        Comeback(wasCnt, wasDSU);
        return;
    }
    for (auto [pos, num] : querys) {
        if (pos <= m) {
            querysL.emplace_back(pos, num);
        } else {
            querysR.emplace_back(pos, num);
        }
    }
    Func(l, m, edgesL, querysL);
    Func(m + 1, r, edgesR, querysR);
    Comeback(wasCnt, wasDSU);
}

std::vector<int> simulateCollapse( int N, std::vector<int> T, std::vector<int> X, std::vector<int> Y, std::vector<int> W, std::vector<int> P) {
    memset(p, -1, sizeof(p));
    fill(rang, rang + MAXN, 1);
    cntComp = N;
    vector <tuple <int, int, int, int>> edges;
    map <pair <int, int>, int> last;
    ans.resize(W.size(), -1);
    for (int i = 0; i < T.size(); ++i) {
        int type = T[i], a = X[i], b = Y[i];
        if (type == 0) {
            last[{min(a, b), max(a, b)}] = i;
        } else {
            edges.emplace_back(a, b, last[{min(a, b), max(a, b)}], i - 1);
            last.erase(last.find({min(a, b), max(a, b)}));
        }
    }
    for (auto [x, y] : last) {
        edges.emplace_back(x.first, x.second, y, T.size() - 1);
    }
    vector <pair <int, int>> querys(W.size());
    if (N <= 5000 && W.size() <= 5000) {
        for (int i = 0; i < W.size(); ++i) {
            int w = W[i], p1 = P[i];
            if (need == -1) {
                need = p1;
            }
            querys[i] = {w, i};
            memset(p, -1, sizeof(p));
            fill(rang, rang + MAXN, 1);
            cntComp = N;
            for (auto [a, b, x, y] : edges) {
                if (x > w || y < w) {continue;}
                if (min(a, b) <= p1 && max(a, b) >= p1 + 1) {continue;}
                a = GetPar(a);
                b = GetPar(b);
                if (a != b) {
                    --cntComp;
                    if (rang[a] > rang[b]) {
                        swap(a, b);
                    }
                    rang[b] += rang[a];
                    p[a] = b;
                }
            }
            ans[i] = cntComp;
        }
    } else {
        for (int i = 0; i < W.size(); ++i) {
            int w = W[i], p = P[i];
            if (need == -1) {
                need = p;
            }
            querys[i] = {w, i};
        }
        Func(0, T.size() - 1, edges, querys);
    }
    return ans;
}

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:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |     for (int i = 0; i < T.size(); ++i) {
      |                     ~~^~~~~~~~~~
collapse.cpp:96:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |         for (int i = 0; i < W.size(); ++i) {
      |                         ~~^~~~~~~~~~
collapse.cpp:122:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |         for (int i = 0; i < W.size(); ++i) {
      |                         ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 408 ms 1516 KB Output is correct
2 Correct 401 ms 1388 KB Output is correct
3 Correct 396 ms 1260 KB Output is correct
4 Correct 398 ms 1260 KB Output is correct
5 Correct 414 ms 1644 KB Output is correct
6 Correct 523 ms 2028 KB Output is correct
7 Correct 398 ms 1260 KB Output is correct
8 Correct 395 ms 1260 KB Output is correct
9 Correct 423 ms 1516 KB Output is correct
10 Correct 464 ms 1772 KB Output is correct
11 Correct 627 ms 1900 KB Output is correct
12 Correct 566 ms 1960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 4716 KB Output is correct
2 Correct 38 ms 7384 KB Output is correct
3 Correct 151 ms 12512 KB Output is correct
4 Correct 40 ms 7340 KB Output is correct
5 Correct 167 ms 12640 KB Output is correct
6 Correct 58 ms 8032 KB Output is correct
7 Correct 234 ms 23772 KB Output is correct
8 Correct 177 ms 13280 KB Output is correct
9 Correct 33 ms 5228 KB Output is correct
10 Correct 40 ms 7776 KB Output is correct
11 Correct 54 ms 7904 KB Output is correct
12 Correct 184 ms 13536 KB Output is correct
13 Correct 232 ms 18908 KB Output is correct
14 Correct 270 ms 25436 KB Output is correct
15 Correct 264 ms 25692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 4716 KB Output is correct
2 Incorrect 36 ms 7136 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 408 ms 1516 KB Output is correct
2 Correct 401 ms 1388 KB Output is correct
3 Correct 396 ms 1260 KB Output is correct
4 Correct 398 ms 1260 KB Output is correct
5 Correct 414 ms 1644 KB Output is correct
6 Correct 523 ms 2028 KB Output is correct
7 Correct 398 ms 1260 KB Output is correct
8 Correct 395 ms 1260 KB Output is correct
9 Correct 423 ms 1516 KB Output is correct
10 Correct 464 ms 1772 KB Output is correct
11 Correct 627 ms 1900 KB Output is correct
12 Correct 566 ms 1960 KB Output is correct
13 Correct 38 ms 4716 KB Output is correct
14 Correct 38 ms 7384 KB Output is correct
15 Correct 151 ms 12512 KB Output is correct
16 Correct 40 ms 7340 KB Output is correct
17 Correct 167 ms 12640 KB Output is correct
18 Correct 58 ms 8032 KB Output is correct
19 Correct 234 ms 23772 KB Output is correct
20 Correct 177 ms 13280 KB Output is correct
21 Correct 33 ms 5228 KB Output is correct
22 Correct 40 ms 7776 KB Output is correct
23 Correct 54 ms 7904 KB Output is correct
24 Correct 184 ms 13536 KB Output is correct
25 Correct 232 ms 18908 KB Output is correct
26 Correct 270 ms 25436 KB Output is correct
27 Correct 264 ms 25692 KB Output is correct
28 Correct 30 ms 4716 KB Output is correct
29 Incorrect 36 ms 7136 KB Output isn't correct
30 Halted 0 ms 0 KB -