Submission #735215

# Submission time Handle Problem Language Result Execution time Memory
735215 2023-05-03T17:39:15 Z arnold518 Two Currencies (JOI23_currencies) C++17
100 / 100
1620 ms 41416 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;

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

struct SEG
{
	ll tree[MAXN*4+10];
	void init()
	{
		fill(tree, tree+MAXN*4+10, 0);
	}
	void update(int node, int tl, int tr, int l, int r, ll k)
	{
		if(l<=tl && tr<=r)
		{
			tree[node]+=k;
			return;
		}
		if(r<tl || tr<l) return;
		int mid=tl+tr>>1;
		update(node*2, tl, mid, l, r, k);
		update(node*2+1, mid+1, tr, l, r, k);
	}
	ll query(int node, int tl, int tr, int k)
	{
		if(tl==tr) return tree[node];
		int mid=tl+tr>>1;
		if(k<=mid) return tree[node]+query(node*2, tl, mid ,k);
		else return tree[node]+query(node*2+1, mid+1, tr, k);
	}
}seg;

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

int lca(int u, int v)
{
	if(dep[u]>dep[v]) swap(u, v);
	for(int i=20; i>=0; i--) if(dep[par[v][i]]>=dep[u]) 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 C[MAXN+10];
pii B[MAXN+10];
pll D[MAXN+10];
int lo[MAXN+10], hi[MAXN+10];
int E[MAXN+10];

ll f(int p)
{
	int u=B[p].first, v=B[p].second, w=C[p];
	return seg.query(1, 1, N, L[u])+seg.query(1, 1, N, L[v])-2*seg.query(1, 1, N, L[w]);
}

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, i});
		adj[v].push_back({u, i});
	}
	for(int i=1; i<=M; i++) scanf("%d%d", &A[i].second, &A[i].first);

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

	for(int i=1; i<=Q; i++) scanf("%d%d%lld%lld", &B[i].first, &B[i].second, &D[i].first, &D[i].second), C[i]=lca(B[i].first, B[i].second);

	sort(A+1, A+M+1);

	for(int i=1; i<=Q; i++) lo[i]=0, hi[i]=M+1;
	while(1)
	{
		vector<pii> V;
		for(int i=1; i<=Q; i++)
		{
			if(lo[i]+1<hi[i])
			{
				V.push_back({lo[i]+hi[i]>>1, i});
			}
		}
		if(V.empty()) break;
		sort(V.begin(), V.end());

		seg.init();
		int pt=1;
		for(auto it : V)
		{
			for(; pt<=M && pt<=it.first; pt++)
			{
				seg.update(1, 1, N, L[P[A[pt].second]], R[P[A[pt].second]], A[pt].first);
			}
			//printf("??%d %d %lld %d\n", it.first, it.second, f(it.second), D[it.second].second);
			if(f(it.second)>D[it.second].second) hi[it.second]=it.first;
			else lo[it.second]=it.first; 
		}
	}

	seg.init();
	int pt=1;
	vector<pii> V;
	for(int i=1; i<=Q; i++) V.push_back({lo[i], i});
	sort(V.begin(), V.end());
	for(auto it : V)
	{
		for(; pt<=M && pt<=it.first; pt++)
		{
			seg.update(1, 1, N, L[P[A[pt].second]], R[P[A[pt].second]], 1);
		}
		E[it.second]=f(it.second);
	}
	for(; pt<=M; pt++) seg.update(1, 1, N, L[P[A[pt].second]], R[P[A[pt].second]], 1);

	for(int i=1; i<=Q; i++)
	{
		//printf("%d %d %d\n", lo[i], f(i), E[i]);
		printf("%lld\n", max(-1ll, D[i].first-f(i)+E[i]));
	}
}

Compilation message

currencies.cpp: In member function 'void SEG::update(int, int, int, int, int, ll)':
currencies.cpp:29:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   29 |   int mid=tl+tr>>1;
      |           ~~^~~
currencies.cpp: In member function 'll SEG::query(int, int, int, int)':
currencies.cpp:36:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   36 |   int mid=tl+tr>>1;
      |           ~~^~~
currencies.cpp: In function 'int main()':
currencies.cpp:105:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  105 |     V.push_back({lo[i]+hi[i]>>1, i});
      |                  ~~~~~^~~~~~
currencies.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |  scanf("%d%d%d", &N, &M, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
currencies.cpp:84:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |   scanf("%d%d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~
currencies.cpp:88:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |  for(int i=1; i<=M; i++) scanf("%d%d", &A[i].second, &A[i].first);
      |                          ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
currencies.cpp:93:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |  for(int i=1; i<=Q; i++) scanf("%d%d%lld%lld", &B[i].first, &B[i].second, &D[i].first, &D[i].second), C[i]=lca(B[i].first, B[i].second);
      |                          ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5844 KB Output is correct
2 Correct 4 ms 5844 KB Output is correct
3 Correct 3 ms 5844 KB Output is correct
4 Correct 3 ms 5844 KB Output is correct
5 Correct 13 ms 6232 KB Output is correct
6 Correct 15 ms 6292 KB Output is correct
7 Correct 12 ms 6188 KB Output is correct
8 Correct 16 ms 6484 KB Output is correct
9 Correct 15 ms 6384 KB Output is correct
10 Correct 15 ms 6356 KB Output is correct
11 Correct 15 ms 6372 KB Output is correct
12 Correct 16 ms 6368 KB Output is correct
13 Correct 17 ms 6416 KB Output is correct
14 Correct 16 ms 6440 KB Output is correct
15 Correct 17 ms 6412 KB Output is correct
16 Correct 17 ms 6396 KB Output is correct
17 Correct 17 ms 6356 KB Output is correct
18 Correct 19 ms 6292 KB Output is correct
19 Correct 14 ms 6288 KB Output is correct
20 Correct 15 ms 6372 KB Output is correct
21 Correct 17 ms 6376 KB Output is correct
22 Correct 14 ms 6364 KB Output is correct
23 Correct 13 ms 6380 KB Output is correct
24 Correct 13 ms 6380 KB Output is correct
25 Correct 13 ms 6356 KB Output is correct
26 Correct 12 ms 6356 KB Output is correct
27 Correct 13 ms 6356 KB Output is correct
28 Correct 13 ms 6356 KB Output is correct
29 Correct 13 ms 6356 KB Output is correct
30 Correct 16 ms 6292 KB Output is correct
31 Correct 16 ms 6288 KB Output is correct
32 Correct 16 ms 6280 KB Output is correct
33 Correct 21 ms 6484 KB Output is correct
34 Correct 17 ms 6484 KB Output is correct
35 Correct 16 ms 6452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 15 ms 6300 KB Output is correct
3 Correct 15 ms 6356 KB Output is correct
4 Correct 15 ms 6368 KB Output is correct
5 Correct 844 ms 31684 KB Output is correct
6 Correct 1028 ms 28848 KB Output is correct
7 Correct 1004 ms 30732 KB Output is correct
8 Correct 758 ms 29376 KB Output is correct
9 Correct 738 ms 28532 KB Output is correct
10 Correct 1198 ms 34720 KB Output is correct
11 Correct 1204 ms 34760 KB Output is correct
12 Correct 1182 ms 34756 KB Output is correct
13 Correct 1157 ms 34888 KB Output is correct
14 Correct 1174 ms 34812 KB Output is correct
15 Correct 1501 ms 40640 KB Output is correct
16 Correct 1449 ms 41084 KB Output is correct
17 Correct 1403 ms 40428 KB Output is correct
18 Correct 1394 ms 34764 KB Output is correct
19 Correct 1367 ms 34844 KB Output is correct
20 Correct 1352 ms 34964 KB Output is correct
21 Correct 986 ms 34300 KB Output is correct
22 Correct 941 ms 34520 KB Output is correct
23 Correct 924 ms 34392 KB Output is correct
24 Correct 1002 ms 34548 KB Output is correct
25 Correct 895 ms 35232 KB Output is correct
26 Correct 918 ms 35312 KB Output is correct
27 Correct 887 ms 35108 KB Output is correct
28 Correct 746 ms 34672 KB Output is correct
29 Correct 722 ms 34700 KB Output is correct
30 Correct 764 ms 34844 KB Output is correct
31 Correct 789 ms 34836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 16 ms 6408 KB Output is correct
3 Correct 15 ms 6484 KB Output is correct
4 Correct 15 ms 6484 KB Output is correct
5 Correct 819 ms 36332 KB Output is correct
6 Correct 824 ms 36072 KB Output is correct
7 Correct 1064 ms 29496 KB Output is correct
8 Correct 1308 ms 41280 KB Output is correct
9 Correct 1313 ms 41328 KB Output is correct
10 Correct 1354 ms 41416 KB Output is correct
11 Correct 1069 ms 41292 KB Output is correct
12 Correct 1113 ms 41304 KB Output is correct
13 Correct 1025 ms 41392 KB Output is correct
14 Correct 873 ms 41032 KB Output is correct
15 Correct 793 ms 40988 KB Output is correct
16 Correct 873 ms 41288 KB Output is correct
17 Correct 936 ms 41200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5844 KB Output is correct
2 Correct 4 ms 5844 KB Output is correct
3 Correct 3 ms 5844 KB Output is correct
4 Correct 3 ms 5844 KB Output is correct
5 Correct 13 ms 6232 KB Output is correct
6 Correct 15 ms 6292 KB Output is correct
7 Correct 12 ms 6188 KB Output is correct
8 Correct 16 ms 6484 KB Output is correct
9 Correct 15 ms 6384 KB Output is correct
10 Correct 15 ms 6356 KB Output is correct
11 Correct 15 ms 6372 KB Output is correct
12 Correct 16 ms 6368 KB Output is correct
13 Correct 17 ms 6416 KB Output is correct
14 Correct 16 ms 6440 KB Output is correct
15 Correct 17 ms 6412 KB Output is correct
16 Correct 17 ms 6396 KB Output is correct
17 Correct 17 ms 6356 KB Output is correct
18 Correct 19 ms 6292 KB Output is correct
19 Correct 14 ms 6288 KB Output is correct
20 Correct 15 ms 6372 KB Output is correct
21 Correct 17 ms 6376 KB Output is correct
22 Correct 14 ms 6364 KB Output is correct
23 Correct 13 ms 6380 KB Output is correct
24 Correct 13 ms 6380 KB Output is correct
25 Correct 13 ms 6356 KB Output is correct
26 Correct 12 ms 6356 KB Output is correct
27 Correct 13 ms 6356 KB Output is correct
28 Correct 13 ms 6356 KB Output is correct
29 Correct 13 ms 6356 KB Output is correct
30 Correct 16 ms 6292 KB Output is correct
31 Correct 16 ms 6288 KB Output is correct
32 Correct 16 ms 6280 KB Output is correct
33 Correct 21 ms 6484 KB Output is correct
34 Correct 17 ms 6484 KB Output is correct
35 Correct 16 ms 6452 KB Output is correct
36 Correct 3 ms 5844 KB Output is correct
37 Correct 15 ms 6300 KB Output is correct
38 Correct 15 ms 6356 KB Output is correct
39 Correct 15 ms 6368 KB Output is correct
40 Correct 844 ms 31684 KB Output is correct
41 Correct 1028 ms 28848 KB Output is correct
42 Correct 1004 ms 30732 KB Output is correct
43 Correct 758 ms 29376 KB Output is correct
44 Correct 738 ms 28532 KB Output is correct
45 Correct 1198 ms 34720 KB Output is correct
46 Correct 1204 ms 34760 KB Output is correct
47 Correct 1182 ms 34756 KB Output is correct
48 Correct 1157 ms 34888 KB Output is correct
49 Correct 1174 ms 34812 KB Output is correct
50 Correct 1501 ms 40640 KB Output is correct
51 Correct 1449 ms 41084 KB Output is correct
52 Correct 1403 ms 40428 KB Output is correct
53 Correct 1394 ms 34764 KB Output is correct
54 Correct 1367 ms 34844 KB Output is correct
55 Correct 1352 ms 34964 KB Output is correct
56 Correct 986 ms 34300 KB Output is correct
57 Correct 941 ms 34520 KB Output is correct
58 Correct 924 ms 34392 KB Output is correct
59 Correct 1002 ms 34548 KB Output is correct
60 Correct 895 ms 35232 KB Output is correct
61 Correct 918 ms 35312 KB Output is correct
62 Correct 887 ms 35108 KB Output is correct
63 Correct 746 ms 34672 KB Output is correct
64 Correct 722 ms 34700 KB Output is correct
65 Correct 764 ms 34844 KB Output is correct
66 Correct 789 ms 34836 KB Output is correct
67 Correct 3 ms 5844 KB Output is correct
68 Correct 16 ms 6408 KB Output is correct
69 Correct 15 ms 6484 KB Output is correct
70 Correct 15 ms 6484 KB Output is correct
71 Correct 819 ms 36332 KB Output is correct
72 Correct 824 ms 36072 KB Output is correct
73 Correct 1064 ms 29496 KB Output is correct
74 Correct 1308 ms 41280 KB Output is correct
75 Correct 1313 ms 41328 KB Output is correct
76 Correct 1354 ms 41416 KB Output is correct
77 Correct 1069 ms 41292 KB Output is correct
78 Correct 1113 ms 41304 KB Output is correct
79 Correct 1025 ms 41392 KB Output is correct
80 Correct 873 ms 41032 KB Output is correct
81 Correct 793 ms 40988 KB Output is correct
82 Correct 873 ms 41288 KB Output is correct
83 Correct 936 ms 41200 KB Output is correct
84 Correct 850 ms 28876 KB Output is correct
85 Correct 875 ms 24848 KB Output is correct
86 Correct 796 ms 24112 KB Output is correct
87 Correct 1326 ms 34968 KB Output is correct
88 Correct 1309 ms 34184 KB Output is correct
89 Correct 1273 ms 34964 KB Output is correct
90 Correct 1198 ms 34828 KB Output is correct
91 Correct 1297 ms 34660 KB Output is correct
92 Correct 1463 ms 38768 KB Output is correct
93 Correct 1506 ms 40140 KB Output is correct
94 Correct 1414 ms 34816 KB Output is correct
95 Correct 1442 ms 34672 KB Output is correct
96 Correct 1620 ms 34588 KB Output is correct
97 Correct 1394 ms 34600 KB Output is correct
98 Correct 1147 ms 34420 KB Output is correct
99 Correct 1137 ms 34368 KB Output is correct
100 Correct 1199 ms 34252 KB Output is correct
101 Correct 1182 ms 34312 KB Output is correct
102 Correct 1097 ms 35308 KB Output is correct
103 Correct 1016 ms 35144 KB Output is correct
104 Correct 1033 ms 35256 KB Output is correct
105 Correct 826 ms 34716 KB Output is correct
106 Correct 790 ms 34532 KB Output is correct
107 Correct 846 ms 34752 KB Output is correct
108 Correct 840 ms 34800 KB Output is correct