#include <bits/stdc++.h>
#include "collapse.h"
using namespace std;
const int Bsize = 323, Nmax = 1e5 + 5;
struct Edge
{
int x, y, t1, t2;
bool operator < (const Edge &other) const
{
return y < other.y;
}
};
struct Query
{
int pos, t, id;
bool operator < (const Query &other) const
{
return pos < other.pos;
}
};
vector<int> answer;
vector<Query> queries;
vector<Edge> edges;
map< pair<int,int> , int > mp;
int N, Q, M;
namespace forest
{
static int moves;
static int t[Nmax], rang[Nmax];
static vector<int> S, T;
void init()
{
moves = 0;
S.clear(); T.clear();
int i;
for(i=0; i<N; ++i) t[i] = i;
}
int boss(int node)
{
while(t[node] != node) node = t[node];
return node;
}
bool unite(int x, int y)
{
x = boss(x); y = boss(y);
if(x == y) return 0;
++moves;
if(rang[x] == rang[y])
{
++rang[y];
S.push_back(y);
}
else
{
S.push_back(0);
if(rang[x] > rang[y]) swap(x, y);
}
t[x] = y;
T.push_back(x);
return 1;
}
void undo(int nr)
{
moves -= nr;
while(nr--)
{
assert(S.size());
assert(T.size());
int x, y;
x = T.back(), y = S.back();
S.pop_back(); T.pop_back();
t[x] = x;
if(y) rang[y] --;
}
}
}
void solve(vector<Edge> E, vector<Query> Q)
{
int i;
vector<Query> v[Nmax];
vector<Edge> all, possible;
for(auto it : Q)
v[it.t / Bsize * Bsize].push_back(it);
for(i=0; i<=M; i += Bsize) if(!v[i].empty())
{
forest::init();
all.clear();
possible.clear();
for(auto it : E)
if(it.t1 <= i && it.t2 >= i + Bsize - 1) all.push_back(it);
else if(it.t2 >= i && it.t1 <= i + Bsize - 1) possible.push_back(it);
sort(all.begin(), all.end());
sort(v[i].begin(), v[i].end());
int cursor = 0;
for(auto q : v[i])
{
while(cursor < all.size() && all[cursor].y <= q.pos)
forest::unite(all[cursor].x, all[cursor].y), ++cursor;
int undo = 0;
for(auto it : possible)
if(it.t1 <= q.t && q.t <= it.t2 && it.y <= q.pos)
if(forest::unite(it.x, it.y)) ++undo;
answer[q.id] -= forest::moves;
forest::undo(undo);
}
}
}
vector<int> simulateCollapse (int _N, vector<int> T, vector<int> X, vector<int> Y, vector<int> W, vector<int> P)
{
N = _N, M = T.size(), Q = W.size();
int i;
answer.resize(Q);
for(i=0; i<Q; ++i) answer[i] = N;
for(i=0; i<M; ++i)
{
if(X[i] > Y[i]) swap(X[i], Y[i]);
if(T[i] == 0) mp[{X[i], Y[i]}] = i;
else
{
edges.push_back({X[i], Y[i], mp[{X[i], Y[i]}], i - 1});
mp[{X[i], Y[i]}] = -1;
}
}
for(auto it : mp)
if(it.second != -1)
edges.push_back({it.first.first, it.first.second, it.second, M-1});
for(i=0; i<Q; ++i)
queries.push_back({P[i], W[i], i});
solve(edges, queries);
for(auto &it : edges)
{
it.x = N - it.x - 1, it.y = N - it.y - 1;
swap(it.x, it.y);
}
for(auto &it : queries)
it.pos = N - it.pos - 2;
solve(edges, queries);
return answer;
}
Compilation message
collapse.cpp: In function 'void solve(std::vector<Edge>, std::vector<Query>)':
collapse.cpp:114:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(cursor < all.size() && all[cursor].y <= q.pos)
~~~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
3320 KB |
Output is correct |
2 |
Correct |
5 ms |
3064 KB |
Output is correct |
3 |
Correct |
16 ms |
3064 KB |
Output is correct |
4 |
Correct |
7 ms |
3064 KB |
Output is correct |
5 |
Correct |
11 ms |
3320 KB |
Output is correct |
6 |
Correct |
32 ms |
4088 KB |
Output is correct |
7 |
Correct |
6 ms |
3156 KB |
Output is correct |
8 |
Correct |
7 ms |
3180 KB |
Output is correct |
9 |
Correct |
13 ms |
3576 KB |
Output is correct |
10 |
Correct |
26 ms |
3576 KB |
Output is correct |
11 |
Correct |
52 ms |
4136 KB |
Output is correct |
12 |
Correct |
39 ms |
4088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
10176 KB |
Output is correct |
2 |
Correct |
44 ms |
10272 KB |
Output is correct |
3 |
Correct |
219 ms |
14428 KB |
Output is correct |
4 |
Correct |
66 ms |
10176 KB |
Output is correct |
5 |
Correct |
307 ms |
15020 KB |
Output is correct |
6 |
Correct |
352 ms |
10168 KB |
Output is correct |
7 |
Correct |
2201 ms |
28016 KB |
Output is correct |
8 |
Correct |
356 ms |
18456 KB |
Output is correct |
9 |
Correct |
43 ms |
10940 KB |
Output is correct |
10 |
Correct |
51 ms |
10932 KB |
Output is correct |
11 |
Correct |
224 ms |
10580 KB |
Output is correct |
12 |
Correct |
350 ms |
19376 KB |
Output is correct |
13 |
Correct |
1292 ms |
24016 KB |
Output is correct |
14 |
Correct |
2598 ms |
29496 KB |
Output is correct |
15 |
Correct |
1918 ms |
30496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
10048 KB |
Output is correct |
2 |
Correct |
47 ms |
10144 KB |
Output is correct |
3 |
Correct |
55 ms |
10148 KB |
Output is correct |
4 |
Correct |
70 ms |
10176 KB |
Output is correct |
5 |
Correct |
438 ms |
10080 KB |
Output is correct |
6 |
Correct |
409 ms |
10300 KB |
Output is correct |
7 |
Correct |
1748 ms |
23584 KB |
Output is correct |
8 |
Correct |
3571 ms |
28504 KB |
Output is correct |
9 |
Correct |
54 ms |
10944 KB |
Output is correct |
10 |
Correct |
380 ms |
10608 KB |
Output is correct |
11 |
Correct |
2819 ms |
30568 KB |
Output is correct |
12 |
Correct |
3983 ms |
30628 KB |
Output is correct |
13 |
Correct |
3155 ms |
30496 KB |
Output is correct |
14 |
Correct |
3935 ms |
30480 KB |
Output is correct |
15 |
Correct |
2903 ms |
30684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
3320 KB |
Output is correct |
2 |
Correct |
5 ms |
3064 KB |
Output is correct |
3 |
Correct |
16 ms |
3064 KB |
Output is correct |
4 |
Correct |
7 ms |
3064 KB |
Output is correct |
5 |
Correct |
11 ms |
3320 KB |
Output is correct |
6 |
Correct |
32 ms |
4088 KB |
Output is correct |
7 |
Correct |
6 ms |
3156 KB |
Output is correct |
8 |
Correct |
7 ms |
3180 KB |
Output is correct |
9 |
Correct |
13 ms |
3576 KB |
Output is correct |
10 |
Correct |
26 ms |
3576 KB |
Output is correct |
11 |
Correct |
52 ms |
4136 KB |
Output is correct |
12 |
Correct |
39 ms |
4088 KB |
Output is correct |
13 |
Correct |
38 ms |
10176 KB |
Output is correct |
14 |
Correct |
44 ms |
10272 KB |
Output is correct |
15 |
Correct |
219 ms |
14428 KB |
Output is correct |
16 |
Correct |
66 ms |
10176 KB |
Output is correct |
17 |
Correct |
307 ms |
15020 KB |
Output is correct |
18 |
Correct |
352 ms |
10168 KB |
Output is correct |
19 |
Correct |
2201 ms |
28016 KB |
Output is correct |
20 |
Correct |
356 ms |
18456 KB |
Output is correct |
21 |
Correct |
43 ms |
10940 KB |
Output is correct |
22 |
Correct |
51 ms |
10932 KB |
Output is correct |
23 |
Correct |
224 ms |
10580 KB |
Output is correct |
24 |
Correct |
350 ms |
19376 KB |
Output is correct |
25 |
Correct |
1292 ms |
24016 KB |
Output is correct |
26 |
Correct |
2598 ms |
29496 KB |
Output is correct |
27 |
Correct |
1918 ms |
30496 KB |
Output is correct |
28 |
Correct |
37 ms |
10048 KB |
Output is correct |
29 |
Correct |
47 ms |
10144 KB |
Output is correct |
30 |
Correct |
55 ms |
10148 KB |
Output is correct |
31 |
Correct |
70 ms |
10176 KB |
Output is correct |
32 |
Correct |
438 ms |
10080 KB |
Output is correct |
33 |
Correct |
409 ms |
10300 KB |
Output is correct |
34 |
Correct |
1748 ms |
23584 KB |
Output is correct |
35 |
Correct |
3571 ms |
28504 KB |
Output is correct |
36 |
Correct |
54 ms |
10944 KB |
Output is correct |
37 |
Correct |
380 ms |
10608 KB |
Output is correct |
38 |
Correct |
2819 ms |
30568 KB |
Output is correct |
39 |
Correct |
3983 ms |
30628 KB |
Output is correct |
40 |
Correct |
3155 ms |
30496 KB |
Output is correct |
41 |
Correct |
3935 ms |
30480 KB |
Output is correct |
42 |
Correct |
2903 ms |
30684 KB |
Output is correct |
43 |
Correct |
332 ms |
17896 KB |
Output is correct |
44 |
Correct |
2521 ms |
28060 KB |
Output is correct |
45 |
Correct |
403 ms |
18408 KB |
Output is correct |
46 |
Correct |
3840 ms |
28520 KB |
Output is correct |
47 |
Correct |
57 ms |
10944 KB |
Output is correct |
48 |
Correct |
61 ms |
11040 KB |
Output is correct |
49 |
Correct |
275 ms |
10864 KB |
Output is correct |
50 |
Correct |
600 ms |
12576 KB |
Output is correct |
51 |
Correct |
432 ms |
19432 KB |
Output is correct |
52 |
Correct |
1118 ms |
21928 KB |
Output is correct |
53 |
Correct |
975 ms |
21568 KB |
Output is correct |
54 |
Correct |
1866 ms |
24268 KB |
Output is correct |
55 |
Correct |
1545 ms |
24168 KB |
Output is correct |
56 |
Correct |
2206 ms |
26088 KB |
Output is correct |
57 |
Correct |
2842 ms |
28464 KB |
Output is correct |
58 |
Correct |
3535 ms |
28920 KB |
Output is correct |
59 |
Correct |
3162 ms |
30636 KB |
Output is correct |
60 |
Correct |
4046 ms |
30432 KB |
Output is correct |