Submission #737814

# Submission time Handle Problem Language Result Execution time Memory
737814 2023-05-07T18:40:31 Z arnold518 Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 24940 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;
const int SQ = 300;

int N, M, Q;
vector<int> adj[MAXN+10];
int A[MAXN+10];

struct Data
{
	int l, r, k;
};
Data B[MAXN+10];

multiset<pii> S;

int dfn[MAXN+10], cnt, par[MAXN+10][30], dep[MAXN+10];
void dfs(int now, int bef, int d)
{
	dfn[now]=++cnt;
	dep[now]=d;
	par[now][0]=bef;
	for(auto nxt : adj[now])
	{
		if(nxt==bef) continue;
		dfs(nxt, now, d+1);
	}
}

int lca(int u, int v)
{
	if(dep[u]>dep[v]) swap(u, v);
	for(int i=20; i>=0; i--) if(dep[u]<=dep[par[v][i]]) v=par[v][i];
	if(u==v) return u;
	for(int i=20; i>=0; i--) if(par[u][i]!=par[v][i]) u=par[u][i], v=par[v][i];
	return par[u][0];
}

int dist(int u, int v)
{
	return dep[u]+dep[v]-dep[lca(u, v)]*2;
}

int ans=0, ans1[MAXN+10];

void push(int x)
{
	auto it=S.lower_bound({dfn[x], x});
	set<pii>::iterator lt, rt;
	if(it==S.end()) lt=prev(it), rt=S.begin();
	else if(it==S.begin()) rt=it, lt=prev(S.end());
	else rt=it, lt=prev(it);
	S.insert({dfn[x], x});
	ans-=dist(rt->second, lt->second);
	ans+=dist(rt->second, x);
	ans+=dist(lt->second, x);
	//printf("PUSH %d %d\n", x, ans);
}

void pop(int x)
{
	auto it=S.lower_bound({dfn[x], x});
	set<pii>::iterator lt, rt;
	if(it==S.begin()) lt=prev(S.end());
	else lt=prev(it);
	if(it==prev(S.end())) rt=S.begin();
	else rt=next(it);
	S.erase(S.find({dfn[x], x}));
	ans+=dist(rt->second, lt->second);
	ans-=dist(rt->second, x);
	ans-=dist(lt->second, x);
	//printf("POP %d %d\n", x, ans);
}

