# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1037752 |
2024-07-29T07:46:04 Z |
변재우(#10984) |
Tourism (JOI23_tourism) |
C++17 |
|
5000 ms |
63304 KB |
#include <bits/stdc++.h>
#pragma gcc optimize("O3")
#define int long long
using namespace std;
const int Nmax=100010, sqrtN=500;
int N, M, Q, C[Nmax], p, L[Nmax], R[Nmax], D[Nmax], P[20][Nmax];
int curr, par, cnt[Nmax], ans[Nmax];
set<int> V;
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 LSeg {
public:
void Init() {
for(int i=1; i<=M; i++) lx[0][i]=rx[0][i]=C[i];
for(int i=1; i<=16; i++) {
for(int j=1; j<=M; j++) {
if(j+(1<<(i-1))<=M) lx[i][j]=LCA(lx[i-1][j], lx[i-1][j+(1<<(i-1))]);
if(j-(1<<(i-1))>=1) rx[i][j]=LCA(rx[i-1][j], rx[i-1][j-(1<<(i-1))]);
}
}
}
int Query(int l, int r) {
int t=(31-__builtin_clz(r-l+1));
return LCA(lx[t][l], rx[t][r]);
}
private:
int lx[20][Nmax], rx[20][Nmax];
}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]++, V.insert(L[x]);
return;
}
cnt[x]++;
if(cnt[x]>1) return;
auto q=V.lower_bound(L[x]);
if(q==V.end() || (*q)>R[x]) {
int y=x;
int rv=INT_MAX, lv=INT_MIN;
auto p=V.lower_bound(R[x]);
if(p!=V.end()) rv=(*p);
if(p!=V.begin()) lv=(*prev(p));
for(int i=16; i>=0; i--) if(P[i][y] && R[P[i][y]]<rv && L[P[i][y]]>lv) y=P[i][y];
y=P[0][y];
curr+=D[x]-D[y];
}
V.insert(L[x]);
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;
V.erase(L[x]);
auto q=V.lower_bound(L[x]);
if(q==V.end() || (*q)>R[x]) {
int y=x;
int rv=INT_MAX, lv=INT_MIN;
auto p=V.lower_bound(R[x]);
if(p!=V.end()) rv=(*p);
if(p!=V.begin()) lv=(*prev(p));
for(int i=16; i>=0; i--) if(P[i][y] && R[P[i][y]]<rv && L[P[i][y]]>lv) 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;
// int u=i, v=i+1;
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];
// for(int i=1; i<=M; i++) C[i]=i;
TL.Init();
for(int i=1; i<=Q; i++) cin>>X[i].l>>X[i].r, X[i].i=i;
// for(int i=1; i<=Q; i++) X[i].l=i, X[i].r=max(i, (M-(i%300)*300));
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;
}
Compilation message
tourism.cpp:2: warning: ignoring '#pragma gcc optimize' [-Wunknown-pragmas]
2 | #pragma gcc optimize("O3")
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
31068 KB |
Output is correct |
2 |
Correct |
3 ms |
31068 KB |
Output is correct |
3 |
Correct |
3 ms |
31068 KB |
Output is correct |
4 |
Correct |
5 ms |
37212 KB |
Output is correct |
5 |
Correct |
6 ms |
37212 KB |
Output is correct |
6 |
Correct |
5 ms |
37212 KB |
Output is correct |
7 |
Correct |
6 ms |
37208 KB |
Output is correct |
8 |
Correct |
5 ms |
37212 KB |
Output is correct |
9 |
Correct |
7 ms |
39504 KB |
Output is correct |
10 |
Correct |
7 ms |
39260 KB |
Output is correct |
11 |
Correct |
7 ms |
39468 KB |
Output is correct |
12 |
Correct |
4 ms |
39464 KB |
Output is correct |
13 |
Correct |
5 ms |
39260 KB |
Output is correct |
14 |
Correct |
4 ms |
39260 KB |
Output is correct |
15 |
Correct |
7 ms |
39260 KB |
Output is correct |
16 |
Correct |
7 ms |
39504 KB |
Output is correct |
17 |
Correct |
7 ms |
39260 KB |
Output is correct |
18 |
Correct |
7 ms |
39260 KB |
Output is correct |
19 |
Correct |
7 ms |
39260 KB |
Output is correct |
20 |
Correct |
7 ms |
39260 KB |
Output is correct |
21 |
Correct |
7 ms |
39256 KB |
Output is correct |
22 |
Correct |
7 ms |
39260 KB |
Output is correct |
23 |
Correct |
7 ms |
39260 KB |
Output is correct |
24 |
Correct |
7 ms |
39480 KB |
Output is correct |
25 |
Correct |
8 ms |
39516 KB |
Output is correct |
26 |
Correct |
7 ms |
39260 KB |
Output is correct |
27 |
Correct |
4 ms |
39260 KB |
Output is correct |
28 |
Correct |
3 ms |
24920 KB |
Output is correct |
29 |
Correct |
5 ms |
39260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
31068 KB |
Output is correct |
2 |
Correct |
3 ms |
31068 KB |
Output is correct |
3 |
Correct |
3 ms |
31068 KB |
Output is correct |
4 |
Correct |
5 ms |
37212 KB |
Output is correct |
5 |
Correct |
6 ms |
37212 KB |
Output is correct |
6 |
Correct |
5 ms |
37212 KB |
Output is correct |
7 |
Correct |
6 ms |
37208 KB |
Output is correct |
8 |
Correct |
5 ms |
37212 KB |
Output is correct |
9 |
Correct |
7 ms |
39504 KB |
Output is correct |
10 |
Correct |
7 ms |
39260 KB |
Output is correct |
11 |
Correct |
7 ms |
39468 KB |
Output is correct |
12 |
Correct |
4 ms |
39464 KB |
Output is correct |
13 |
Correct |
5 ms |
39260 KB |
Output is correct |
14 |
Correct |
4 ms |
39260 KB |
Output is correct |
15 |
Correct |
7 ms |
39260 KB |
Output is correct |
16 |
Correct |
7 ms |
39504 KB |
Output is correct |
17 |
Correct |
7 ms |
39260 KB |
Output is correct |
18 |
Correct |
7 ms |
39260 KB |
Output is correct |
19 |
Correct |
7 ms |
39260 KB |
Output is correct |
20 |
Correct |
7 ms |
39260 KB |
Output is correct |
21 |
Correct |
7 ms |
39256 KB |
Output is correct |
22 |
Correct |
7 ms |
39260 KB |
Output is correct |
23 |
Correct |
7 ms |
39260 KB |
Output is correct |
24 |
Correct |
7 ms |
39480 KB |
Output is correct |
25 |
Correct |
8 ms |
39516 KB |
Output is correct |
26 |
Correct |
7 ms |
39260 KB |
Output is correct |
27 |
Correct |
4 ms |
39260 KB |
Output is correct |
28 |
Correct |
3 ms |
24920 KB |
Output is correct |
29 |
Correct |
5 ms |
39260 KB |
Output is correct |
30 |
Correct |
50 ms |
43720 KB |
Output is correct |
31 |
Correct |
53 ms |
43612 KB |
Output is correct |
32 |
Correct |
65 ms |
43612 KB |
Output is correct |
33 |
Correct |
65 ms |
43764 KB |
Output is correct |
34 |
Correct |
61 ms |
43748 KB |
Output is correct |
35 |
Correct |
11 ms |
43612 KB |
Output is correct |
36 |
Correct |
11 ms |
43612 KB |
Output is correct |
37 |
Correct |
13 ms |
43612 KB |
Output is correct |
38 |
Correct |
55 ms |
43832 KB |
Output is correct |
39 |
Correct |
49 ms |
43836 KB |
Output is correct |
40 |
Correct |
50 ms |
43608 KB |
Output is correct |
41 |
Correct |
10 ms |
43868 KB |
Output is correct |
42 |
Correct |
10 ms |
43868 KB |
Output is correct |
43 |
Correct |
10 ms |
43612 KB |
Output is correct |
44 |
Correct |
52 ms |
43612 KB |
Output is correct |
45 |
Correct |
49 ms |
43612 KB |
Output is correct |
46 |
Correct |
56 ms |
43612 KB |
Output is correct |
47 |
Correct |
11 ms |
43864 KB |
Output is correct |
48 |
Correct |
12 ms |
43612 KB |
Output is correct |
49 |
Correct |
11 ms |
43612 KB |
Output is correct |
50 |
Correct |
68 ms |
43612 KB |
Output is correct |
51 |
Correct |
59 ms |
43612 KB |
Output is correct |
52 |
Correct |
60 ms |
43772 KB |
Output is correct |
53 |
Correct |
61 ms |
43608 KB |
Output is correct |
54 |
Correct |
58 ms |
43748 KB |
Output is correct |
55 |
Correct |
63 ms |
43612 KB |
Output is correct |
56 |
Correct |
9 ms |
43612 KB |
Output is correct |
57 |
Correct |
3 ms |
25180 KB |
Output is correct |
58 |
Correct |
10 ms |
43612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
31068 KB |
Output is correct |
2 |
Correct |
4 ms |
39444 KB |
Output is correct |
3 |
Correct |
9 ms |
43612 KB |
Output is correct |
4 |
Execution timed out |
5092 ms |
60988 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
31064 KB |
Output is correct |
2 |
Correct |
221 ms |
55752 KB |
Output is correct |
3 |
Correct |
316 ms |
55888 KB |
Output is correct |
4 |
Correct |
255 ms |
56144 KB |
Output is correct |
5 |
Correct |
371 ms |
63304 KB |
Output is correct |
6 |
Correct |
383 ms |
62788 KB |
Output is correct |
7 |
Correct |
415 ms |
61264 KB |
Output is correct |
8 |
Correct |
416 ms |
60756 KB |
Output is correct |
9 |
Correct |
400 ms |
60496 KB |
Output is correct |
10 |
Correct |
405 ms |
60492 KB |
Output is correct |
11 |
Correct |
376 ms |
60500 KB |
Output is correct |
12 |
Correct |
405 ms |
60756 KB |
Output is correct |
13 |
Correct |
373 ms |
60496 KB |
Output is correct |
14 |
Correct |
380 ms |
60496 KB |
Output is correct |
15 |
Correct |
392 ms |
60688 KB |
Output is correct |
16 |
Correct |
393 ms |
62036 KB |
Output is correct |
17 |
Correct |
382 ms |
62032 KB |
Output is correct |
18 |
Correct |
393 ms |
62024 KB |
Output is correct |
19 |
Correct |
378 ms |
62836 KB |
Output is correct |
20 |
Correct |
396 ms |
63060 KB |
Output is correct |
21 |
Correct |
413 ms |
61520 KB |
Output is correct |
22 |
Correct |
410 ms |
60756 KB |
Output is correct |
23 |
Correct |
420 ms |
60524 KB |
Output is correct |
24 |
Correct |
439 ms |
60496 KB |
Output is correct |
25 |
Correct |
433 ms |
60244 KB |
Output is correct |
26 |
Correct |
407 ms |
59988 KB |
Output is correct |
27 |
Correct |
433 ms |
59860 KB |
Output is correct |
28 |
Correct |
422 ms |
59980 KB |
Output is correct |
29 |
Correct |
428 ms |
59852 KB |
Output is correct |
30 |
Correct |
434 ms |
59988 KB |
Output is correct |
31 |
Correct |
443 ms |
59908 KB |
Output is correct |
32 |
Correct |
438 ms |
60244 KB |
Output is correct |
33 |
Correct |
429 ms |
59992 KB |
Output is correct |
34 |
Correct |
485 ms |
60164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
31064 KB |
Output is correct |
2 |
Correct |
4 ms |
39260 KB |
Output is correct |
3 |
Correct |
8 ms |
43612 KB |
Output is correct |
4 |
Execution timed out |
5065 ms |
57900 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
31068 KB |
Output is correct |
2 |
Correct |
3 ms |
31068 KB |
Output is correct |
3 |
Correct |
3 ms |
31068 KB |
Output is correct |
4 |
Correct |
5 ms |
37212 KB |
Output is correct |
5 |
Correct |
6 ms |
37212 KB |
Output is correct |
6 |
Correct |
5 ms |
37212 KB |
Output is correct |
7 |
Correct |
6 ms |
37208 KB |
Output is correct |
8 |
Correct |
5 ms |
37212 KB |
Output is correct |
9 |
Correct |
7 ms |
39504 KB |
Output is correct |
10 |
Correct |
7 ms |
39260 KB |
Output is correct |
11 |
Correct |
7 ms |
39468 KB |
Output is correct |
12 |
Correct |
4 ms |
39464 KB |
Output is correct |
13 |
Correct |
5 ms |
39260 KB |
Output is correct |
14 |
Correct |
4 ms |
39260 KB |
Output is correct |
15 |
Correct |
7 ms |
39260 KB |
Output is correct |
16 |
Correct |
7 ms |
39504 KB |
Output is correct |
17 |
Correct |
7 ms |
39260 KB |
Output is correct |
18 |
Correct |
7 ms |
39260 KB |
Output is correct |
19 |
Correct |
7 ms |
39260 KB |
Output is correct |
20 |
Correct |
7 ms |
39260 KB |
Output is correct |
21 |
Correct |
7 ms |
39256 KB |
Output is correct |
22 |
Correct |
7 ms |
39260 KB |
Output is correct |
23 |
Correct |
7 ms |
39260 KB |
Output is correct |
24 |
Correct |
7 ms |
39480 KB |
Output is correct |
25 |
Correct |
8 ms |
39516 KB |
Output is correct |
26 |
Correct |
7 ms |
39260 KB |
Output is correct |
27 |
Correct |
4 ms |
39260 KB |
Output is correct |
28 |
Correct |
3 ms |
24920 KB |
Output is correct |
29 |
Correct |
5 ms |
39260 KB |
Output is correct |
30 |
Correct |
50 ms |
43720 KB |
Output is correct |
31 |
Correct |
53 ms |
43612 KB |
Output is correct |
32 |
Correct |
65 ms |
43612 KB |
Output is correct |
33 |
Correct |
65 ms |
43764 KB |
Output is correct |
34 |
Correct |
61 ms |
43748 KB |
Output is correct |
35 |
Correct |
11 ms |
43612 KB |
Output is correct |
36 |
Correct |
11 ms |
43612 KB |
Output is correct |
37 |
Correct |
13 ms |
43612 KB |
Output is correct |
38 |
Correct |
55 ms |
43832 KB |
Output is correct |
39 |
Correct |
49 ms |
43836 KB |
Output is correct |
40 |
Correct |
50 ms |
43608 KB |
Output is correct |
41 |
Correct |
10 ms |
43868 KB |
Output is correct |
42 |
Correct |
10 ms |
43868 KB |
Output is correct |
43 |
Correct |
10 ms |
43612 KB |
Output is correct |
44 |
Correct |
52 ms |
43612 KB |
Output is correct |
45 |
Correct |
49 ms |
43612 KB |
Output is correct |
46 |
Correct |
56 ms |
43612 KB |
Output is correct |
47 |
Correct |
11 ms |
43864 KB |
Output is correct |
48 |
Correct |
12 ms |
43612 KB |
Output is correct |
49 |
Correct |
11 ms |
43612 KB |
Output is correct |
50 |
Correct |
68 ms |
43612 KB |
Output is correct |
51 |
Correct |
59 ms |
43612 KB |
Output is correct |
52 |
Correct |
60 ms |
43772 KB |
Output is correct |
53 |
Correct |
61 ms |
43608 KB |
Output is correct |
54 |
Correct |
58 ms |
43748 KB |
Output is correct |
55 |
Correct |
63 ms |
43612 KB |
Output is correct |
56 |
Correct |
9 ms |
43612 KB |
Output is correct |
57 |
Correct |
3 ms |
25180 KB |
Output is correct |
58 |
Correct |
10 ms |
43612 KB |
Output is correct |
59 |
Correct |
3 ms |
31068 KB |
Output is correct |
60 |
Correct |
4 ms |
39444 KB |
Output is correct |
61 |
Correct |
9 ms |
43612 KB |
Output is correct |
62 |
Execution timed out |
5092 ms |
60988 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |