#include "collapse.h"
#include <bits/stdc++.h>
#warning W e PREFIXUL
using namespace std;
int B;
const int nmax = 1e5 + 5, qmax = 1e5 + 5, cmax = 1e5 + 5;
using pii = pair<int,int>;
int n, q, c;
int rollbacks = 0, forceinsert = 0, always = 0;
// decomp dupa POZITIE
namespace Driver {
namespace DSU {
int area[nmax], dsu[nmax];
vector<pair<int,int> > bk;
int cc;
void init(int n) {
for(int i = 0; i < n; i++)
dsu[i] = i, area[i] = 1;
bk.clear();
cc = n;
return;
}
int father(int x) {
return (dsu[x] == x? x : father(dsu[x]));
}
int comprfather(int x) {
return (dsu[x] == x? x : dsu[x] = father(dsu[x]));
}
int gettime() {return bk.size();}
void unite(int a, int b, bool compr) {
if(!compr)
a = father(a),
b = father(b);
else
a = comprfather(a),
b = comprfather(b);
if(a == b)
return;
if(area[a] < area[b])
swap(a, b);
dsu[b] = a;
area[a] += area[b];
dsu[b] = a;
if(!compr)
bk.push_back({b, a});
cc--;
}
int rollback() {
if(bk.size() == 0)
return -1;
int a, b;
tie(b, a) = bk.back();
dsu[b] = b;
area[a] -= area[b];
cc++;
bk.pop_back();
//rollbacks++;
return bk.size();
}
int query() { return cc; }
}
unordered_map<int, unordered_map<int,int> > atredge;
unordered_map<int, unordered_map<int,int> > open;
vector< pair<int,int> > edge;
vector< vector< pair<int,int> > > span;
vector< pair<int, int> > qs;
vector<int> rez;
struct Bucket {
vector<int> included;
vector<tuple<int,int,int> > partial;
int L, R;
vector<int> queries;
Bucket(int l, int r) {tie(L, R) = {l, r};}
void apply() {
DSU::init(n);
sort(queries.begin(), queries.end(), [&](auto x, auto y) {return qs[x].second < qs[y].second;});
sort(included.begin(), included.end(), [&](auto x, auto y) {return edge[x].second < edge[y].second;});
int ptr = 0;
for(auto idx : queries) {
int poz, time;
tie(poz, time) = qs[idx];
while(ptr < included.size() && edge[included[ptr]].second <= time)
DSU::unite(edge[included[ptr]].first, edge[included[ptr]].second, 0), ptr++;
int temporary = DSU::gettime();
for(auto X : partial) {
int edg, l, r;
tie(edg, l, r) = X;
if(l <= poz && poz <= r && edge[edg].second <= time)
DSU::unite(edge[edg].first, edge[edg].second, 0);
//always++;
}
rez[idx] += DSU::query() - (n - qs[idx].second - 1);
if(DSU::gettime() > temporary)
while(DSU::rollback() > temporary);
}
//forceinsert += included.size();
}
};
vector<Bucket> decomp;
namespace Init {
void prepare() {
int l = 0, r = min(c - 1, B - 1);
while(l < c) {
decomp.push_back(Bucket(l, r));
l += B;
r = min(c - 1, r + B);
}
decomp.push_back(Bucket(2 * c + 1, 2 * c + 2));
for(int i = 0; i < edge.size(); i++) {
int ptr = 0;
for(auto X : span[i]) {
tie(l, r) = X;
while(decomp[ptr].R < l)
ptr++;
if(ptr == decomp.size())
break;
decomp[ptr].partial.push_back({i, l, r});
if(r <= decomp[ptr].R)
continue;
ptr++;
while(l <= decomp[ptr].L && decomp[ptr].R <= r)
decomp[ptr++].included.push_back(i);
if(!(decomp[ptr].R < l || decomp[ptr].L > r))
decomp[ptr].partial.push_back({i, l, r});
}
}
for(int i = 0; i < q; i++)
decomp[qs[i].first / B].queries.push_back(i);
return;
}
void read(vector<int> t, vector<int>& lf, vector<int>& rh, vector<int> w, vector<int> p) {
rez.resize(q, 0);
for(int i = 0, ptr; i < c; i++) {
int x, y;
tie(x, y) = {lf[i], rh[i]};
if(x > y)
swap(lf[i], rh[i]), swap(x, y);
if(atredge.find(x) == atredge.end() || atredge[x].find(y) == atredge[x].end())
atredge[x][y] = edge.size(), span.push_back(vector<pii>()), edge.push_back({x, y});
ptr = atredge[x][y];
open[x][y];
if(open[x][y] == 0)
span[ptr].push_back({i, -1});
open[x][y] += -2 * t[i] + 1;
if(open[x][y] == 0)
span[ptr].back().second = i - 1;
}
int x, y, val;
for(auto X : open) {
x = X.first;
for(auto Y : X.second) {
tie(y, val) = Y;
if(val > 0)
span[atredge[x][y]].back().second = c - 1;
}
}
for(int i = 0; i < q; i++)
qs.push_back({w[i], p[i]});
return;
}
}
namespace Decomp {
void start() {
for(auto& x : decomp)
x.apply();
}
}
void erase() {
DSU::init(n);
decomp.clear();
span.clear();
qs.clear();
edge.clear();
atredge.clear();
open.clear();
}
}
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
) {
n = N;
c = T.size();
q = W.size();
B = 220;
Driver::DSU::init(n);
Driver::Init::read(T, X, Y, W, P);
Driver::Init::prepare();
Driver::Decomp::start();
Driver::erase();
for(int i = 0; i < c; i++) {
X[i] = N - X[i] - 1;
Y[i] = N - Y[i] - 1;
}
for(int i = 0; i < q; i++)
P[i] = N - P[i] - 2;
Driver::Init::read(T, X, Y, W, P);
Driver::Init::prepare();
Driver::Decomp::start();
return Driver::rez;
}
Compilation message
collapse.cpp:3:2: warning: #warning W e PREFIXUL [-Wcpp]
3 | #warning W e PREFIXUL
| ^~~~~~~
collapse.cpp: In member function 'void Driver::Bucket::apply()':
collapse.cpp:86:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | while(ptr < included.size() && edge[included[ptr]].second <= time)
| ~~~~^~~~~~~~~~~~~~~~~
collapse.cpp: In function 'void Driver::Init::prepare()':
collapse.cpp:113:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for(int i = 0; i < edge.size(); i++) {
| ~~^~~~~~~~~~~~~
collapse.cpp:119:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Driver::Bucket>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | if(ptr == decomp.size())
| ~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
4 ms |
468 KB |
Output is correct |
4 |
Correct |
4 ms |
468 KB |
Output is correct |
5 |
Correct |
10 ms |
744 KB |
Output is correct |
6 |
Correct |
20 ms |
1836 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
4 ms |
596 KB |
Output is correct |
9 |
Correct |
12 ms |
1692 KB |
Output is correct |
10 |
Correct |
21 ms |
1828 KB |
Output is correct |
11 |
Correct |
34 ms |
2912 KB |
Output is correct |
12 |
Correct |
28 ms |
2780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
4172 KB |
Output is correct |
2 |
Correct |
35 ms |
4172 KB |
Output is correct |
3 |
Correct |
177 ms |
8280 KB |
Output is correct |
4 |
Correct |
53 ms |
4220 KB |
Output is correct |
5 |
Correct |
261 ms |
9392 KB |
Output is correct |
6 |
Correct |
113 ms |
5480 KB |
Output is correct |
7 |
Correct |
3518 ms |
139168 KB |
Output is correct |
8 |
Correct |
305 ms |
17896 KB |
Output is correct |
9 |
Correct |
37 ms |
4932 KB |
Output is correct |
10 |
Correct |
43 ms |
4940 KB |
Output is correct |
11 |
Correct |
164 ms |
5644 KB |
Output is correct |
12 |
Correct |
445 ms |
29420 KB |
Output is correct |
13 |
Correct |
1901 ms |
83184 KB |
Output is correct |
14 |
Correct |
5146 ms |
156472 KB |
Output is correct |
15 |
Correct |
3877 ms |
159632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
4172 KB |
Output is correct |
2 |
Correct |
49 ms |
4164 KB |
Output is correct |
3 |
Correct |
53 ms |
4172 KB |
Output is correct |
4 |
Correct |
60 ms |
4204 KB |
Output is correct |
5 |
Correct |
142 ms |
4476 KB |
Output is correct |
6 |
Correct |
134 ms |
5520 KB |
Output is correct |
7 |
Correct |
2185 ms |
85284 KB |
Output is correct |
8 |
Correct |
4886 ms |
137948 KB |
Output is correct |
9 |
Correct |
57 ms |
4940 KB |
Output is correct |
10 |
Correct |
221 ms |
5464 KB |
Output is correct |
11 |
Correct |
5078 ms |
165008 KB |
Output is correct |
12 |
Correct |
6647 ms |
172364 KB |
Output is correct |
13 |
Correct |
5463 ms |
168820 KB |
Output is correct |
14 |
Correct |
6591 ms |
157340 KB |
Output is correct |
15 |
Correct |
5131 ms |
171168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
4 ms |
468 KB |
Output is correct |
4 |
Correct |
4 ms |
468 KB |
Output is correct |
5 |
Correct |
10 ms |
744 KB |
Output is correct |
6 |
Correct |
20 ms |
1836 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
4 ms |
596 KB |
Output is correct |
9 |
Correct |
12 ms |
1692 KB |
Output is correct |
10 |
Correct |
21 ms |
1828 KB |
Output is correct |
11 |
Correct |
34 ms |
2912 KB |
Output is correct |
12 |
Correct |
28 ms |
2780 KB |
Output is correct |
13 |
Correct |
34 ms |
4172 KB |
Output is correct |
14 |
Correct |
35 ms |
4172 KB |
Output is correct |
15 |
Correct |
177 ms |
8280 KB |
Output is correct |
16 |
Correct |
53 ms |
4220 KB |
Output is correct |
17 |
Correct |
261 ms |
9392 KB |
Output is correct |
18 |
Correct |
113 ms |
5480 KB |
Output is correct |
19 |
Correct |
3518 ms |
139168 KB |
Output is correct |
20 |
Correct |
305 ms |
17896 KB |
Output is correct |
21 |
Correct |
37 ms |
4932 KB |
Output is correct |
22 |
Correct |
43 ms |
4940 KB |
Output is correct |
23 |
Correct |
164 ms |
5644 KB |
Output is correct |
24 |
Correct |
445 ms |
29420 KB |
Output is correct |
25 |
Correct |
1901 ms |
83184 KB |
Output is correct |
26 |
Correct |
5146 ms |
156472 KB |
Output is correct |
27 |
Correct |
3877 ms |
159632 KB |
Output is correct |
28 |
Correct |
42 ms |
4172 KB |
Output is correct |
29 |
Correct |
49 ms |
4164 KB |
Output is correct |
30 |
Correct |
53 ms |
4172 KB |
Output is correct |
31 |
Correct |
60 ms |
4204 KB |
Output is correct |
32 |
Correct |
142 ms |
4476 KB |
Output is correct |
33 |
Correct |
134 ms |
5520 KB |
Output is correct |
34 |
Correct |
2185 ms |
85284 KB |
Output is correct |
35 |
Correct |
4886 ms |
137948 KB |
Output is correct |
36 |
Correct |
57 ms |
4940 KB |
Output is correct |
37 |
Correct |
221 ms |
5464 KB |
Output is correct |
38 |
Correct |
5078 ms |
165008 KB |
Output is correct |
39 |
Correct |
6647 ms |
172364 KB |
Output is correct |
40 |
Correct |
5463 ms |
168820 KB |
Output is correct |
41 |
Correct |
6591 ms |
157340 KB |
Output is correct |
42 |
Correct |
5131 ms |
171168 KB |
Output is correct |
43 |
Correct |
281 ms |
15080 KB |
Output is correct |
44 |
Correct |
3774 ms |
137392 KB |
Output is correct |
45 |
Correct |
348 ms |
17932 KB |
Output is correct |
46 |
Correct |
4856 ms |
138620 KB |
Output is correct |
47 |
Correct |
60 ms |
4936 KB |
Output is correct |
48 |
Correct |
62 ms |
4940 KB |
Output is correct |
49 |
Correct |
213 ms |
5664 KB |
Output is correct |
50 |
Correct |
331 ms |
11400 KB |
Output is correct |
51 |
Correct |
512 ms |
25868 KB |
Output is correct |
52 |
Correct |
1651 ms |
59504 KB |
Output is correct |
53 |
Correct |
1342 ms |
57632 KB |
Output is correct |
54 |
Correct |
2795 ms |
90884 KB |
Output is correct |
55 |
Correct |
2250 ms |
85276 KB |
Output is correct |
56 |
Correct |
3259 ms |
106060 KB |
Output is correct |
57 |
Correct |
4282 ms |
141264 KB |
Output is correct |
58 |
Correct |
5294 ms |
132784 KB |
Output is correct |
59 |
Correct |
5088 ms |
169084 KB |
Output is correct |
60 |
Correct |
6596 ms |
172404 KB |
Output is correct |