# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
255932 |
2020-08-02T05:59:19 Z |
IgorI |
Collapse (JOI18_collapse) |
C++17 |
|
13767 ms |
42856 KB |
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
const int N = 302020;
int n;
int cuts[N];
int cutssz;
int root[N], sz[N];
int comp;
void Reset()
{
for (int i = 0; i < n; i++) root[i] = i, sz[i] = 1;
cutssz = 0;
comp = n;
}
int Root(int x)
{
if (x == root[x]) return root[x];
return Root(root[x]);
}
void Merge(int v, int u)
{
v = Root(v), u = Root(u);
if (v == u) return;
comp--;
if (sz[v] < sz[u])
{
cuts[cutssz++] = v;
sz[u] += sz[v];
root[v] = u;
}
else
{
cuts[cutssz++] = u;
sz[v] += sz[u];
root[u] = v;
}
}
void Cut()
{
cutssz--;
int x = cuts[cutssz];
sz[root[x]] -= sz[x];
root[x] = x;
comp++;
}
int v[N], u[N], L[N], R[N], ans[N], z[N], it[N];
vector<pair<int, pair<int, int> > > on_left[N];
vector<pair<int, pair<int, int> > > on_right[N];
void solve(int le, int ri, int gap_le, int gap_ri)
{
if (le + 1 == ri)
{
if (v[le] == -1)
{
int ss = cutssz;
for (int i = gap_le + 1; i <= z[le]; i++)
{
for (auto e : on_left[i])
{
if (e.second.first <= it[le] && it[le] < e.second.second)
{
Merge(e.first, i);
}
}
}
for (int i = gap_ri - 1; i > z[le]; i--)
{
for (auto e : on_right[i])
{
if (e.second.first <= it[le] && it[le] < e.second.second)
{
Merge(i, e.first);
}
}
}
ans[le] = comp;
while (cutssz != ss) Cut();
}
return;
}
int mi = (le + ri) / 2;
int ss = cutssz;
for (int i = le; i < mi; i++)
{
if (v[i] != -1 && R[i] != -1 && ri <= R[i]) Merge(v[i], u[i]);
}
solve(mi, ri, gap_le, gap_ri);
while (cutssz != ss) Cut();
for (int i = ri - 1; i >= mi; i--)
{
if (v[i] != -1 && L[i] != -1 && L[i] < le) Merge(v[i], u[i]);
}
solve(le, mi, gap_le, gap_ri);
while (cutssz != ss) Cut();
}
void DCP(vector<int> x, vector<int> y, vector<pair<int, int> > &t, vector<int> p, int gap_le, int gap_ri)
{
Reset();
map<pair<int, int>, int> open;
int T = 0;
int j = 0;
for (int i = 0; i < x.size(); i++)
{
if (y[i] <= gap_le || gap_ri <= x[i])
{
if (open.find({x[i], y[i]}) == open.end())
{
open[{x[i], y[i]}] = T;
v[T] = x[i];
u[T] = y[i];
T++;
}
else
{
int z = open[{x[i], y[i]}];
L[T] = z;
L[z] = -1;
R[T] = -1;
R[z] = T;
v[T] = x[i];
u[T] = y[i];
open.erase({x[i], y[i]});
T++;
}
}
while (j < t.size() && i == t[j].first)
{
v[T] = -1, u[T] = -1;
it[T] = t[j].first;
z[T] = p[j];
T++;
j++;
}
}
solve(0, T, gap_le, gap_ri);
j = 0;
for (int i = 0; i < T; i++)
{
if (v[i] == -1)
{
t[j].first = ans[i];
j++;
}
}
}
void solve_queries(int gap_le, int gap_ri, vector<int> x, vector<int> y, vector<int> w, vector<int> p, vector<int> &answer)
{
vector<pair<int, int> > times;
for (int i = 0; i < w.size(); i++)
{
if (gap_le <= p[i] && p[i] < gap_ri)
{
times.push_back({w[i], i});
}
}
sort(times.begin(), times.end());
vector<int> mp(times.size());
for (int i = 0; i < times.size(); i++)
{
mp[i] = p[times[i].second];
}
DCP(x, y, times, mp, gap_le, gap_ri);
for (int i = 0; i < times.size(); i++)
{
answer[times[i].second] = times[i].first;
}
}
const int K = 2600;
vector<int> simulateCollapse(int n0, vector<int> t, vector<int> x, vector<int> y, vector<int> w, vector<int> p)
{
n = n0;
int c = t.size();
int q = w.size();
set<pair<int, int> > s;
for (int i = 0; i < c; i++)
{
if (x[i] > y[i]) swap(x[i], y[i]);
if (s.find({x[i], y[i]}) == s.end())
{
s.insert({x[i], y[i]});
}
else
{
s.erase({x[i], y[i]});
}
}
while (s.size())
{
pair<int, int> d = *(s.begin());
s.erase(s.begin());
x.push_back(d.first);
y.push_back(d.second);
c++;
}
map<pair<int, int>, int> mm;
for (int i = 0; i < c; i++)
{
if (mm.find({x[i], y[i]}) == mm.end())
{
mm[{x[i], y[i]}] = i;
}
else
{
int z = mm[{x[i], y[i]}];
mm.erase({x[i], y[i]});
on_left[y[i]].push_back({x[i], {z, i}});
on_right[x[i]].push_back({y[i], {z, i}});
}
}
vector<int> answer(q);
vector<int> pleft(n);
vector<int> pright(n);
for (int i = 1; i < n; i++)
{
pleft[i] = pleft[i - 1] + on_left[i].size();
}
for (int i = n - 2; i >= 0; i--)
{
pright[i] = pright[i + 1] + on_right[i].size();
}
vector<pair<int, int> > e;
int GL = 0;
while (GL != n - 1)
{
int okay = GL + 1;
for (int HF = GL + 1; HF < n; HF++)
{
if (pleft[HF - 1] - pleft[GL] <= K && pright[GL + 1] - pright[HF] <= K)
okay = HF;
}
e.push_back({GL, okay});
GL = okay;
}
assert(e.size() <= 80);
for (auto ee : e)
{
solve_queries(ee.first, ee.second, x, y, w, p, answer);
}
return answer;
}
#ifdef LOCAL
int main()
{
int n, c, q;
cin >> n >> c >> q;
vector<int> t(c), x(c), y(c), w(q), p(q);
for (int i = 0; i < c; i++)
{
cin >> t[i] >> x[i] >> y[i];
}
for (int i = 0; i < q; i++)
{
cin >> w[i] >> p[i];
}
vector<int> ans = simulateCollapse(n, t, x, y, w, p);
for (int i = 0; i < q; i++)
{
cout << ans[i] << "\n";
}
}
#endif // LOCAL
Compilation message
collapse.cpp: In function 'void DCP(std::vector<int>, std::vector<int>, std::vector<std::pair<int, int> >&, std::vector<int>, int, int)':
collapse.cpp:118:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < x.size(); i++)
~~^~~~~~~~~~
collapse.cpp:142:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (j < t.size() && i == t[j].first)
~~^~~~~~~~~~
collapse.cpp: In function 'void solve_queries(int, int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>&)':
collapse.cpp:166:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < w.size(); i++)
~~^~~~~~~~~~
collapse.cpp:175:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < times.size(); i++)
~~^~~~~~~~~~~~~~
collapse.cpp:180:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < times.size(); i++)
~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
15168 KB |
Output is correct |
2 |
Correct |
13 ms |
14848 KB |
Output is correct |
3 |
Correct |
12 ms |
14848 KB |
Output is correct |
4 |
Correct |
13 ms |
14848 KB |
Output is correct |
5 |
Correct |
23 ms |
15148 KB |
Output is correct |
6 |
Correct |
63 ms |
15616 KB |
Output is correct |
7 |
Correct |
32 ms |
15032 KB |
Output is correct |
8 |
Correct |
33 ms |
14976 KB |
Output is correct |
9 |
Correct |
126 ms |
15232 KB |
Output is correct |
10 |
Correct |
109 ms |
15360 KB |
Output is correct |
11 |
Correct |
154 ms |
15760 KB |
Output is correct |
12 |
Correct |
120 ms |
15864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
20776 KB |
Output is correct |
2 |
Correct |
57 ms |
20848 KB |
Output is correct |
3 |
Correct |
290 ms |
28476 KB |
Output is correct |
4 |
Correct |
67 ms |
20848 KB |
Output is correct |
5 |
Correct |
994 ms |
28608 KB |
Output is correct |
6 |
Correct |
874 ms |
22492 KB |
Output is correct |
7 |
Correct |
4583 ms |
39988 KB |
Output is correct |
8 |
Correct |
1473 ms |
29372 KB |
Output is correct |
9 |
Correct |
8028 ms |
22636 KB |
Output is correct |
10 |
Correct |
8102 ms |
23104 KB |
Output is correct |
11 |
Correct |
10286 ms |
23396 KB |
Output is correct |
12 |
Correct |
2532 ms |
31744 KB |
Output is correct |
13 |
Correct |
7649 ms |
34820 KB |
Output is correct |
14 |
Correct |
11485 ms |
41228 KB |
Output is correct |
15 |
Correct |
7747 ms |
41816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
20844 KB |
Output is correct |
2 |
Correct |
58 ms |
20844 KB |
Output is correct |
3 |
Correct |
65 ms |
20844 KB |
Output is correct |
4 |
Correct |
69 ms |
20844 KB |
Output is correct |
5 |
Correct |
266 ms |
20848 KB |
Output is correct |
6 |
Correct |
767 ms |
20148 KB |
Output is correct |
7 |
Correct |
3398 ms |
32544 KB |
Output is correct |
8 |
Correct |
6413 ms |
38148 KB |
Output is correct |
9 |
Correct |
8932 ms |
22556 KB |
Output is correct |
10 |
Correct |
13767 ms |
22828 KB |
Output is correct |
11 |
Correct |
7591 ms |
39976 KB |
Output is correct |
12 |
Correct |
10692 ms |
40540 KB |
Output is correct |
13 |
Correct |
7913 ms |
39836 KB |
Output is correct |
14 |
Correct |
10320 ms |
40308 KB |
Output is correct |
15 |
Correct |
7828 ms |
39948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
15168 KB |
Output is correct |
2 |
Correct |
13 ms |
14848 KB |
Output is correct |
3 |
Correct |
12 ms |
14848 KB |
Output is correct |
4 |
Correct |
13 ms |
14848 KB |
Output is correct |
5 |
Correct |
23 ms |
15148 KB |
Output is correct |
6 |
Correct |
63 ms |
15616 KB |
Output is correct |
7 |
Correct |
32 ms |
15032 KB |
Output is correct |
8 |
Correct |
33 ms |
14976 KB |
Output is correct |
9 |
Correct |
126 ms |
15232 KB |
Output is correct |
10 |
Correct |
109 ms |
15360 KB |
Output is correct |
11 |
Correct |
154 ms |
15760 KB |
Output is correct |
12 |
Correct |
120 ms |
15864 KB |
Output is correct |
13 |
Correct |
49 ms |
20776 KB |
Output is correct |
14 |
Correct |
57 ms |
20848 KB |
Output is correct |
15 |
Correct |
290 ms |
28476 KB |
Output is correct |
16 |
Correct |
67 ms |
20848 KB |
Output is correct |
17 |
Correct |
994 ms |
28608 KB |
Output is correct |
18 |
Correct |
874 ms |
22492 KB |
Output is correct |
19 |
Correct |
4583 ms |
39988 KB |
Output is correct |
20 |
Correct |
1473 ms |
29372 KB |
Output is correct |
21 |
Correct |
8028 ms |
22636 KB |
Output is correct |
22 |
Correct |
8102 ms |
23104 KB |
Output is correct |
23 |
Correct |
10286 ms |
23396 KB |
Output is correct |
24 |
Correct |
2532 ms |
31744 KB |
Output is correct |
25 |
Correct |
7649 ms |
34820 KB |
Output is correct |
26 |
Correct |
11485 ms |
41228 KB |
Output is correct |
27 |
Correct |
7747 ms |
41816 KB |
Output is correct |
28 |
Correct |
44 ms |
20844 KB |
Output is correct |
29 |
Correct |
58 ms |
20844 KB |
Output is correct |
30 |
Correct |
65 ms |
20844 KB |
Output is correct |
31 |
Correct |
69 ms |
20844 KB |
Output is correct |
32 |
Correct |
266 ms |
20848 KB |
Output is correct |
33 |
Correct |
767 ms |
20148 KB |
Output is correct |
34 |
Correct |
3398 ms |
32544 KB |
Output is correct |
35 |
Correct |
6413 ms |
38148 KB |
Output is correct |
36 |
Correct |
8932 ms |
22556 KB |
Output is correct |
37 |
Correct |
13767 ms |
22828 KB |
Output is correct |
38 |
Correct |
7591 ms |
39976 KB |
Output is correct |
39 |
Correct |
10692 ms |
40540 KB |
Output is correct |
40 |
Correct |
7913 ms |
39836 KB |
Output is correct |
41 |
Correct |
10320 ms |
40308 KB |
Output is correct |
42 |
Correct |
7828 ms |
39948 KB |
Output is correct |
43 |
Correct |
1089 ms |
27996 KB |
Output is correct |
44 |
Correct |
5696 ms |
39604 KB |
Output is correct |
45 |
Correct |
1405 ms |
28600 KB |
Output is correct |
46 |
Correct |
7167 ms |
40200 KB |
Output is correct |
47 |
Correct |
9898 ms |
23148 KB |
Output is correct |
48 |
Correct |
8735 ms |
23148 KB |
Output is correct |
49 |
Correct |
11604 ms |
23604 KB |
Output is correct |
50 |
Correct |
6120 ms |
22776 KB |
Output is correct |
51 |
Correct |
2976 ms |
30364 KB |
Output is correct |
52 |
Correct |
4618 ms |
33032 KB |
Output is correct |
53 |
Correct |
3677 ms |
32684 KB |
Output is correct |
54 |
Correct |
5876 ms |
35512 KB |
Output is correct |
55 |
Correct |
5581 ms |
35248 KB |
Output is correct |
56 |
Correct |
6195 ms |
37472 KB |
Output is correct |
57 |
Correct |
7312 ms |
40040 KB |
Output is correct |
58 |
Correct |
9644 ms |
40300 KB |
Output is correct |
59 |
Correct |
7426 ms |
42600 KB |
Output is correct |
60 |
Correct |
10651 ms |
42856 KB |
Output is correct |