int main()
{
	scanf("%d%d%d", &N, &M, &Q);
	for(int i=1; i<N; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	for(int i=1; i<=M; i++) scanf("%d", &A[i]);
	for(int i=1; i<=Q; i++) scanf("%d%d", &B[i].l, &B[i].r), B[i].k=i;

	sort(B+1, B+Q+1, [&](const Data &p, const Data &q)
	{
		if(p.l/SQ==q.l/SQ) return p.r<q.r;
		return p.l/SQ<q.l/SQ;
	});

	dfs(1, 1, 1);
	for(int j=1; j<=20; j++) for(int i=1; i<=N; i++) par[i][j]=par[par[i][j-1]][j-1];

	int l=1, r=1;
	S.insert({dfn[A[1]], A[1]});
	for(int i=1; i<=Q; i++)
	{
		while(r<B[i].r) push(A[++r]);
		while(B[i].l<l) push(A[--l]);
		while(r>B[i].r) pop(A[r--]);
		while(l<B[i].l) pop(A[l++]);
		ans1[B[i].k]=ans/2;
	}
	for(int i=1; i<=Q; i++) printf("%d\n", ans1[i]+1);	
}

Compilation message

tourism.cpp: In function 'int main()':
tourism.cpp:83:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |  scanf("%d%d%d", &N, &M, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:87:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |   scanf("%d%d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~
tourism.cpp:91:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |  for(int i=1; i<=M; i++) scanf("%d", &A[i]);
      |                          ~~~~~^~~~~~~~~~~~~
tourism.cpp:92:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |  for(int i=1; i<=Q; i++) scanf("%d%d", &B[i].l, &B[i].r), B[i].k=i;
      |                          ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 12 ms 2644 KB Output is correct
5 Correct 9 ms 2840 KB Output is correct
6 Correct 8 ms 2716 KB Output is correct
7 Correct 9 ms 2660 KB Output is correct
8 Correct 7 ms 2644 KB Output is correct
9 Correct 17 ms 2644 KB Output is correct
10 Correct 14 ms 2644 KB Output is correct
11 Correct 15 ms 2672 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 4 ms 2664 KB Output is correct
14 Correct 3 ms 2644 KB Output is correct
15 Correct 11 ms 2760 KB Output is correct
16 Correct 10 ms 2668 KB Output is correct
17 Correct 11 ms 2756 KB Output is correct
18 Correct 13 ms 2644 KB Output is correct
19 Correct 12 ms 2752 KB Output is correct
20 Correct 10 ms 2644 KB Output is correct
21 Correct 15 ms 2732 KB Output is correct
22 Correct 14 ms 2668 KB Output is correct
23 Correct 13 ms 2752 KB Output is correct
24 Correct 13 ms 2644 KB Output is correct
25 Correct 16 ms 2676 KB Output is correct
26 Correct 15 ms 2756 KB Output is correct
27 Correct 11 ms 2700 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 12 ms 2644 KB Output is correct
5 Correct 9 ms 2840 KB Output is correct
6 Correct 8 ms 2716 KB Output is correct
7 Correct 9 ms 2660 KB Output is correct
8 Correct 7 ms 2644 KB Output is correct
9 Correct 17 ms 2644 KB Output is correct
10 Correct 14 ms 2644 KB Output is correct
11 Correct 15 ms 2672 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 4 ms 2664 KB Output is correct
14 Correct 3 ms 2644 KB Output is correct
15 Correct 11 ms 2760 KB Output is correct
16 Correct 10 ms 2668 KB Output is correct
17 Correct 11 ms 2756 KB Output is correct
18 Correct 13 ms 2644 KB Output is correct
19 Correct 12 ms 2752 KB Output is correct
20 Correct 10 ms 2644 KB Output is correct
21 Correct 15 ms 2732 KB Output is correct
22 Correct 14 ms 2668 KB Output is correct
23 Correct 13 ms 2752 KB Output is correct
24 Correct 13 ms 2644 KB Output is correct
25 Correct 16 ms 2676 KB Output is correct
26 Correct 15 ms 2756 KB Output is correct
27 Correct 11 ms 2700 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2680 KB Output is correct
30 Correct 89 ms 3004 KB Output is correct
31 Correct 114 ms 3044 KB Output is correct
32 Correct 138 ms 3056 KB Output is correct
33 Correct 136 ms 3064 KB Output is correct
34 Correct 131 ms 3068 KB Output is correct
35 Correct 18 ms 3156 KB Output is correct
36 Correct 18 ms 3128 KB Output is correct
37 Correct 20 ms 3040 KB Output is correct
38 Correct 99 ms 3272 KB Output is correct
39 Correct 97 ms 3172 KB Output is correct
40 Correct 97 ms 3140 KB Output is correct
41 Correct 15 ms 3156 KB Output is correct
42 Correct 16 ms 3156 KB Output is correct
43 Correct 16 ms 3284 KB Output is correct
44 Correct 104 ms 3172 KB Output is correct
45 Correct 103 ms 3128 KB Output is correct
46 Correct 103 ms 3148 KB Output is correct
47 Correct 16 ms 3156 KB Output is correct
48 Correct 16 ms 3156 KB Output is correct
49 Correct 16 ms 3156 KB Output is correct
50 Correct 136 ms 3120 KB Output is correct
51 Correct 133 ms 3132 KB Output is correct
52 Correct 136 ms 3112 KB Output is correct
53 Correct 132 ms 3128 KB Output is correct
54 Correct 136 ms 3028 KB Output is correct
55 Correct 131 ms 3124 KB Output is correct
56 Correct 75 ms 2800 KB Output is correct
57 Correct 3 ms 3028 KB Output is correct
58 Correct 5 ms 3064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 10 ms 2664 KB Output is correct
3 Correct 78 ms 2888 KB Output is correct
4 Execution timed out 5046 ms 20848 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 149 ms 11808 KB Output is correct
3 Correct 190 ms 12908 KB Output is correct
4 Correct 165 ms 14544 KB Output is correct
5 Correct 203 ms 24244 KB Output is correct
6 Correct 285 ms 23024 KB Output is correct
7 Correct 337 ms 20384 KB Output is correct
8 Correct 305 ms 20400 KB Output is correct
9 Correct 303 ms 19916 KB Output is correct
10 Correct 296 ms 19668 KB Output is correct
11 Correct 266 ms 19376 KB Output is correct
12 Correct 289 ms 19788 KB Output is correct
13 Correct 238 ms 19328 KB Output is correct
14 Correct 252 ms 20412 KB Output is correct
15 Correct 217 ms 22132 KB Output is correct
16 Correct 266 ms 22280 KB Output is correct
17 Correct 278 ms 22340 KB Output is correct
18 Correct 288 ms 22348 KB Output is correct
19 Correct 204 ms 23980 KB Output is correct
20 Correct 314 ms 24940 KB Output is correct
21 Correct 276 ms 21904 KB Output is correct
22 Correct 312 ms 20632 KB Output is correct
23 Correct 296 ms 19892 KB Output is correct
24 Correct 304 ms 20300 KB Output is correct
25 Correct 323 ms 20200 KB Output is correct
26 Correct 348 ms 20160 KB Output is correct
27 Correct 366 ms 19508 KB Output is correct
28 Correct 376 ms 20300 KB Output is correct
29 Correct 378 ms 20380 KB Output is correct
30 Correct 353 ms 20356 KB Output is correct
31 Correct 329 ms 19904 KB Output is correct
32 Correct 308 ms 20496 KB Output is correct
33 Correct 301 ms 20876 KB Output is correct
34 Correct 243 ms 22116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2672 KB Output is correct
2 Correct 10 ms 2692 KB Output is correct
3 Correct 77 ms 2904 KB Output is correct
4 Execution timed out 5064 ms 19012 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 12 ms 2644 KB Output is correct
5 Correct 9 ms 2840 KB Output is correct
6 Correct 8 ms 2716 KB Output is correct
7 Correct 9 ms 2660 KB Output is correct
8 Correct 7 ms 2644 KB Output is correct
9 Correct 17 ms 2644 KB Output is correct
10 Correct 14 ms 2644 KB Output is correct
11 Correct 15 ms 2672 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 4 ms 2664 KB Output is correct
14 Correct 3 ms 2644 KB Output is correct
15 Correct 11 ms 2760 KB Output is correct
16 Correct 10 ms 2668 KB Output is correct
17 Correct 11 ms 2756 KB Output is correct
18 Correct 13 ms 2644 KB Output is correct
19 Correct 12 ms 2752 KB Output is correct
20 Correct 10 ms 2644 KB Output is correct
21 Correct 15 ms 2732 KB Output is correct
22 Correct 14 ms 2668 KB Output is correct
23 Correct 13 ms 2752 KB Output is correct
24 Correct 13 ms 2644 KB Output is correct
25 Correct 16 ms 2676 KB Output is correct
26 Correct 15 ms 2756 KB Output is correct
27 Correct 11 ms 2700 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2680 KB Output is correct
30 Correct 89 ms 3004 KB Output is correct
31 Correct 114 ms 3044 KB Output is correct
32 Correct 138 ms 3056 KB Output is correct
33 Correct 136 ms 3064 KB Output is correct
34 Correct 131 ms 3068 KB Output is correct
35 Correct 18 ms 3156 KB Output is correct
36 Correct 18 ms 3128 KB Output is correct
37 Correct 20 ms 3040 KB Output is correct
38 Correct 99 ms 3272 KB Output is correct
39 Correct 97 ms 3172 KB Output is correct
40 Correct 97 ms 3140 KB Output is correct
41 Correct 15 ms 3156 KB Output is correct
42 Correct 16 ms 3156 KB Output is correct
43 Correct 16 ms 3284 KB Output is correct
44 Correct 104 ms 3172 KB Output is correct
45 Correct 103 ms 3128 KB Output is correct
46 Correct 103 ms 3148 KB Output is correct
47 Correct 16 ms 3156 KB Output is correct
48 Correct 16 ms 3156 KB Output is correct
49 Correct 16 ms 3156 KB Output is correct
50 Correct 136 ms 3120 KB Output is correct
51 Correct 133 ms 3132 KB Output is correct
52 Correct 136 ms 3112 KB Output is correct
53 Correct 132 ms 3128 KB Output is correct
54 Correct 136 ms 3028 KB Output is correct
55 Correct 131 ms 3124 KB Output is correct
56 Correct 75 ms 2800 KB Output is correct
57 Correct 3 ms 3028 KB Output is correct
58 Correct 5 ms 3064 KB Output is correct
59 Correct 2 ms 2644 KB Output is correct
60 Correct 10 ms 2664 KB Output is correct
61 Correct 78 ms 2888 KB Output is correct
62 Execution timed out 5046 ms 20848 KB Time limit exceeded
63 Halted 0 ms 0 KB -