Submission #835331

# Submission time Handle Problem Language Result Execution time Memory
835331 2023-08-23T13:09:35 Z arnold518 Highway Tolls (IOI18_highway) C++17
100 / 100
239 ms 12140 KB
#include "highway.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int MAXN = 9e4;
const int MAXM = 13e4;

int N, M, S, T;
ll A, B, D;
pii E[MAXM+10];
vector<int> adj[MAXN+10];

int dist1[MAXN+10], dist2[MAXN+10];
int P[MAXN+10];

int TT[MAXM+10];
ll query()
{
	vector<int> V;
	//for(int i=1; i<=M; i++) printf("%d ", TT[i]); printf("\n");
	for(int i=0; i<M; i++) V.push_back(TT[i+1]);
	return ask(V);
}

void find_pair(int _N, vector<int> _U, vector<int> _V, int _A, int _B)
{
	N=_N; M=_U.size();
	for(int i=1; i<=M; i++) E[i]={_U[i-1]+1, _V[i-1]+1};
	for(int i=1; i<=M; i++)
	{
		adj[E[i].first].push_back(E[i].second);
		adj[E[i].second].push_back(E[i].first);
	}
	A=_A; B=_B;

	for(int i=1; i<=M; i++) TT[i]=0;
	D=query()/A;

	int lo=0, hi=M;
	while(lo+1<hi)
	{
		int mid=lo+hi>>1;
		for(int i=1; i<=M; i++) TT[i]=0;
		for(int i=1; i<=mid; i++) TT[i]=1;
		if(D*A!=query()) hi=mid;
		else lo=mid;
	}

	queue<int> Q;
	for(int i=1; i<=N; i++) dist1[i]=-1;
	Q.push(E[hi].first); dist1[E[hi].first]=0;
	while(!Q.empty())
	{
		int now=Q.front(); Q.pop();
		for(auto nxt : adj[now])
		{
			if(dist1[nxt]!=-1) continue;
			dist1[nxt]=dist1[now]+1;
			Q.push(nxt);
		}
	}

	for(int i=1; i<=N; i++) dist2[i]=-1;
	Q.push(E[hi].second); dist2[E[hi].second]=0;
	while(!Q.empty())
	{
		int now=Q.front(); Q.pop();
		for(auto nxt : adj[now])
		{
			if(dist2[nxt]!=-1) continue;
			dist2[nxt]=dist2[now]+1;
			Q.push(nxt);
		}
	}
	int num=hi;

	vector<pii> V1, V2;
	for(int i=1; i<=N; i++)
	{
		if(dist1[i]<dist2[i]) V1.push_back({dist1[i], i});
		if(dist1[i]>dist2[i]) V2.push_back({dist2[i], i});
	}

	sort(V1.begin(), V1.end());
	sort(V2.begin(), V2.end());

	for(int i=0; i<V1.size(); i++) P[V1[i].second]=i+1;
	for(int i=0; i<V2.size(); i++) P[V2[i].second]=-(i+1);

	//for(int i=1; i<=N; i++) printf("%d ", P[i]); printf("\n");

	lo=0, hi=V1.size();
	while(lo+1<hi)
	{
		int mid=lo+hi>>1;
		for(int i=1; i<=M; i++)
		{
			auto [u, v]=E[i];
			if(P[u]==0 || P[v]==0) TT[i]=1;
			else if((P[u]>0)!=(P[v]>0)) TT[i]=1;
			else if(P[u]<0) TT[i]=0;
			else if(P[u]<=mid && P[v]<=mid) TT[i]=0;
			else TT[i]=1;
		}
		TT[num]=0;
		if(D*A!=query()) lo=mid;
		else hi=mid;
	}
	for(int i=1; i<=N; i++) if(P[i]==hi) S=i;
	//printf("!%d\n", hi);

	lo=0, hi=V2.size();
	while(lo+1<hi)
	{
		int mid=lo+hi>>1;
		for(int i=1; i<=M; i++)
		{
			auto [u, v]=E[i];
			if(P[u]==0 || P[v]==0) TT[i]=1;
			else if((P[u]>0)!=(P[v]>0)) TT[i]=1;
			else if(P[u]>0) TT[i]=0;
			else if(-P[u]<=mid && -P[v]<=mid) TT[i]=0;
			else TT[i]=1;
		}
		TT[num]=0;
		if(D*A!=query()) lo=mid;
		else hi=mid;
	}
	for(int i=1; i<=N; i++) if(-P[i]==hi) T=i;
	//printf("!%d\n", hi);

	answer(S-1, T-1);
}

Compilation message

highway.cpp: In function 'void find_pair(int, std::vector<int>, std::vector<int>, int, int)':
highway.cpp:46:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   46 |   int mid=lo+hi>>1;
      |           ~~^~~
highway.cpp:91:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |  for(int i=0; i<V1.size(); i++) P[V1[i].second]=i+1;
      |               ~^~~~~~~~~~
highway.cpp:92:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |  for(int i=0; i<V2.size(); i++) P[V2[i].second]=-(i+1);
      |               ~^~~~~~~~~~
highway.cpp:99:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   99 |   int mid=lo+hi>>1;
      |           ~~^~~
highway.cpp:119:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  119 |   int mid=lo+hi>>1;
      |           ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 2 ms 2432 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 1 ms 2384 KB Output is correct
