# |
제출 시각 |
아이디 |
문제 |
언어 |
결과 |
실행 시간 |
메모리 |
1037635 |
2024-07-29T06:16:40 Z |
변재우(#10984) |
Tourism (JOI23_tourism) |
C++17 |
|
5000 ms |
33364 KB |
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int Nmax=100010, sqrtN=300, S=1<<17;
int N, M, Q, C[Nmax], p, L[Nmax], R[Nmax], D[Nmax], P[20][Nmax];
int curr, par, cnt[Nmax], ans[Nmax];
vector<int> adj[Nmax];
struct Query {int l, r, i;}X[Nmax];
int LCA(int u, int v) {
if(!u || !v) return 0;
if(D[u]<D[v]) swap(u, v);
for(int i=16; i>=0; i--) if(D[P[i][u]]>D[v]) u=P[i][u];
if(D[u]!=D[v]) u=P[0][u];
for(int i=16; i>=0; i--) if(P[i][u]!=P[i][v]) u=P[i][u], v=P[i][v];
if(u!=v) u=P[0][u];
return u;
}
class Seg {
public:
void Update(int k, int v) {Update(1, 1, S, k, v);}
int Query(int l, int r) {return Query(1, 1, S, l, r);}
private:
int Tree[2*S];
void Update(int node, int s, int e, int k, int v) {
if(s==e) {
Tree[node]=v;
return;
}
int lch=2*node, rch=lch+1, m=(s+e)/2;
if(k<=m) Update(lch, s, m, k, v);
else Update(rch, m+1, e, k, v);
Tree[node]=Tree[lch]+Tree[rch];
}
int Query(int node, int s, int e, int l, int r) {
if(s>r || l>e) return 0;
if(l<=s && e<=r) return Tree[node];
int lch=2*node, rch=lch+1, m=(s+e)/2;
return Query(lch, s, m, l, r)+Query(rch, m+1, e, l, r);
}
}T;
class LSeg {
public:
void Update(int k, int v) {Update(1, 1, S, k, v);}
int Query(int l, int r) {return Query(1, 1, S, l, r);}
private:
int Tree[2*S];
void Update(int node, int s, int e, int k, int v) {
if(s==e) {
Tree[node]=v;
return;
}
int lch=2*node, rch=lch+1, m=(s+e)/2;
if(k<=m) Update(lch, s, m, k, v);
else Update(rch, m+1, e, k, v);
Tree[node]=LCA(Tree[lch], Tree[rch]);
}
int Query(int node, int s, int e, int l, int r) {
if(s>r || l>e) return 0;
if(l<=s && e<=r) return Tree[node];
int lch=2*node, rch=lch+1, m=(s+e)/2;
if(r<=m) return Query(lch, s, m, l, r);
if(l>=m+1) return Query(rch, m+1, e, l, r);
return LCA(Query(lch, s, m, l, r), Query(rch, m+1, e, l, r));
}
}TL;
void DFS(int curr, int prev) {
L[curr]=++p;
for(int next:adj[curr]) if(next!=prev) D[next]=D[curr]+1, P[0][next]=curr, DFS(next, curr);
R[curr]=p;
}
void Add(int x) {
if(!par) {
par=x, curr=1;
cnt[x]++, T.Update(L[x], 1);
return;
}
cnt[x]++;
if(cnt[x]>1) return;
if(!T.Query(L[x], R[x])) {
int y=x;
for(int i=16; i>=0; i--) if(P[i][y] && !T.Query(L[P[i][y]], R[P[i][y]])) y=P[i][y];
y=P[0][y];
curr+=D[x]-D[y];
}
T.Update(L[x], 1);
int np=LCA(par, x);
curr+=D[par]-D[np], par=np;
return;
}
void Delete(int x, int tl, int tr) {
cnt[x]--;
if(cnt[x]) return;
T.Update(L[x], 0);
if(!T.Query(L[x], R[x])) {
int y=x;
for(int i=16; i>=0; i--) if(P[i][y] && !T.Query(L[P[i][y]], R[P[i][y]])) y=P[i][y];
y=P[0][y];
curr-=D[x]-D[y];
}
int np=TL.Query(tl, tr);
curr-=D[np]-D[par], par=np;
return;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>N>>M>>Q;
for(int i=1; i<N; i++) {
int u, v; cin>>u>>v;
adj[u].push_back(v), adj[v].push_back(u);
}
DFS(1, 0);
for(int i=1; i<20; i++) for(int j=1; j<=N; j++) P[i][j]=P[i-1][P[i-1][j]];
for(int i=1; i<=M; i++) cin>>C[i], TL.Update(i, C[i]);
for(int i=1; i<=Q; i++) cin>>X[i].l>>X[i].r, X[i].i=i;
sort(X+1, X+Q+1, [](Query a, Query b) {return ((a.l/sqrtN)!=(b.l/sqrtN))?((a.l/sqrtN)<(b.l/sqrtN)):(a.r<b.r);});
for(int i=X[1].l; i<=X[1].r; i++) Add(C[i]);
ans[X[1].i]=curr;
for(int i=2, l=X[1].l, r=X[1].r; i<=Q; i++) {
while(l>X[i].l) Add(C[--l]);
while(r<X[i].r) Add(C[++r]);
while(l<X[i].l) Delete(C[l], l+1, r), l++;
while(r>X[i].r) Delete(C[r], l, r-1), r--;
ans[X[i].i]=curr;
}
for(int i=1; i<=Q; i++) cout<<ans[i]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
22872 KB |
Output is correct |
2 |
Correct |
2 ms |
22876 KB |
Output is correct |
3 |
Correct |
2 ms |
22876 KB |
Output is correct |
4 |
Correct |
7 ms |
22876 KB |
Output is correct |
5 |
Correct |
8 ms |
23108 KB |
Output is correct |
6 |
Correct |
6 ms |
22876 KB |
Output is correct |
7 |
Correct |
7 ms |
22876 KB |
Output is correct |
8 |
Correct |
6 ms |
23060 KB |
Output is correct |
9 |
Correct |
12 ms |
23104 KB |
Output is correct |
10 |
Correct |
12 ms |
22876 KB |
Output is correct |
11 |
Correct |
11 ms |
22876 KB |
Output is correct |
12 |
Correct |
3 ms |
22876 KB |
Output is correct |
13 |
Correct |
3 ms |
22876 KB |
Output is correct |
14 |
Correct |
3 ms |
22876 KB |
Output is correct |
15 |
Correct |
10 ms |
23108 KB |
Output is correct |
16 |
Correct |
10 ms |
22872 KB |
Output is correct |
17 |
Correct |
10 ms |
23116 KB |
Output is correct |
18 |
Correct |
10 ms |
22876 KB |
Output is correct |
19 |
Correct |
9 ms |
23108 KB |
Output is correct |
20 |
Correct |
9 ms |
22876 KB |
Output is correct |
21 |
Correct |
11 ms |
22876 KB |
Output is correct |
22 |
Correct |
12 ms |
23116 KB |
Output is correct |
23 |
Correct |
9 ms |
22876 KB |
Output is correct |
24 |
Correct |
11 ms |
23112 KB |
Output is correct |
25 |
Correct |
13 ms |
22876 KB |
Output is correct |
26 |
Correct |
11 ms |
23056 KB |
Output is correct |
27 |
Correct |
2 ms |
24924 KB |
Output is correct |
28 |
Correct |
3 ms |
22872 KB |
Output is correct |
29 |
Correct |
3 ms |
22872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
22872 KB |
Output is correct |
2 |
Correct |
2 ms |
22876 KB |
Output is correct |
3 |
Correct |
2 ms |
22876 KB |
Output is correct |
4 |
Correct |
7 ms |
22876 KB |
Output is correct |
5 |
Correct |
8 ms |
23108 KB |
Output is correct |
6 |
Correct |
6 ms |
22876 KB |
Output is correct |
7 |
Correct |
7 ms |
22876 KB |
Output is correct |
8 |
Correct |
6 ms |
23060 KB |
Output is correct |
9 |
Correct |
12 ms |
23104 KB |
Output is correct |
10 |
Correct |
12 ms |
22876 KB |
Output is correct |
11 |
Correct |
11 ms |
22876 KB |
Output is correct |
12 |
Correct |
3 ms |
22876 KB |
Output is correct |
13 |
Correct |
3 ms |
22876 KB |
Output is correct |
14 |
Correct |
3 ms |
22876 KB |
Output is correct |
15 |
Correct |
10 ms |
23108 KB |
Output is correct |
16 |
Correct |
10 ms |
22872 KB |
Output is correct |
17 |
Correct |
10 ms |
23116 KB |
Output is correct |
18 |
Correct |
10 ms |
22876 KB |
Output is correct |
19 |
Correct |
9 ms |
23108 KB |
Output is correct |
20 |
Correct |
9 ms |
22876 KB |
Output is correct |
21 |
Correct |
11 ms |
22876 KB |
Output is correct |
22 |
Correct |
12 ms |
23116 KB |
Output is correct |
23 |
Correct |
9 ms |
22876 KB |
Output is correct |
24 |
Correct |
11 ms |
23112 KB |
Output is correct |
25 |
Correct |
13 ms |
22876 KB |
Output is correct |
26 |
Correct |
11 ms |
23056 KB |
Output is correct |
27 |
Correct |
2 ms |
24924 KB |
Output is correct |
28 |
Correct |
3 ms |
22872 KB |
Output is correct |
29 |
Correct |
3 ms |
22872 KB |
Output is correct |
30 |
Correct |
79 ms |
23132 KB |
Output is correct |
31 |
Correct |
89 ms |
23128 KB |
Output is correct |
32 |
Correct |
114 ms |
23132 KB |
Output is correct |
33 |
Correct |
122 ms |
23132 KB |
Output is correct |
34 |
Correct |
115 ms |
23132 KB |
Output is correct |
35 |
Correct |
13 ms |
23132 KB |
Output is correct |
36 |
Correct |
13 ms |
23240 KB |
Output is correct |
37 |
Correct |
13 ms |
23236 KB |
Output is correct |
38 |
Correct |
89 ms |
23292 KB |
Output is correct |
39 |
Correct |
93 ms |
23128 KB |
Output is correct |
40 |
Correct |
107 ms |
23132 KB |
Output is correct |
41 |
Correct |
12 ms |
23132 KB |
Output is correct |
42 |
Correct |
13 ms |
23132 KB |
Output is correct |
43 |
Correct |
13 ms |
23300 KB |
Output is correct |
44 |
Correct |
99 ms |
23132 KB |
Output is correct |
45 |
Correct |
97 ms |
23132 KB |
Output is correct |
46 |
Correct |
113 ms |
23376 KB |
Output is correct |
47 |
Correct |
12 ms |
23132 KB |
Output is correct |
48 |
Correct |
12 ms |
23248 KB |
Output is correct |
49 |
Correct |
13 ms |
23264 KB |
Output is correct |
50 |
Correct |
96 ms |
23132 KB |
Output is correct |
51 |
Correct |
91 ms |
23220 KB |
Output is correct |
52 |
Correct |
92 ms |
23132 KB |
Output is correct |
53 |
Correct |
107 ms |
23128 KB |
Output is correct |
54 |
Correct |
95 ms |
23224 KB |
Output is correct |
55 |
Correct |
94 ms |
23128 KB |
Output is correct |
56 |
Correct |
3 ms |
25136 KB |
Output is correct |
57 |
Correct |
2 ms |
23024 KB |
Output is correct |
58 |
Correct |
5 ms |
23132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
22872 KB |
Output is correct |
2 |
Correct |
2 ms |
24924 KB |
Output is correct |
3 |
Correct |
3 ms |
25084 KB |
Output is correct |
4 |
Execution timed out |
5072 ms |
32852 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
22876 KB |
Output is correct |
2 |
Correct |
160 ms |
27428 KB |
Output is correct |
3 |
Correct |
232 ms |
28244 KB |
Output is correct |
4 |
Correct |
214 ms |
28528 KB |
Output is correct |
5 |
Correct |
119 ms |
31144 KB |
Output is correct |
6 |
Correct |
226 ms |
31056 KB |
Output is correct |
7 |
Correct |
371 ms |
31264 KB |
Output is correct |
8 |
Correct |
383 ms |
31056 KB |
Output is correct |
9 |
Correct |
375 ms |
31060 KB |
Output is correct |
10 |
Correct |
412 ms |
31268 KB |
Output is correct |
11 |
Correct |
377 ms |
31152 KB |
Output is correct |
12 |
Correct |
315 ms |
31116 KB |
Output is correct |
13 |
Correct |
307 ms |
33108 KB |
Output is correct |
14 |
Correct |
251 ms |
33364 KB |
Output is correct |
15 |
Correct |
276 ms |
33340 KB |
Output is correct |
16 |
Correct |
315 ms |
33204 KB |
Output is correct |
17 |
Correct |
307 ms |
33316 KB |
Output is correct |
18 |
Correct |
289 ms |
33108 KB |
Output is correct |
19 |
Correct |
114 ms |
30548 KB |
Output is correct |
20 |
Correct |
161 ms |
30544 KB |
Output is correct |
21 |
Correct |
261 ms |
30672 KB |
Output is correct |
22 |
Correct |
314 ms |
30584 KB |
Output is correct |
23 |
Correct |
339 ms |
31000 KB |
Output is correct |
24 |
Correct |
388 ms |
30688 KB |
Output is correct |
25 |
Correct |
409 ms |
30904 KB |
Output is correct |
26 |
Correct |
481 ms |
30808 KB |
Output is correct |
27 |
Correct |
489 ms |
30804 KB |
Output is correct |
28 |
Correct |
497 ms |
30752 KB |
Output is correct |
29 |
Correct |
559 ms |
30664 KB |
Output is correct |
30 |
Correct |
541 ms |
30756 KB |
Output is correct |
31 |
Correct |
504 ms |
32836 KB |
Output is correct |
32 |
Correct |
458 ms |
32856 KB |
Output is correct |
33 |
Correct |
490 ms |
32848 KB |
Output is correct |
34 |
Correct |
478 ms |
32896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
22876 KB |
Output is correct |
2 |
Correct |
3 ms |
25088 KB |
Output is correct |
3 |
Correct |
3 ms |
24924 KB |
Output is correct |
4 |
Execution timed out |
5087 ms |
29972 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
22872 KB |
Output is correct |
2 |
Correct |
2 ms |
22876 KB |
Output is correct |
3 |
Correct |
2 ms |
22876 KB |
Output is correct |
4 |
Correct |
7 ms |
22876 KB |
Output is correct |
5 |
Correct |
8 ms |
23108 KB |
Output is correct |
6 |
Correct |
6 ms |
22876 KB |
Output is correct |
7 |
Correct |
7 ms |
22876 KB |
Output is correct |
8 |
Correct |
6 ms |
23060 KB |
Output is correct |
9 |
Correct |
12 ms |
23104 KB |
Output is correct |
10 |
Correct |
12 ms |
22876 KB |
Output is correct |
11 |
Correct |
11 ms |
22876 KB |
Output is correct |
12 |
Correct |
3 ms |
22876 KB |
Output is correct |
13 |
Correct |
3 ms |
22876 KB |
Output is correct |
14 |
Correct |
3 ms |
22876 KB |
Output is correct |
15 |
Correct |
10 ms |
23108 KB |
Output is correct |
16 |
Correct |
10 ms |
22872 KB |
Output is correct |
17 |
Correct |
10 ms |
23116 KB |
Output is correct |
18 |
Correct |
10 ms |
22876 KB |
Output is correct |
19 |
Correct |
9 ms |
23108 KB |
Output is correct |
20 |
Correct |
9 ms |
22876 KB |
Output is correct |
21 |
Correct |
11 ms |
22876 KB |
Output is correct |
22 |
Correct |
12 ms |
23116 KB |
Output is correct |
23 |
Correct |
9 ms |
22876 KB |
Output is correct |
24 |
Correct |
11 ms |
23112 KB |
Output is correct |
25 |
Correct |
13 ms |
22876 KB |
Output is correct |
26 |
Correct |
11 ms |
23056 KB |
Output is correct |
27 |
Correct |
2 ms |
24924 KB |
Output is correct |
28 |
Correct |
3 ms |
22872 KB |
Output is correct |
29 |
Correct |
3 ms |
22872 KB |
Output is correct |
30 |
Correct |
79 ms |
23132 KB |
Output is correct |
31 |
Correct |
89 ms |
23128 KB |
Output is correct |
32 |
Correct |
114 ms |
23132 KB |
Output is correct |
33 |
Correct |
122 ms |
23132 KB |
Output is correct |
34 |
Correct |
115 ms |
23132 KB |
Output is correct |
35 |
Correct |
13 ms |
23132 KB |
Output is correct |
36 |
Correct |
13 ms |
23240 KB |
Output is correct |
37 |
Correct |
13 ms |
23236 KB |
Output is correct |
38 |
Correct |
89 ms |
23292 KB |
Output is correct |
39 |
Correct |
93 ms |
23128 KB |
Output is correct |
40 |
Correct |
107 ms |
23132 KB |
Output is correct |
41 |
Correct |
12 ms |
23132 KB |
Output is correct |
42 |
Correct |
13 ms |
23132 KB |
Output is correct |
43 |
Correct |
13 ms |
23300 KB |
Output is correct |
44 |
Correct |
99 ms |
23132 KB |
Output is correct |
45 |
Correct |
97 ms |
23132 KB |
Output is correct |
46 |
Correct |
113 ms |
23376 KB |
Output is correct |
47 |
Correct |
12 ms |
23132 KB |
Output is correct |
48 |
Correct |
12 ms |
23248 KB |
Output is correct |
49 |
Correct |
13 ms |
23264 KB |
Output is correct |
50 |
Correct |
96 ms |
23132 KB |
Output is correct |
51 |
Correct |
91 ms |
23220 KB |
Output is correct |
52 |
Correct |
92 ms |
23132 KB |
Output is correct |
53 |
Correct |
107 ms |
23128 KB |
Output is correct |
54 |
Correct |
95 ms |
23224 KB |
Output is correct |
55 |
Correct |
94 ms |
23128 KB |
Output is correct |
56 |
Correct |
3 ms |
25136 KB |
Output is correct |
57 |
Correct |
2 ms |
23024 KB |
Output is correct |
58 |
Correct |
5 ms |
23132 KB |
Output is correct |
59 |
Correct |
2 ms |
22872 KB |
Output is correct |
60 |
Correct |
2 ms |
24924 KB |
Output is correct |
61 |
Correct |
3 ms |
25084 KB |
Output is correct |
62 |
Execution timed out |
5072 ms |
32852 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |