#include <bits/stdc++.h>
using namespace std;
int par[710];
int root(int x)
{
return x == par[x] ? x : (par[x] = root(par[x]));
}
bool merge(int x, int y)
{
x = root(x);
y = root(y);
if(x == y)
return 0;
par[x] = y;
return 1;
}
pair<int, int> edg[150000];
vector<pair<int, int>> seg[270000];
void init(int x, int l, int r)
{
if(l == r)
{
seg[x].push_back(edg[l]);
return;
}
init(2 * x, l, (l + r) / 2);
init(2 * x + 1, (l + r) / 2 + 1, r);
for(auto [a, b] : seg[2 * x])
{
par[a] = a;
par[b] = b;
}
for(auto [a, b] : seg[2 * x + 1])
{
par[a] = a;
par[b] = b;
}
for(auto [a, b] : seg[2 * x])
if(merge(a, b))
seg[x].push_back({ a, b });
for(auto [a, b] : seg[2 * x + 1])
if(merge(a, b))
seg[x].push_back({ a, b });
}
int get_val(int x, int l, int r, int s, int g)
{
if(g < l || r < s || g < s)
return 0;
if(s <= l && r <= g)
{
int c = 0;
for(auto [a, b] : seg[x])
if(merge(a, b))
c++;
return c;
}
return get_val(2 * x, l, (l + r) / 2, s, g) + get_val(2 * x + 1, (l + r) / 2 + 1, r, s, g);
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++)
{
int x, y;
scanf("%d%d", &x, &y);
edg[i] = { x, y };
}
init(1, 1, m);
int q;
scanf("%d", &q);
for(int i = 0; i < q; i++)
{
int x, y;
scanf("%d%d", &x, &y);
for(int i = 1; i <= n; i++)
par[i] = i;
int r = n - get_val(1, 1, m, 1, x - 1) - get_val(1, 1, m, y + 1, m);
printf("%d\n", r);
}
return 0;
}
Compilation message
destroy.cpp: In function 'int main()':
destroy.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
destroy.cpp:77:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~
destroy.cpp:84:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
destroy.cpp:88:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
6656 KB |
Output is correct |
2 |
Correct |
11 ms |
6656 KB |
Output is correct |
3 |
Correct |
9 ms |
6656 KB |
Output is correct |
4 |
Correct |
9 ms |
6656 KB |
Output is correct |
5 |
Correct |
9 ms |
6656 KB |
Output is correct |
6 |
Correct |
9 ms |
6656 KB |
Output is correct |
7 |
Correct |
9 ms |
6656 KB |
Output is correct |
8 |
Correct |
9 ms |
6656 KB |
Output is correct |
9 |
Correct |
9 ms |
6656 KB |
Output is correct |
10 |
Correct |
9 ms |
6656 KB |
Output is correct |
11 |
Correct |
9 ms |
6784 KB |
Output is correct |
12 |
Correct |
9 ms |
6656 KB |
Output is correct |
13 |
Correct |
9 ms |
6656 KB |
Output is correct |
14 |
Correct |
9 ms |
6656 KB |
Output is correct |
15 |
Correct |
9 ms |
6656 KB |
Output is correct |
16 |
Correct |
8 ms |
6656 KB |
Output is correct |
17 |
Correct |
8 ms |
6656 KB |
Output is correct |
18 |
Correct |
9 ms |
6656 KB |
Output is correct |
19 |
Correct |
8 ms |
6656 KB |
Output is correct |
20 |
Correct |
8 ms |
6656 KB |
Output is correct |
21 |
Correct |
8 ms |
6656 KB |
Output is correct |
22 |
Correct |
9 ms |
6656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
8832 KB |
Output is correct |
2 |
Correct |
77 ms |
8952 KB |
Output is correct |
3 |
Correct |
84 ms |
8960 KB |
Output is correct |
4 |
Correct |
115 ms |
14712 KB |
Output is correct |
5 |
Correct |
113 ms |
14712 KB |
Output is correct |
6 |
Correct |
772 ms |
14828 KB |
Output is correct |
7 |
Correct |
742 ms |
14584 KB |
Output is correct |
8 |
Correct |
720 ms |
14744 KB |
Output is correct |
9 |
Correct |
57 ms |
7032 KB |
Output is correct |
10 |
Correct |
56 ms |
7032 KB |
Output is correct |
11 |
Correct |
55 ms |
7008 KB |
Output is correct |
12 |
Correct |
113 ms |
14712 KB |
Output is correct |
13 |
Correct |
114 ms |
14840 KB |
Output is correct |
14 |
Correct |
113 ms |
14712 KB |
Output is correct |
15 |
Correct |
44 ms |
6904 KB |
Output is correct |
16 |
Correct |
50 ms |
6912 KB |
Output is correct |
17 |
Correct |
20 ms |
6784 KB |
Output is correct |
18 |
Correct |
17 ms |
6912 KB |
Output is correct |
19 |
Correct |
14 ms |
6784 KB |
Output is correct |
20 |
Correct |
13 ms |
6784 KB |
Output is correct |
21 |
Correct |
10 ms |
6784 KB |
Output is correct |
22 |
Correct |
10 ms |
6656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2437 ms |
16312 KB |
Output is correct |
2 |
Correct |
2402 ms |
15924 KB |
Output is correct |
3 |
Execution timed out |
2576 ms |
26648 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |