#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define pll pair<ll, ll>
#define MP make_pair
#define PB push_back
#define pii pair<int, int>
#define pi3 pair<pii, int>
#define ft first
#define sd second
#define MP3(a,b,c) MP(MP(a,b),c)
using namespace std;
typedef long long ll;
const int N = 50100;
const int M = 100100;
const int Q = 100100;
const int CO = 200100;
const int oo = 2e9;
const int B = 700;
const int MB = M / B + 10;
stack<pi3> stk;
vector<int> g[N], vc, ed[CO], vec, qr;
int n, m, U[M], V[M], D[M], q, ans[Q], tt = 0, mrk[M], loc[M];
int vls[B][B], tp[Q], l[Q], r[Q], siz[N], pr[N];
bool cmp(int x, int y){
return r[x] > r[y];
}
int get(int x){ return (x == pr[x] ? x : get(pr[x])); }
void link(int x, int y){
x = get(x);
y = get(y);
if (x == y) return;
if (siz[x] < siz[y]) swap(x, y);
stk.push(MP3(x, y, pr[y]));
pr[y] = x;
siz[x] += siz[y];
}
int main(){
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#else
ios_base::sync_with_stdio(0); cin.tie(0);
#endif // _LOCAL
cin >> n >> m;
for (int i = 0; i < m; i++){
cin >> U[i] >> V[i] >> D[i];
U[i]--; V[i]--;
g[U[i]].PB(i);
g[V[i]].PB(i);
vc.PB(D[i]);
}
cin >> q;
for (int i = 0; i < q; i++) {
cin >> tp[i] >> l[i] >> r[i];
l[i]--;
vc.PB(r[i]);
}
sort(all(vc));
vc.resize(unique(all(vc)) - vc.begin());
for (int i = 0; i < m; i++)
D[i] = lower_bound(all(vc), D[i]) - vc.begin();
for (int i = 0; i < q; i++)
r[i] = lower_bound(all(vc), r[i]) - vc.begin();
for (int it = 0; it < q; it += B){
int bd = min(q, it + B);
tt++;
vec.clear();
qr.clear();
for (int i = it; i < bd; i++)
if (tp[i] == 1) {
if (mrk[l[i]] < tt) {
mrk[l[i]] = tt;
loc[l[i]] = sz(vec);
vec.PB(l[i]);
}
} else qr.PB(i);
for (int i = it; i < bd; i++){
int nm = i - it;
if (i == it){
for (int j = 0; j < sz(vec); j++)
vls[nm][j] = D[vec[j]];
} else {
for (int j = 0; j < sz(vec); j++)
vls[nm][j] = vls[nm - 1][j];
}
if (tp[i] == 1)
vls[nm][loc[l[i]]] = r[i];
}
for (int i = 0; i < m; i++)
if (mrk[i] < tt)
ed[D[i]].PB(i);
for (int i = 0; i < n; i++){
siz[i] = 1;
pr[i] = i;
}
if (sz(qr)){
sort(all(qr), cmp);
}
while (sz(stk)) stk.pop();
int lst = sz(vc) - 1;
for (int nm : qr){
while (lst >= 0 && lst >= r[nm]){
if (sz(ed[lst])){
for (int nw : ed[lst])
link(U[nw], V[nw]);
ed[lst].clear();
}
lst--;
}
int mem = sz(stk);
int gt = nm - it;
for (int i = 0; i < sz(vec); i++)
if (vls[gt][i] >= r[nm])
link(U[vec[i]], V[vec[i]]);
ans[nm] = siz[get(l[nm])];
while (sz(stk) > mem){
pi3 cr = stk.top();
stk.pop();
siz[cr.ft.ft] -= siz[cr.ft.sd];
pr[cr.ft.sd] = cr.sd;
}
}
while (lst >= 0){
ed[lst].clear();
lst--;
}
//end
for (int i = 0; i < sz(vec); i++)
D[vec[i]] = vls[bd - it - 1][i];
}
for (int i = 0; i < q; i++)
if (tp[i] == 2)
cout << ans[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6272 KB |
Output is correct |
2 |
Correct |
8 ms |
6272 KB |
Output is correct |
3 |
Correct |
33 ms |
8572 KB |
Output is correct |
4 |
Correct |
14 ms |
6528 KB |
Output is correct |
5 |
Correct |
19 ms |
8448 KB |
Output is correct |
6 |
Correct |
17 ms |
8464 KB |
Output is correct |
7 |
Correct |
27 ms |
8448 KB |
Output is correct |
8 |
Correct |
22 ms |
8448 KB |
Output is correct |
9 |
Correct |
29 ms |
8448 KB |
Output is correct |
10 |
Correct |
22 ms |
8448 KB |
Output is correct |
11 |
Correct |
22 ms |
8448 KB |
Output is correct |
12 |
Correct |
23 ms |
8448 KB |
Output is correct |
13 |
Correct |
27 ms |
8448 KB |
Output is correct |
14 |
Correct |
24 ms |
8448 KB |
Output is correct |
15 |
Correct |
22 ms |
8448 KB |
Output is correct |
16 |
Correct |
21 ms |
8448 KB |
Output is correct |
17 |
Correct |
22 ms |
8448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1908 ms |
17052 KB |
Output is correct |
2 |
Correct |
1874 ms |
17092 KB |
Output is correct |
3 |
Correct |
1852 ms |
17120 KB |
Output is correct |
4 |
Correct |
1895 ms |
17068 KB |
Output is correct |
5 |
Correct |
1973 ms |
17168 KB |
Output is correct |
6 |
Correct |
2160 ms |
17240 KB |
Output is correct |
7 |
Correct |
2181 ms |
17256 KB |
Output is correct |
8 |
Correct |
2189 ms |
17360 KB |
Output is correct |
9 |
Correct |
88 ms |
8564 KB |
Output is correct |
10 |
Correct |
819 ms |
14352 KB |
Output is correct |
11 |
Correct |
759 ms |
14368 KB |
Output is correct |
12 |
Correct |
1266 ms |
16432 KB |
Output is correct |
13 |
Correct |
1764 ms |
18460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1293 ms |
15264 KB |
Output is correct |
2 |
Correct |
706 ms |
12692 KB |
Output is correct |
3 |
Correct |
1346 ms |
15592 KB |
Output is correct |
4 |
Correct |
1321 ms |
15420 KB |
Output is correct |
5 |
Correct |
84 ms |
8564 KB |
Output is correct |
6 |
Correct |
1447 ms |
15412 KB |
Output is correct |
7 |
Correct |
1282 ms |
15340 KB |
Output is correct |
8 |
Correct |
1131 ms |
15280 KB |
Output is correct |
9 |
Correct |
883 ms |
14336 KB |
Output is correct |
10 |
Correct |
832 ms |
14216 KB |
Output is correct |
11 |
Correct |
1114 ms |
16388 KB |
Output is correct |
12 |
Correct |
1116 ms |
16364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2965 ms |
16432 KB |
Output is correct |
2 |
Correct |
87 ms |
8564 KB |
Output is correct |
3 |
Correct |
239 ms |
12144 KB |
Output is correct |
4 |
Correct |
106 ms |
9716 KB |
Output is correct |
5 |
Correct |
793 ms |
13932 KB |
Output is correct |
6 |
Correct |
2838 ms |
16284 KB |
Output is correct |
7 |
Correct |
778 ms |
13936 KB |
Output is correct |
8 |
Correct |
1234 ms |
13352 KB |
Output is correct |
9 |
Correct |
1223 ms |
13456 KB |
Output is correct |
10 |
Correct |
950 ms |
13456 KB |
Output is correct |
11 |
Correct |
1967 ms |
14588 KB |
Output is correct |
12 |
Correct |
2111 ms |
14720 KB |
Output is correct |
13 |
Correct |
1713 ms |
14828 KB |
Output is correct |
14 |
Correct |
791 ms |
13880 KB |
Output is correct |
15 |
Correct |
789 ms |
13860 KB |
Output is correct |
16 |
Correct |
2800 ms |
15924 KB |
Output is correct |
17 |
Correct |
2660 ms |
15928 KB |
Output is correct |
18 |
Correct |
2360 ms |
16052 KB |
Output is correct |
19 |
Correct |
2354 ms |
15948 KB |
Output is correct |
20 |
Correct |
1529 ms |
15344 KB |
Output is correct |
21 |
Correct |
1473 ms |
15208 KB |
Output is correct |
22 |
Correct |
2179 ms |
15888 KB |
Output is correct |
23 |
Correct |
749 ms |
12776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1908 ms |
17052 KB |
Output is correct |
2 |
Correct |
1874 ms |
17092 KB |
Output is correct |
3 |
Correct |
1852 ms |
17120 KB |
Output is correct |
4 |
Correct |
1895 ms |
17068 KB |
Output is correct |
5 |
Correct |
1973 ms |
17168 KB |
Output is correct |
6 |
Correct |
2160 ms |
17240 KB |
Output is correct |
7 |
Correct |
2181 ms |
17256 KB |
Output is correct |
8 |
Correct |
2189 ms |
17360 KB |
Output is correct |
9 |
Correct |
88 ms |
8564 KB |
Output is correct |
10 |
Correct |
819 ms |
14352 KB |
Output is correct |
11 |
Correct |
759 ms |
14368 KB |
Output is correct |
12 |
Correct |
1266 ms |
16432 KB |
Output is correct |
13 |
Correct |
1764 ms |
18460 KB |
Output is correct |
14 |
Correct |
1293 ms |
15264 KB |
Output is correct |
15 |
Correct |
706 ms |
12692 KB |
Output is correct |
16 |
Correct |
1346 ms |
15592 KB |
Output is correct |
17 |
Correct |
1321 ms |
15420 KB |
Output is correct |
18 |
Correct |
84 ms |
8564 KB |
Output is correct |
19 |
Correct |
1447 ms |
15412 KB |
Output is correct |
20 |
Correct |
1282 ms |
15340 KB |
Output is correct |
21 |
Correct |
1131 ms |
15280 KB |
Output is correct |
22 |
Correct |
883 ms |
14336 KB |
Output is correct |
23 |
Correct |
832 ms |
14216 KB |
Output is correct |
24 |
Correct |
1114 ms |
16388 KB |
Output is correct |
25 |
Correct |
1116 ms |
16364 KB |
Output is correct |
26 |
Correct |
1796 ms |
17164 KB |
Output is correct |
27 |
Correct |
2065 ms |
17408 KB |
Output is correct |
28 |
Correct |
1946 ms |
17140 KB |
Output is correct |
29 |
Correct |
1628 ms |
17188 KB |
Output is correct |
30 |
Correct |
2063 ms |
17172 KB |
Output is correct |
31 |
Correct |
2050 ms |
17404 KB |
Output is correct |
32 |
Correct |
1990 ms |
17092 KB |
Output is correct |
33 |
Correct |
1823 ms |
17036 KB |
Output is correct |
34 |
Correct |
1947 ms |
17072 KB |
Output is correct |
35 |
Correct |
1847 ms |
17112 KB |
Output is correct |
36 |
Correct |
1586 ms |
17168 KB |
Output is correct |
37 |
Correct |
1612 ms |
17056 KB |
Output is correct |
38 |
Correct |
1631 ms |
17004 KB |
Output is correct |
39 |
Correct |
1275 ms |
15976 KB |
Output is correct |
40 |
Correct |
1340 ms |
15972 KB |
Output is correct |
41 |
Correct |
1313 ms |
16080 KB |
Output is correct |
42 |
Correct |
1564 ms |
18184 KB |
Output is correct |
43 |
Correct |
1643 ms |
18116 KB |
Output is correct |
44 |
Correct |
1625 ms |
18316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6272 KB |
Output is correct |
2 |
Correct |
8 ms |
6272 KB |
Output is correct |
3 |
Correct |
33 ms |
8572 KB |
Output is correct |
4 |
Correct |
14 ms |
6528 KB |
Output is correct |
5 |
Correct |
19 ms |
8448 KB |
Output is correct |
6 |
Correct |
17 ms |
8464 KB |
Output is correct |
7 |
Correct |
27 ms |
8448 KB |
Output is correct |
8 |
Correct |
22 ms |
8448 KB |
Output is correct |
9 |
Correct |
29 ms |
8448 KB |
Output is correct |
10 |
Correct |
22 ms |
8448 KB |
Output is correct |
11 |
Correct |
22 ms |
8448 KB |
Output is correct |
12 |
Correct |
23 ms |
8448 KB |
Output is correct |
13 |
Correct |
27 ms |
8448 KB |
Output is correct |
14 |
Correct |
24 ms |
8448 KB |
Output is correct |
15 |
Correct |
22 ms |
8448 KB |
Output is correct |
16 |
Correct |
21 ms |
8448 KB |
Output is correct |
17 |
Correct |
22 ms |
8448 KB |
Output is correct |
18 |
Correct |
1908 ms |
17052 KB |
Output is correct |
19 |
Correct |
1874 ms |
17092 KB |
Output is correct |
20 |
Correct |
1852 ms |
17120 KB |
Output is correct |
21 |
Correct |
1895 ms |
17068 KB |
Output is correct |
22 |
Correct |
1973 ms |
17168 KB |
Output is correct |
23 |
Correct |
2160 ms |
17240 KB |
Output is correct |
24 |
Correct |
2181 ms |
17256 KB |
Output is correct |
25 |
Correct |
2189 ms |
17360 KB |
Output is correct |
26 |
Correct |
88 ms |
8564 KB |
Output is correct |
27 |
Correct |
819 ms |
14352 KB |
Output is correct |
28 |
Correct |
759 ms |
14368 KB |
Output is correct |
29 |
Correct |
1266 ms |
16432 KB |
Output is correct |
30 |
Correct |
1764 ms |
18460 KB |
Output is correct |
31 |
Correct |
1293 ms |
15264 KB |
Output is correct |
32 |
Correct |
706 ms |
12692 KB |
Output is correct |
33 |
Correct |
1346 ms |
15592 KB |
Output is correct |
34 |
Correct |
1321 ms |
15420 KB |
Output is correct |
35 |
Correct |
84 ms |
8564 KB |
Output is correct |
36 |
Correct |
1447 ms |
15412 KB |
Output is correct |
37 |
Correct |
1282 ms |
15340 KB |
Output is correct |
38 |
Correct |
1131 ms |
15280 KB |
Output is correct |
39 |
Correct |
883 ms |
14336 KB |
Output is correct |
40 |
Correct |
832 ms |
14216 KB |
Output is correct |
41 |
Correct |
1114 ms |
16388 KB |
Output is correct |
42 |
Correct |
1116 ms |
16364 KB |
Output is correct |
43 |
Correct |
2965 ms |
16432 KB |
Output is correct |
44 |
Correct |
87 ms |
8564 KB |
Output is correct |
45 |
Correct |
239 ms |
12144 KB |
Output is correct |
46 |
Correct |
106 ms |
9716 KB |
Output is correct |
47 |
Correct |
793 ms |
13932 KB |
Output is correct |
48 |
Correct |
2838 ms |
16284 KB |
Output is correct |
49 |
Correct |
778 ms |
13936 KB |
Output is correct |
50 |
Correct |
1234 ms |
13352 KB |
Output is correct |
51 |
Correct |
1223 ms |
13456 KB |
Output is correct |
52 |
Correct |
950 ms |
13456 KB |
Output is correct |
53 |
Correct |
1967 ms |
14588 KB |
Output is correct |
54 |
Correct |
2111 ms |
14720 KB |
Output is correct |
55 |
Correct |
1713 ms |
14828 KB |
Output is correct |
56 |
Correct |
791 ms |
13880 KB |
Output is correct |
57 |
Correct |
789 ms |
13860 KB |
Output is correct |
58 |
Correct |
2800 ms |
15924 KB |
Output is correct |
59 |
Correct |
2660 ms |
15928 KB |
Output is correct |
60 |
Correct |
2360 ms |
16052 KB |
Output is correct |
61 |
Correct |
2354 ms |
15948 KB |
Output is correct |
62 |
Correct |
1529 ms |
15344 KB |
Output is correct |
63 |
Correct |
1473 ms |
15208 KB |
Output is correct |
64 |
Correct |
2179 ms |
15888 KB |
Output is correct |
65 |
Correct |
749 ms |
12776 KB |
Output is correct |
66 |
Correct |
1796 ms |
17164 KB |
Output is correct |
67 |
Correct |
2065 ms |
17408 KB |
Output is correct |
68 |
Correct |
1946 ms |
17140 KB |
Output is correct |
69 |
Correct |
1628 ms |
17188 KB |
Output is correct |
70 |
Correct |
2063 ms |
17172 KB |
Output is correct |
71 |
Correct |
2050 ms |
17404 KB |
Output is correct |
72 |
Correct |
1990 ms |
17092 KB |
Output is correct |
73 |
Correct |
1823 ms |
17036 KB |
Output is correct |
74 |
Correct |
1947 ms |
17072 KB |
Output is correct |
75 |
Correct |
1847 ms |
17112 KB |
Output is correct |
76 |
Correct |
1586 ms |
17168 KB |
Output is correct |
77 |
Correct |
1612 ms |
17056 KB |
Output is correct |
78 |
Correct |
1631 ms |
17004 KB |
Output is correct |
79 |
Correct |
1275 ms |
15976 KB |
Output is correct |
80 |
Correct |
1340 ms |
15972 KB |
Output is correct |
81 |
Correct |
1313 ms |
16080 KB |
Output is correct |
82 |
Correct |
1564 ms |
18184 KB |
Output is correct |
83 |
Correct |
1643 ms |
18116 KB |
Output is correct |
84 |
Correct |
1625 ms |
18316 KB |
Output is correct |
85 |
Execution timed out |
3067 ms |
19932 KB |
Time limit exceeded |
86 |
Halted |
0 ms |
0 KB |
- |