Submission #827379

# Submission time Handle Problem Language Result Execution time Memory
827379 2023-08-16T12:01:42 Z lovrot Tourism (JOI23_tourism) C++17
100 / 100
618 ms 27592 KB
#include <bits/stdc++.h>

#define X first
#define Y second
#define pb push_back
#define MP make_pair

using namespace std; 

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

int abs(int x) {
	if(x < 0) return -x;
	return x;
}

const int LOG = 17; 
const int OFF = 1 << LOG; 
const int INF = 1e9;

struct intv {
	int l, r, val;
	bool op;
	intv (int l, int r, int val) : l(l), r(r), val(val) { op = 0; }
	intv (int l, int r, int val, int op) : l(l), r(r), val(val), op(op) {}
	bool operator< (intv a) const { return l < a.l || (l == a.l && r < a.r); }
};

int CNT; 
int head[OFF], heav[OFF], pos[OFF], par[OFF], dep[OFF];

vector<int> g[OFF]; 
vector<intv> ups;

int dfs(int u) {
	int siz = 1, maxs = 0, ind = 0;
	for(int v : g[u]) {
		if(v == par[u]) continue; 
		par[v] = u, dep[v] = dep[u] + 1;
		int res = dfs(v); 
		if(res > maxs) maxs = res, ind = v;
		siz += res;
	}
	heav[u] = ind; 
	// printf("%d -> %d\n", u, ind);
	return siz;
} 

void decomp(int u, int h) {
	pos[u] = CNT++, head[u] = h;
	// printf("%d[%d, %d]\n", u, pos[u], head[u]);
	if(heav[u] && u != 1) decomp(heav[u], h); 
	for(int v : g[u]) {
		if(v == par[u] || v == heav[u] && u != 1) continue;
		decomp(v, v);
	}
}

int fen[OFF];
set<intv> s; 

void add(int x, int val) {
	for(x; x < OFF; x += x & -x) fen[x] += val;
}

int sum(int x) {
	int ret = 0; 
	for(x; x; x -= x & -x) ret += fen[x]; 
	return ret;
}

void change(intv i) {
	// if(i.op) printf("%d %d -%d\n", i.l, i.r, i.val);
	// else printf("%d %d %d\n", i.l, i.r, i.val);
	int val = i.r - i.l + 1;
	if(i.op) val = -val;
	add(i.val + 1, val); 
}

void debug() {
	for(auto it : s) {
		printf("[%d, %d (%d)] ", it.l, it.r, it.val);
	}
	printf("\n");
}

//shit
void interval(int l, int r, int val) {
	if(l > r) return;

	ups.clear(); 
	ups.pb(intv(l, r, val)); 
	auto it = prev(s.upper_bound(intv(l, INF, INF)));

	if(it->l < l) ups.pb(intv(it->l, l - 1, it->val)); 
	
	while(it != s.end() && it->l <= r) {
		if(it->r > r) ups.pb(intv(r + 1, it->r, it->val)); 
		auto nit = next(it);
		change(intv(it->l, it->r, it->val, 1));  
		s.erase(it); 
		it = nit;
	}
	for(intv p : ups) {
		s.insert(p);
		change(p);
	}
	//printf("add %d %d %d\n", l, r, val);
	// debug();
}

void update(int u, int v, int val) {
	for(; head[u] != head[v]; v = par[head[v]]) {
		if(dep[head[u]] > dep[head[v]]) swap(u, v); 
		interval(pos[head[v]], pos[v], val); 
	}
	if(dep[u] > dep[v]) swap(u, v);
	interval(pos[u] + 1, pos[v], val); 
}

int n, m, t, ans[OFF]; 
vector<int> c;
vector<pii> q[OFF]; 

int main() {
	scanf("%d%d%d", &n, &m, &t); 
	for(int i = 0; i < n - 1; i++) {
		int a, b;
		scanf("%d%d", &a, &b);
		g[a].pb(b); 
		g[b].pb(a); 
	}
	dfs(1); 
	decomp(1, 1); 

	change(intv(1, n - 1, 0));
	s.insert(intv(1, n - 1, 0)); 

	for(int i = 0; i < m; i++) {
		int x;
		scanf("%d", &x); 
		c.pb(x); 
	}

	for(int i = 0; i < t; i++) {
		int l, r;
		scanf("%d%d", &l, &r); 
		q[r].pb({l, i}); 
	}

	for(int i = 1; i <= m; i++) {
		if(i != 1) update(c[i - 2], c[i - 1], i - 1);
		for(pii p : q[i]) ans[p.Y] = sum(p.X);   
	}

	for(int i = 0; i < t; i++) printf("%d\n", n - ans[i]);
	return 0;	
}

Compilation message

tourism.cpp: In function 'void decomp(int, int)':
tourism.cpp:55:34: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   55 |   if(v == par[u] || v == heav[u] && u != 1) continue;
      |                     ~~~~~~~~~~~~~^~~~~~~~~
tourism.cpp: In function 'void add(int, int)':
tourism.cpp:64:6: warning: statement has no effect [-Wunused-value]
   64 |  for(x; x < OFF; x += x & -x) fen[x] += val;
      |      ^
tourism.cpp: In function 'int sum(int)':
tourism.cpp:69:6: warning: statement has no effect [-Wunused-value]
   69 |  for(x; x; x -= x & -x) ret += fen[x];
      |      ^
tourism.cpp: In function 'int main()':
tourism.cpp:127:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |  scanf("%d%d%d", &n, &m, &t);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:130:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |   scanf("%d%d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~
tourism.cpp:142:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  142 |   scanf("%d", &x);
      |   ~~~~~^~~~~~~~~~
tourism.cpp:148:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  148 |   scanf("%d%d", &l, &r);
      |   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6484 KB Output is correct
2 Correct 3 ms 6472 KB Output is correct
3 Correct 3 ms 6472 KB Output is correct
4 Correct 3 ms 6484 KB Output is correct
5 Correct 3 ms 6484 KB Output is correct
6 Correct 3 ms 6476 KB Output is correct
7 Correct 3 ms 6484 KB Output is correct
8 Correct 3 ms 6484 KB Output is correct
9 Correct 3 ms 6484 KB Output is correct
10 Correct 3 ms 6484 KB Output is correct
11 Correct 3 ms 6484 KB Output is correct
12 Correct 3 ms 6484 KB Output is correct
13 Correct 3 ms 6484 KB Output is correct
14 Correct 3 ms 6484 KB Output is correct
15 Correct 4 ms 6556 KB Output is correct
16 Correct 3 ms 6484 KB Output is correct
17 Correct 3 ms 6484 KB Output is correct
18 Correct 4 ms 6484 KB Output is correct
19 Correct 3 ms 6484 KB Output is correct
20 Correct 4 ms 6460 KB Output is correct
21 Correct 3 ms 6484 KB Output is correct
22 Correct 3 ms 6484 KB Output is correct
23 Correct 4 ms 6484 KB Output is correct
24 Correct 3 ms 6480 KB Output is correct
25 Correct 3 ms 6484 KB Output is correct
26 Correct 4 ms 6484 KB Output is correct
27 Correct 3 ms 6476 KB Output is correct
28 Correct 3 ms 6484 KB Output is correct
29 Correct 3 ms 6484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6484 KB Output is correct
2 Correct 3 ms 6472 KB Output is correct
3 Correct 3 ms 6472 KB Output is correct
4 Correct 3 ms 6484 KB Output is correct
5 Correct 3 ms 6484 KB Output is correct
6 Correct 3 ms 6476 KB Output is correct
7 Correct 3 ms 6484 KB Output is correct
8 Correct 3 ms 6484 KB Output is correct
9 Correct 3 ms 6484 KB Output is correct
10 Correct 3 ms 6484 KB Output is correct
11 Correct 3 ms 6484 KB Output is correct
12 Correct 3 ms 6484 KB Output is correct
13 Correct 3 ms 6484 KB Output is correct
14 Correct 3 ms 6484 KB Output is correct
15 Correct 4 ms 6556 KB Output is correct
16 Correct 3 ms 6484 KB Output is correct
17 Correct 3 ms 6484 KB Output is correct
18 Correct 4 ms 6484 KB Output is correct
19 Correct 3 ms 6484 KB Output is correct
20 Correct 4 ms 6460 KB Output is correct
21 Correct 3 ms 6484 KB Output is correct
22 Correct 3 ms 6484 KB Output is correct
23 Correct 4 ms 6484 KB Output is correct
24 Correct 3 ms 6480 KB Output is correct
25 Correct 3 ms 6484 KB Output is correct
26 Correct 4 ms 6484 KB Output is correct
27 Correct 3 ms 6476 KB Output is correct
28 Correct 3 ms 6484 KB Output is correct
29 Correct 3 ms 6484 KB Output is correct
30 Correct 6 ms 6704 KB Output is correct
31 Correct 7 ms 6740 KB Output is correct
32 Correct 7 ms 6740 KB Output is correct
33 Correct 7 ms 6792 KB Output is correct
34 Correct 7 ms 6748 KB Output is correct
35 Correct 7 ms 6740 KB Output is correct
36 Correct 7 ms 6720 KB Output is correct
37 Correct 7 ms 6740 KB Output is correct
38 Correct 6 ms 6748 KB Output is correct
39 Correct 5 ms 6868 KB Output is correct
40 Correct 5 ms 6820 KB Output is correct
41 Correct 5 ms 6872 KB Output is correct
42 Correct 5 ms 6868 KB Output is correct
43 Correct 5 ms 6740 KB Output is correct
44 Correct 5 ms 6740 KB Output is correct
45 Correct 6 ms 6740 KB Output is correct
46 Correct 5 ms 6740 KB Output is correct
47 Correct 5 ms 6740 KB Output is correct
48 Correct 6 ms 6748 KB Output is correct
49 Correct 7 ms 6740 KB Output is correct
50 Correct 6 ms 6744 KB Output is correct
51 Correct 6 ms 6740 KB Output is correct
52 Correct 7 ms 6740 KB Output is correct
53 Correct 6 ms 6740 KB Output is correct
54 Correct 5 ms 6748 KB Output is correct
55 Correct 6 ms 6740 KB Output is correct
56 Correct 4 ms 6484 KB Output is correct
57 Correct 4 ms 6616 KB Output is correct
58 Correct 7 ms 6744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6476 KB Output is correct
2 Correct 3 ms 6472 KB Output is correct
3 Correct 3 ms 6480 KB Output is correct
4 Correct 76 ms 19428 KB Output is correct
5 Correct 74 ms 22456 KB Output is correct
6 Correct 78 ms 24300 KB Output is correct
7 Correct 104 ms 27592 KB Output is correct
8 Correct 101 ms 27540 KB Output is correct
9 Correct 133 ms 27544 KB Output is correct
10 Correct 101 ms 27548 KB Output is correct
11 Correct 101 ms 27508 KB Output is correct
12 Correct 94 ms 27064 KB Output is correct
13 Correct 94 ms 27084 KB Output is correct
14 Correct 96 ms 27012 KB Output is correct
15 Correct 52 ms 24012 KB Output is correct
16 Correct 110 ms 27340 KB Output is correct
17 Correct 45 ms 10824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6480 KB Output is correct
2 Correct 177 ms 13044 KB Output is correct
3 Correct 279 ms 14164 KB Output is correct
4 Correct 216 ms 14508 KB Output is correct
5 Correct 354 ms 18316 KB Output is correct
6 Correct 352 ms 18396 KB Output is correct
7 Correct 354 ms 18380 KB Output is correct
8 Correct 353 ms 18404 KB Output is correct
9 Correct 357 ms 18312 KB Output is correct
10 Correct 357 ms 18512 KB Output is correct
11 Correct 357 ms 18384 KB Output is correct
12 Correct 370 ms 18520 KB Output is correct
13 Correct 361 ms 18900 KB Output is correct
14 Correct 398 ms 19892 KB Output is correct
15 Correct 409 ms 23292 KB Output is correct
16 Correct 355 ms 19008 KB Output is correct
17 Correct 353 ms 18852 KB Output is correct
18 Correct 378 ms 18864 KB Output is correct
19 Correct 203 ms 14480 KB Output is correct
20 Correct 207 ms 14448 KB Output is correct
21 Correct 214 ms 14516 KB Output is correct
22 Correct 207 ms 14540 KB Output is correct
23 Correct 202 ms 14524 KB Output is correct
24 Correct 214 ms 14436 KB Output is correct
25 Correct 218 ms 14476 KB Output is correct
26 Correct 211 ms 14544 KB Output is correct
27 Correct 213 ms 14476 KB Output is correct
28 Correct 206 ms 14540 KB Output is correct
29 Correct 210 ms 14568 KB Output is correct
30 Correct 224 ms 14772 KB Output is correct
31 Correct 208 ms 15028 KB Output is correct
32 Correct 217 ms 15484 KB Output is correct
33 Correct 234 ms 17104 KB Output is correct
34 Correct 234 ms 19508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6484 KB Output is correct
2 Correct 3 ms 6512 KB Output is correct
3 Correct 4 ms 6484 KB Output is correct
4 Correct 491 ms 17784 KB Output is correct
5 Correct 468 ms 17904 KB Output is correct
6 Correct 566 ms 21188 KB Output is correct
7 Correct 590 ms 22912 KB Output is correct
8 Correct 597 ms 23032 KB Output is correct
9 Correct 586 ms 22856 KB Output is correct
10 Correct 586 ms 22932 KB Output is correct
11 Correct 605 ms 22888 KB Output is correct
12 Correct 618 ms 22888 KB Output is correct
13 Correct 592 ms 22868 KB Output is correct
14 Correct 42 ms 10720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6484 KB Output is correct
2 Correct 3 ms 6472 KB Output is correct
3 Correct 3 ms 6472 KB Output is correct
4 Correct 3 ms 6484 KB Output is correct
5 Correct 3 ms 6484 KB Output is correct
6 Correct 3 ms 6476 KB Output is correct
7 Correct 3 ms 6484 KB Output is correct
8 Correct 3 ms 6484 KB Output is correct
9 Correct 3 ms 6484 KB Output is correct
10 Correct 3 ms 6484 KB Output is correct
11 Correct 3 ms 6484 KB Output is correct
12 Correct 3 ms 6484 KB Output is correct
13 Correct 3 ms 6484 KB Output is correct
14 Correct 3 ms 6484 KB Output is correct
15 Correct 4 ms 6556 KB Output is correct
16 Correct 3 ms 6484 KB Output is correct
17 Correct 3 ms 6484 KB Output is correct
18 Correct 4 ms 6484 KB Output is correct
19 Correct 3 ms 6484 KB Output is correct
20 Correct 4 ms 6460 KB Output is correct
21 Correct 3 ms 6484 KB Output is correct
22 Correct 3 ms 6484 KB Output is correct
23 Correct 4 ms 6484 KB Output is correct
24 Correct 3 ms 6480 KB Output is correct
25 Correct 3 ms 6484 KB Output is correct
26 Correct 4 ms 6484 KB Output is correct
27 Correct 3 ms 6476 KB Output is correct
28 Correct 3 ms 6484 KB Output is correct
29 Correct 3 ms 6484 KB Output is correct
30 Correct 6 ms 6704 KB Output is correct
31 Correct 7 ms 6740 KB Output is correct
32 Correct 7 ms 6740 KB Output is correct
33 Correct 7 ms 6792 KB Output is correct
34 Correct 7 ms 6748 KB Output is correct
35 Correct 7 ms 6740 KB Output is correct
36 Correct 7 ms 6720 KB Output is correct
37 Correct 7 ms 6740 KB Output is correct
38 Correct 6 ms 6748 KB Output is correct
39 Correct 5 ms 6868 KB Output is correct
40 Correct 5 ms 6820 KB Output is correct
41 Correct 5 ms 6872 KB Output is correct
42 Correct 5 ms 6868 KB Output is correct
43 Correct 5 ms 6740 KB Output is correct
44 Correct 5 ms 6740 KB Output is correct
45 Correct 6 ms 6740 KB Output is correct
46 Correct 5 ms 6740 KB Output is correct
47 Correct 5 ms 6740 KB Output is correct
48 Correct 6 ms 6748 KB Output is correct
49 Correct 7 ms 6740 KB Output is correct
50 Correct 6 ms 6744 KB Output is correct
51 Correct 6 ms 6740 KB Output is correct
52 Correct 7 ms 6740 KB Output is correct
53 Correct 6 ms 6740 KB Output is correct
54 Correct 5 ms 6748 KB Output is correct
55 Correct 6 ms 6740 KB Output is correct
56 Correct 4 ms 6484 KB Output is correct
57 Correct 4 ms 6616 KB Output is correct
58 Correct 7 ms 6744 KB Output is correct
59 Correct 3 ms 6476 KB Output is correct
60 Correct 3 ms 6472 KB Output is correct
61 Correct 3 ms 6480 KB Output is correct
62 Correct 76 ms 19428 KB Output is correct
63 Correct 74 ms 22456 KB Output is correct
64 Correct 78 ms 24300 KB Output is correct
65 Correct 104 ms 27592 KB Output is correct
66 Correct 101 ms 27540 KB Output is correct
67 Correct 133 ms 27544 KB Output is correct
68 Correct 101 ms 27548 KB Output is correct
69 Correct 101 ms 27508 KB Output is correct
70 Correct 94 ms 27064 KB Output is correct
71 Correct 94 ms 27084 KB Output is correct
72 Correct 96 ms 27012 KB Output is correct
73 Correct 52 ms 24012 KB Output is correct
74 Correct 110 ms 27340 KB Output is correct
75 Correct 45 ms 10824 KB Output is correct
76 Correct 3 ms 6480 KB Output is correct
77 Correct 177 ms 13044 KB Output is correct
78 Correct 279 ms 14164 KB Output is correct
79 Correct 216 ms 14508 KB Output is correct
80 Correct 354 ms 18316 KB Output is correct
81 Correct 352 ms 18396 KB Output is correct
82 Correct 354 ms 18380 KB Output is correct
83 Correct 353 ms 18404 KB Output is correct
84 Correct 357 ms 18312 KB Output is correct
85 Correct 357 ms 18512 KB Output is correct
86 Correct 357 ms 18384 KB Output is correct
87 Correct 370 ms 18520 KB Output is correct
88 Correct 361 ms 18900 KB Output is correct
89 Correct 398 ms 19892 KB Output is correct
90 Correct 409 ms 23292 KB Output is correct
91 Correct 355 ms 19008 KB Output is correct
92 Correct 353 ms 18852 KB Output is correct
93 Correct 378 ms 18864 KB Output is correct
94 Correct 203 ms 14480 KB Output is correct
95 Correct 207 ms 14448 KB Output is correct
96 Correct 214 ms 14516 KB Output is correct
97 Correct 207 ms 14540 KB Output is correct
98 Correct 202 ms 14524 KB Output is correct
99 Correct 214 ms 14436 KB Output is correct
100 Correct 218 ms 14476 KB Output is correct
101 Correct 211 ms 14544 KB Output is correct
102 Correct 213 ms 14476 KB Output is correct
103 Correct 206 ms 14540 KB Output is correct
104 Correct 210 ms 14568 KB Output is correct
105 Correct 224 ms 14772 KB Output is correct
106 Correct 208 ms 15028 KB Output is correct
107 Correct 217 ms 15484 KB Output is correct
108 Correct 234 ms 17104 KB Output is correct
109 Correct 234 ms 19508 KB Output is correct
110 Correct 3 ms 6484 KB Output is correct
111 Correct 3 ms 6512 KB Output is correct
112 Correct 4 ms 6484 KB Output is correct
113 Correct 491 ms 17784 KB Output is correct
114 Correct 468 ms 17904 KB Output is correct
115 Correct 566 ms 21188 KB Output is correct
116 Correct 590 ms 22912 KB Output is correct
117 Correct 597 ms 23032 KB Output is correct
118 Correct 586 ms 22856 KB Output is correct
119 Correct 586 ms 22932 KB Output is correct
120 Correct 605 ms 22888 KB Output is correct
121 Correct 618 ms 22888 KB Output is correct
122 Correct 592 ms 22868 KB Output is correct
123 Correct 42 ms 10720 KB Output is correct
124 Correct 336 ms 21928 KB Output is correct
125 Correct 263 ms 19572 KB Output is correct
126 Correct 379 ms 22548 KB Output is correct
127 Correct 395 ms 22484 KB Output is correct
128 Correct 411 ms 22504 KB Output is correct
129 Correct 396 ms 22532 KB Output is correct
130 Correct 386 ms 22524 KB Output is correct
131 Correct 119 ms 26148 KB Output is correct
132 Correct 114 ms 27592 KB Output is correct
133 Correct 111 ms 22824 KB Output is correct
134 Correct 250 ms 18612 KB Output is correct
135 Correct 256 ms 18604 KB Output is correct
136 Correct 243 ms 18648 KB Output is correct
137 Correct 177 ms 23984 KB Output is correct
138 Correct 170 ms 24024 KB Output is correct
139 Correct 178 ms 23912 KB Output is correct
140 Correct 159 ms 23976 KB Output is correct
141 Correct 164 ms 23964 KB Output is correct
142 Correct 163 ms 23980 KB Output is correct
143 Correct 68 ms 14716 KB Output is correct
144 Correct 399 ms 22204 KB Output is correct