12 Correct 1 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2512 KB Output is correct
2 Correct 14 ms 3276 KB Output is correct
3 Correct 135 ms 10496 KB Output is correct
4 Correct 110 ms 10472 KB Output is correct
5 Correct 147 ms 10488 KB Output is correct
6 Correct 112 ms 10488 KB Output is correct
7 Correct 115 ms 10476 KB Output is correct
8 Correct 97 ms 10484 KB Output is correct
9 Correct 117 ms 10532 KB Output is correct
10 Correct 137 ms 10476 KB Output is correct
11 Correct 126 ms 10676 KB Output is correct
12 Correct 112 ms 10492 KB Output is correct
13 Correct 125 ms 10380 KB Output is correct
14 Correct 136 ms 10520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 3464 KB Output is correct
2 Correct 24 ms 4208 KB Output is correct
3 Correct 32 ms 5164 KB Output is correct
4 Correct 89 ms 10356 KB Output is correct
5 Correct 80 ms 10360 KB Output is correct
6 Correct 98 ms 10500 KB Output is correct
7 Correct 90 ms 10424 KB Output is correct
8 Correct 78 ms 10380 KB Output is correct
9 Correct 106 ms 10372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2512 KB Output is correct
2 Correct 13 ms 3392 KB Output is correct
3 Correct 72 ms 8872 KB Output is correct
4 Correct 140 ms 10468 KB Output is correct
5 Correct 111 ms 10468 KB Output is correct
6 Correct 100 ms 10484 KB Output is correct
7 Correct 140 ms 10540 KB Output is correct
8 Correct 95 ms 10576 KB Output is correct
9 Correct 110 ms 10580 KB Output is correct
10 Correct 138 ms 10716 KB Output is correct
11 Correct 136 ms 10344 KB Output is correct
12 Correct 123 ms 10404 KB Output is correct
13 Correct 132 ms 10476 KB Output is correct
14 Correct 115 ms 10532 KB Output is correct
15 Correct 87 ms 10516 KB Output is correct
16 Correct 90 ms 10636 KB Output is correct
17 Correct 112 ms 10556 KB Output is correct
18 Correct 129 ms 10396 KB Output is correct
19 Correct 108 ms 10480 KB Output is correct
20 Correct 117 ms 10448 KB Output is correct
21 Correct 95 ms 10972 KB Output is correct
22 Correct 90 ms 10972 KB Output is correct
23 Correct 117 ms 11216 KB Output is correct
24 Correct 125 ms 10984 KB Output is correct
25 Correct 105 ms 10788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 3280 KB Output is correct
2 Correct 15 ms 3464 KB Output is correct
3 Correct 144 ms 10928 KB Output is correct
4 Correct 192 ms 11200 KB Output is correct
5 Correct 150 ms 11912 KB Output is correct
6 Correct 181 ms 12004 KB Output is correct
7 Correct 153 ms 11880 KB Output is correct
8 Correct 179 ms 11860 KB Output is correct
9 Correct 133 ms 9336 KB Output is correct
10 Correct 130 ms 9596 KB Output is correct
11 Correct 134 ms 9908 KB Output is correct
12 Correct 217 ms 11204 KB Output is correct
13 Correct 239 ms 11664 KB Output is correct
14 Correct 190 ms 11768 KB Output is correct
15 Correct 182 ms 11844 KB Output is correct
16 Correct 147 ms 10200 KB Output is correct
17 Correct 117 ms 11008 KB Output is correct
18 Correct 97 ms 11064 KB Output is correct
19 Correct 95 ms 11108 KB Output is correct
20 Correct 136 ms 11124 KB Output is correct
21 Correct 186 ms 11916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 3272 KB Output is correct
2 Correct 15 ms 3476 KB Output is correct
3 Correct 131 ms 10972 KB Output is correct
4 Correct 151 ms 10996 KB Output is correct
5 Correct 142 ms 11276 KB Output is correct
6 Correct 180 ms 11916 KB Output is correct
7 Correct 110 ms 10972 KB Output is correct
8 Correct 121 ms 11000 KB Output is correct
9 Correct 178 ms 11348 KB Output is correct
10 Correct 162 ms 12028 KB Output is correct
11 Correct 169 ms 12140 KB Output is correct
12 Correct 161 ms 12004 KB Output is correct
13 Correct 145 ms 10016 KB Output is correct
14 Correct 145 ms 9640 KB Output is correct
15 Correct 139 ms 9800 KB Output is correct
16 Correct 121 ms 9616 KB Output is correct
17 Correct 116 ms 9796 KB Output is correct
18 Correct 139 ms 9628 KB Output is correct
19 Correct 216 ms 11152 KB Output is correct
20 Correct 144 ms 11572 KB Output is correct
21 Correct 184 ms 11848 KB Output is correct
22 Correct 162 ms 11764 KB Output is correct
23 Correct 198 ms 12096 KB Output is correct
24 Correct 168 ms 11828 KB Output is correct
25 Correct 193 ms 11792 KB Output is correct
26 Correct 159 ms 11804 KB Output is correct
27 Correct 133 ms 11092 KB Output is correct
28 Correct 109 ms 11020 KB Output is correct
29 Correct 106 ms 11208 KB Output is correct
30 Correct 99 ms 11120 KB Output is correct
31 Correct 93 ms 11120 KB Output is correct
32 Correct 99 ms 10996 KB Output is correct
33 Correct 137 ms 11192 KB Output is correct
34 Correct 130 ms 11064 KB Output is correct
35 Correct 104 ms 11048 KB Output is correct
36 Correct 136 ms 11068 KB Output is correct
37 Correct 122 ms 11188 KB Output is correct
38 Correct 94 ms 11124 KB Output is correct
39 Correct 158 ms 11936 KB Output is correct
40 Correct 145 ms 11852 KB Output is correct
41 Correct 182 ms 11916 KB Output is correct
42 Correct 199 ms 12024 KB Output is correct