답안 #940052

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
940052 2024-03-07T04:47:09 Z vjudge1 Collapse (JOI18_collapse) C++17
0 / 100
27 ms 31940 KB
#include "collapse.h"
#include <map>
#include <string>
#include <vector>
#include <stack>
#include <utility>

#define S 400
#define N 100000

struct dsu
{
    std::vector<int>p;
    std::stack<std::pair<int, int> > save;

    dsu() {}
    dsu(int n): p(n, -1), savemode(0), comp(n) {}
    int savemode, comp;

    int find(int x) { return p[x] < 0 ? x : find(p[x]); }

    bool unite(int x, int y)
    {
        if ((x = find(x)) == (y = find(y))) return false;
        if (-p[x] < -p[y]) std::swap(x, y);
        save.push(std::make_pair(y, p[y]));
        p[x] += p[y];
        p[y] = x;
        --comp;
        return true;
    }

    void rollback()
    {
        auto [y, py] = save.top();
        int x = p[y];
        p[x] -= py;
        p[y] = py;
        ++comp;
    }
};

int n, c, q;
std::vector<std::pair<int, int> > t[N<<2];
std::vector<std::pair<int, int > > at[N];
std::vector<int> ans;

dsu ds[999];

void ins(int v, int l, int r, int x, int y, int ex, int ey)
{
    if (r < x || y < l) return;
    if (x <= l && r <= y)
    {
        t[v].emplace_back(ex, ey);
        return;
    }
    ins(2*v+1, l, (l+r)/2, x, y, ex, ey);
    ins(2*v+2, (l+r)/2+1, r, x, y, ex, ey);
}

int ban;

dsu ds1;

void dfs(int v, int l, int r)
{
    int nrb = 0;
    for (auto[x,y]:t[v])
    {
        if (x <= ban && ban < y) continue;
        nrb += ds1.unite(x, y);
    }

    if (l-r) dfs(2*v+1,l,(l+r)/2),dfs(2*v+2,(l+r)/2+1,r);
    else
    {
        for (auto[i,_]:at[l])
        {
            ans[i] = ds1.comp;
        }
    }

    while (nrb--) ds1.rollback();
}

std::vector<int> simulateCollapse(
	int n0,
	std::vector<int> t,
	std::vector<int> x,
	std::vector<int> y,
	std::vector<int> w,
	std::vector<int> p
) {
    n=n0;c=t.size();q=w.size();

    ban = p[0];

    {
        std::map<std::pair<int, int>, int> st;
        for (int i = 0; i < t.size(); ++i)
        {
            if(x[i] > y[i]) std::swap(x[i], y[i]);

            if (t[i] == 0)
            {
                st[std::make_pair(x[i], y[i])] = i;
            }
            else
            {
                ins(0, 0, c, st[std::make_pair(x[i], y[i])], i - 1, x[i], y[i]);
            }
        }
    }

    for (int i = 0; i < S; ++i) ds[i] = S;

    ds1 = dsu(n+1);
    ds1.savemode = 1;

    for (int i = 0; i < q; ++i)
        at[w[i]].emplace_back(i, p[i]);

    dfs(0, 0, c);

    ans.resize(q);
    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:101:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |         for (int i = 0; i < t.size(); ++i)
      |                         ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 23 ms 27836 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 27 ms 31928 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 26 ms 31940 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 23 ms 27836 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -