#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, 1), 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())
| ~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
724 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
5 ms |
584 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
10 ms |
812 KB |
Output is correct |
6 |
Correct |
20 ms |
1796 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
4 ms |
596 KB |
Output is correct |
9 |
Correct |
11 ms |
1876 KB |
Output is correct |
10 |
Correct |
19 ms |
1920 KB |
Output is correct |
11 |
Correct |
37 ms |
2944 KB |
Output is correct |
12 |
Correct |
29 ms |
2820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
4132 KB |
Output is correct |
2 |
Correct |
39 ms |
4552 KB |
Output is correct |
3 |
Correct |
166 ms |
9636 KB |
Output is correct |
4 |
Correct |
53 ms |
4724 KB |
Output is correct |
5 |
Correct |
298 ms |
11068 KB |
Output is correct |
6 |
Correct |
123 ms |
6332 KB |
Output is correct |
7 |
Correct |
3804 ms |
141132 KB |
Output is correct |
8 |
Correct |
314 ms |
20168 KB |
Output is correct |
9 |
Correct |
40 ms |
5688 KB |
Output is correct |
10 |
Correct |
54 ms |
5720 KB |
Output is correct |
11 |
Correct |
176 ms |
6664 KB |
Output is correct |
12 |
Correct |
464 ms |
31832 KB |
Output is correct |
13 |
Correct |
1971 ms |
82052 KB |
Output is correct |
14 |
Correct |
5320 ms |
157464 KB |
Output is correct |
15 |
Correct |
4168 ms |
162136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
4168 KB |
Output is correct |
2 |
Correct |
47 ms |
4532 KB |
Output is correct |
3 |
Correct |
53 ms |
4664 KB |
Output is correct |
4 |
Correct |
58 ms |
4636 KB |
Output is correct |
5 |
Correct |
146 ms |
5132 KB |
Output is correct |
6 |
Correct |
145 ms |
6332 KB |
Output is correct |
7 |
Correct |
2491 ms |
82256 KB |
Output is correct |
8 |
Correct |
5435 ms |
138912 KB |
Output is correct |
9 |
Correct |
68 ms |
5708 KB |
Output is correct |
10 |
Correct |
224 ms |
6508 KB |
Output is correct |
11 |
Correct |
5289 ms |
155244 KB |
Output is correct |
12 |
Correct |
6984 ms |
157832 KB |
Output is correct |
13 |
Correct |
5608 ms |
155292 KB |
Output is correct |
14 |
Correct |
6884 ms |
158484 KB |
Output is correct |
15 |
Correct |
5390 ms |
157940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
724 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
5 ms |
584 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
10 ms |
812 KB |
Output is correct |
6 |
Correct |
20 ms |
1796 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
4 ms |
596 KB |
Output is correct |
9 |
Correct |
11 ms |
1876 KB |
Output is correct |
10 |
Correct |
19 ms |
1920 KB |
Output is correct |
11 |
Correct |
37 ms |
2944 KB |
Output is correct |
12 |
Correct |
29 ms |
2820 KB |
Output is correct |
13 |
Correct |
34 ms |
4132 KB |
Output is correct |
14 |
Correct |
39 ms |
4552 KB |
Output is correct |
15 |
Correct |
166 ms |
9636 KB |
Output is correct |
16 |
Correct |
53 ms |
4724 KB |
Output is correct |
17 |
Correct |
298 ms |
11068 KB |
Output is correct |
18 |
Correct |
123 ms |
6332 KB |
Output is correct |
19 |
Correct |
3804 ms |
141132 KB |
Output is correct |
20 |
Correct |
314 ms |
20168 KB |
Output is correct |
21 |
Correct |
40 ms |
5688 KB |
Output is correct |
22 |
Correct |
54 ms |
5720 KB |
Output is correct |
23 |
Correct |
176 ms |
6664 KB |
Output is correct |
24 |
Correct |
464 ms |
31832 KB |
Output is correct |
25 |
Correct |
1971 ms |
82052 KB |
Output is correct |
26 |
Correct |
5320 ms |
157464 KB |
Output is correct |
27 |
Correct |
4168 ms |
162136 KB |
Output is correct |
28 |
Correct |
31 ms |
4168 KB |
Output is correct |
29 |
Correct |
47 ms |
4532 KB |
Output is correct |
30 |
Correct |
53 ms |
4664 KB |
Output is correct |
31 |
Correct |
58 ms |
4636 KB |
Output is correct |
32 |
Correct |
146 ms |
5132 KB |
Output is correct |
33 |
Correct |
145 ms |
6332 KB |
Output is correct |
34 |
Correct |
2491 ms |
82256 KB |
Output is correct |
35 |
Correct |
5435 ms |
138912 KB |
Output is correct |
36 |
Correct |
68 ms |
5708 KB |
Output is correct |
37 |
Correct |
224 ms |
6508 KB |
Output is correct |
38 |
Correct |
5289 ms |
155244 KB |
Output is correct |
39 |
Correct |
6984 ms |
157832 KB |
Output is correct |
40 |
Correct |
5608 ms |
155292 KB |
Output is correct |
41 |
Correct |
6884 ms |
158484 KB |
Output is correct |
42 |
Correct |
5390 ms |
157940 KB |
Output is correct |
43 |
Correct |
310 ms |
16880 KB |
Output is correct |
44 |
Correct |
4389 ms |
139308 KB |
Output is correct |
45 |
Correct |
369 ms |
20036 KB |
Output is correct |
46 |
Correct |
5400 ms |
139824 KB |
Output is correct |
47 |
Correct |
57 ms |
5708 KB |
Output is correct |
48 |
Correct |
59 ms |
5784 KB |
Output is correct |
49 |
Correct |
219 ms |
6760 KB |
Output is correct |
50 |
Correct |
352 ms |
12604 KB |
Output is correct |
51 |
Correct |
537 ms |
28428 KB |
Output is correct |
52 |
Correct |
1661 ms |
59444 KB |
Output is correct |
53 |
Correct |
1417 ms |
57796 KB |
Output is correct |
54 |
Correct |
2877 ms |
87772 KB |
Output is correct |
55 |
Correct |
2415 ms |
81892 KB |
Output is correct |
56 |
Correct |
3352 ms |
102792 KB |
Output is correct |
57 |
Correct |
4418 ms |
132248 KB |
Output is correct |
58 |
Correct |
5390 ms |
135092 KB |
Output is correct |
59 |
Correct |
5081 ms |
159120 KB |
Output is correct |
60 |
Correct |
6454 ms |
158980 KB |
Output is correct |