Submission #737815

# Submission time Handle Problem Language Result Execution time Memory
737815 2023-05-07T18:43:08 Z arnold518 Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 23472 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 = 500;

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 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 10 ms 2724 KB Output is correct
5 Correct 7 ms 2644 KB Output is correct
6 Correct 7 ms 2644 KB Output is correct
7 Correct 8 ms 2644 KB Output is correct
8 Correct 6 ms 2644 KB Output is correct
9 Correct 15 ms 2644 KB Output is correct
10 Correct 14 ms 2644 KB Output is correct
11 Correct 13 ms 2732 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 3 ms 2644 KB Output is correct
14 Correct 3 ms 2656 KB Output is correct
15 Correct 11 ms 2744 KB Output is correct
16 Correct 10 ms 2752 KB Output is correct
17 Correct 11 ms 2752 KB Output is correct
18 Correct 12 ms 2740 KB Output is correct
19 Correct 11 ms 2744 KB Output is correct
20 Correct 10 ms 2740 KB Output is correct
21 Correct 15 ms 2748 KB Output is correct
22 Correct 15 ms 2744 KB Output is correct
23 Correct 13 ms 2732 KB Output is correct
24 Correct 14 ms 2644 KB Output is correct
25 Correct 15 ms 2740 KB Output is correct
26 Correct 15 ms 2644 KB Output is correct
27 Correct 10 ms 2644 KB Output is correct
28 Correct 2 ms 2620 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 10 ms 2724 KB Output is correct
5 Correct 7 ms 2644 KB Output is correct
6 Correct 7 ms 2644 KB Output is correct
7 Correct 8 ms 2644 KB Output is correct
8 Correct 6 ms 2644 KB Output is correct
9 Correct 15 ms 2644 KB Output is correct
10 Correct 14 ms 2644 KB Output is correct
11 Correct 13 ms 2732 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 3 ms 2644 KB Output is correct
14 Correct 3 ms 2656 KB Output is correct
15 Correct 11 ms 2744 KB Output is correct
16 Correct 10 ms 2752 KB Output is correct
17 Correct 11 ms 2752 KB Output is correct
18 Correct 12 ms 2740 KB Output is correct
19 Correct 11 ms 2744 KB Output is correct
20 Correct 10 ms 2740 KB Output is correct
21 Correct 15 ms 2748 KB Output is correct
22 Correct 15 ms 2744 KB Output is correct
23 Correct 13 ms 2732 KB Output is correct
24 Correct 14 ms 2644 KB Output is correct
25 Correct 15 ms 2740 KB Output is correct
26 Correct 15 ms 2644 KB Output is correct
27 Correct 10 ms 2644 KB Output is correct
28 Correct 2 ms 2620 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 139 ms 2960 KB Output is correct
31 Correct 185 ms 3084 KB Output is correct
32 Correct 206 ms 3044 KB Output is correct
33 Correct 224 ms 3028 KB Output is correct
34 Correct 216 ms 3172 KB Output is correct
35 Correct 18 ms 3028 KB Output is correct
36 Correct 19 ms 3028 KB Output is correct
37 Correct 19 ms 3124 KB Output is correct
38 Correct 153 ms 3112 KB Output is correct
39 Correct 146 ms 3148 KB Output is correct
40 Correct 158 ms 3148 KB Output is correct
41 Correct 17 ms 3156 KB Output is correct
42 Correct 16 ms 3156 KB Output is correct
43 Correct 15 ms 3180 KB Output is correct
44 Correct 152 ms 3116 KB Output is correct
45 Correct 156 ms 3124 KB Output is correct
46 Correct 159 ms 3172 KB Output is correct
47 Correct 15 ms 3152 KB Output is correct
48 Correct 17 ms 3028 KB Output is correct
49 Correct 18 ms 3148 KB Output is correct
50 Correct 213 ms 3068 KB Output is correct
51 Correct 211 ms 3048 KB Output is correct
52 Correct 198 ms 3028 KB Output is correct
53 Correct 203 ms 3112 KB Output is correct
54 Correct 201 ms 3076 KB Output is correct
55 Correct 209 ms 3084 KB Output is correct
56 Correct 117 ms 2776 KB Output is correct
57 Correct 3 ms 3028 KB Output is correct
58 Correct 5 ms 3032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 10 ms 2696 KB Output is correct
3 Correct 123 ms 2792 KB Output is correct
4 Execution timed out 5076 ms 19576 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 126 ms 11604 KB Output is correct
3 Correct 193 ms 12328 KB Output is correct
4 Correct 168 ms 13516 KB Output is correct
5 Correct 199 ms 23464 KB Output is correct
6 Correct 298 ms 21868 KB Output is correct
7 Correct 319 ms 19792 KB Output is correct
8 Correct 337 ms 19032 KB Output is correct
9 Correct 337 ms 18936 KB Output is correct
10 Correct 306 ms 18900 KB Output is correct
11 Correct 283 ms 18808 KB Output is correct
12 Correct 262 ms 18844 KB Output is correct
13 Correct 246 ms 18884 KB Output is correct
14 Correct 249 ms 19380 KB Output is correct
15 Correct 206 ms 20556 KB Output is correct
16 Correct 265 ms 21084 KB Output is correct
17 Correct 276 ms 21052 KB Output is correct
18 Correct 277 ms 20984 KB Output is correct
19 Correct 195 ms 23376 KB Output is correct
20 Correct 260 ms 23472 KB Output is correct
21 Correct 275 ms 20548 KB Output is correct
22 Correct 282 ms 19660 KB Output is correct
23 Correct 300 ms 19244 KB Output is correct
24 Correct 318 ms 18956 KB Output is correct
25 Correct 376 ms 18860 KB Output is correct
26 Correct 372 ms 18836 KB Output is correct
27 Correct 377 ms 18928 KB Output is correct
28 Correct 369 ms 18756 KB Output is correct
29 Correct 361 ms 18896 KB Output is correct
30 Correct 355 ms 18856 KB Output is correct
31 Correct 351 ms 18940 KB Output is correct
32 Correct 333 ms 19184 KB Output is correct
33 Correct 347 ms 19588 KB Output is correct
34 Correct 261 ms 20500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 10 ms 2688 KB Output is correct
3 Correct 132 ms 2780 KB Output is correct
4 Execution timed out 5019 ms 17356 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 10 ms 2724 KB Output is correct
5 Correct 7 ms 2644 KB Output is correct
6 Correct 7 ms 2644 KB Output is correct
7 Correct 8 ms 2644 KB Output is correct
8 Correct 6 ms 2644 KB Output is correct
9 Correct 15 ms 2644 KB Output is correct
10 Correct 14 ms 2644 KB Output is correct
11 Correct 13 ms 2732 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 3 ms 2644 KB Output is correct
14 Correct 3 ms 2656 KB Output is correct
15 Correct 11 ms 2744 KB Output is correct
16 Correct 10 ms 2752 KB Output is correct
17 Correct 11 ms 2752 KB Output is correct
18 Correct 12 ms 2740 KB Output is correct
19 Correct 11 ms 2744 KB Output is correct
20 Correct 10 ms 2740 KB Output is correct
21 Correct 15 ms 2748 KB Output is correct
22 Correct 15 ms 2744 KB Output is correct
23 Correct 13 ms 2732 KB Output is correct
24 Correct 14 ms 2644 KB Output is correct
25 Correct 15 ms 2740 KB Output is correct
26 Correct 15 ms 2644 KB Output is correct
27 Correct 10 ms 2644 KB Output is correct
28 Correct 2 ms 2620 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 139 ms 2960 KB Output is correct
31 Correct 185 ms 3084 KB Output is correct
32 Correct 206 ms 3044 KB Output is correct
33 Correct 224 ms 3028 KB Output is correct
34 Correct 216 ms 3172 KB Output is correct
35 Correct 18 ms 3028 KB Output is correct
36 Correct 19 ms 3028 KB Output is correct
37 Correct 19 ms 3124 KB Output is correct
38 Correct 153 ms 3112 KB Output is correct
39 Correct 146 ms 3148 KB Output is correct
40 Correct 158 ms 3148 KB Output is correct
41 Correct 17 ms 3156 KB Output is correct
42 Correct 16 ms 3156 KB Output is correct
43 Correct 15 ms 3180 KB Output is correct
44 Correct 152 ms 3116 KB Output is correct
45 Correct 156 ms 3124 KB Output is correct
46 Correct 159 ms 3172 KB Output is correct
47 Correct 15 ms 3152 KB Output is correct
48 Correct 17 ms 3028 KB Output is correct
49 Correct 18 ms 3148 KB Output is correct
50 Correct 213 ms 3068 KB Output is correct
51 Correct 211 ms 3048 KB Output is correct
52 Correct 198 ms 3028 KB Output is correct
53 Correct 203 ms 3112 KB Output is correct
54 Correct 201 ms 3076 KB Output is correct
55 Correct 209 ms 3084 KB Output is correct
56 Correct 117 ms 2776 KB Output is correct
57 Correct 3 ms 3028 KB Output is correct
58 Correct 5 ms 3032 KB Output is correct
59 Correct 2 ms 2644 KB Output is correct
60 Correct 10 ms 2696 KB Output is correct
61 Correct 123 ms 2792 KB Output is correct
62 Execution timed out 5076 ms 19576 KB Time limit exceeded
63 Halted 0 ms 0 KB -