Submission #40814

# Submission time Handle Problem Language Result Execution time Memory
40814 2018-02-08T10:35:28 Z IvanC Pictionary (COCI18_pictionary) C++14
140 / 140
275 ms 5468 KB
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
struct pergunta{
	int ini,fim,resp,meio,u,v,id;
	pergunta(int _ini,int _fim,int _u,int _v,int _id){
		ini = _ini;
		fim = _fim;
		resp = -1;
		u = _u;
		v = _v;
		id = _id;
	}
	bool operator<(const pergunta &other)const{
		return meio > other.meio;
	}
};
vector<pergunta> P;
int N,M,Q,pai[MAXN],peso[MAXN],resposta[MAXN];
void clear(){
	for(int i = 1;i<=N;i++){
		pai[i] = i;
		peso[i] = 1; 
	}
}
int find(int x){
	if(x == pai[x]) return x;
	return pai[x] = find(pai[x]);
}
void join(int x,int y){
	x = find(x);
	y = find(y);
	if(x == y) return;
	if(peso[x] < peso[y]) swap(x,y);
	pai[y] = x;
	peso[x] += peso[y];
}
void uniao(int v){
	for(int u = 2*v;u<=N;u+=v){
		join(u,v);
	}
}
int main(){
	scanf("%d %d %d",&N,&M,&Q);
	for(int i = 1;i<=Q;i++){
		int u,v;
		scanf("%d %d",&u,&v);
		pergunta davez(1,M,u,v,i);
		P.push_back(davez);
	}
	for(int iteracao = 1;iteracao<=17;iteracao++){
		clear();
		for(int i = 0;i<Q;i++){
			if(P[i].ini > P[i].fim){
				P[i].meio = -1;
			}
			else{
				P[i].meio = (P[i].ini + P[i].fim)/2;
			}
		}
		sort(P.begin(),P.end());
		int atual = M;
		for(int i = 0;i<Q;i++){
			if(P[i].meio == -1) continue;
			while(atual >= P[i].meio){
				uniao(atual);
				atual--;
			}
			if(find(P[i].u) == find(P[i].v)){
				P[i].resp = P[i].meio;
				P[i].ini = P[i].meio + 1;
			}
			else{
				P[i].fim = P[i].meio - 1;
			}
		}
	}
	for(int i = 0;i<Q;i++) resposta[P[i].id] = abs(P[i].resp - M) + 1;
	for(int i = 1;i<=Q;i++) printf("%d\n",resposta[i]);
	return 0;
}

Compilation message

pictionary.cpp: In function 'int main()':
pictionary.cpp:44:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d",&N,&M,&Q);
                            ^
pictionary.cpp:47:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&u,&v);
                       ^
# Verdict Execution time Memory Grader output
1 Correct 8 ms 664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 1740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 4352 KB Output is correct
2 Correct 95 ms 4452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 128 ms 4668 KB Output is correct
2 Correct 120 ms 4668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 4668 KB Output is correct
2 Correct 77 ms 4668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 4668 KB Output is correct
2 Correct 82 ms 4668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 136 ms 4668 KB Output is correct
2 Correct 106 ms 4668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 4668 KB Output is correct
2 Correct 187 ms 4668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 231 ms 4876 KB Output is correct
2 Correct 235 ms 4960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 275 ms 5468 KB Output is correct
2 Correct 245 ms 5468 KB Output is correct