Submission #365482

# Submission time Handle Problem Language Result Execution time Memory
365482 2021-02-11T17:41:22 Z kostia244 popa (BOI18_popa) C++17
100 / 100
131 ms 736 KB
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#ifdef EVAL
#include"popa.h"
#endif
#include<bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
 
namespace local {
	int ar[10010], qc = 0;
	int query(int a, int b, int c, int d) {
		qc++;
		int x = 0, y = 0;
		for(;a<=b;a++) x = __gcd(x, ar[a]);
		for(;c<=d;c++) y = __gcd(y, ar[c]);
		return x==y;
	}
};
#ifndef EVAL
using namespace local;
#endif
 
int nnn;
int q69(int a, int b, int c, int d) {
	if(min(a, c) < 0 || max(b, d) >= nnn || a > b || c > d) exit(7);
	return query(a, b, c, d);
}
 
#define query q69
int solve(int n, int *L, int *R) {
	nnn = n;
	vector<int> st {0};
	int rt = 0;
	vector<int> start(n);
	for(int i = 0; i < n; i++) L[i] = R[i] = -1;
	for(int i = 1; i < n; i++) {
		int ins = 0;
		while(st.size()) {
			int v = st.back();
			if(R[v] == -1) {//try becoming right child
				if(query(start[v], i, v, v)) {//possible
					R[v] = i;
					start[i] = i;
					ins = 1;break;
				} else {
					st.pop_back();
				}
			} else {//try putting right child as my left child
				if(!query(start[v], i, v, v)) {
					st.pop_back();continue;
				}
				int vr = R[v];
				if(696969) {//query(start[vr], i, i, i)
					R[v] = i;
					L[i] = vr;
					start[i] = start[vr];
					ins = 1;break;
				} else {
					st.pop_back();
				}
			}
		}
		if(!ins) {
			L[i] = rt;
			start[i] = 0;
			rt = i;
		}
		st.push_back(i);
	}
	return rt;
}
 
#ifndef EVAL
int main() {
	cin.tie(0)->sync_with_stdio(0);
	//multitest([&](){});
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> ar[i];
	vector<int> L(n), R(n);
	cout << solve(n, L.data(), R.data()) << endl;
	for(int i = 0; i < n; i++) cout << L[i] << " " << R[i] << '\n';
	cout << " :: " << qc << endl;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 9 ms 364 KB Output is correct
2 Correct 12 ms 364 KB Output is correct
3 Correct 12 ms 364 KB Output is correct
4 Correct 13 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 364 KB Output is correct
2 Correct 91 ms 396 KB Output is correct
3 Correct 57 ms 392 KB Output is correct
4 Correct 94 ms 516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 736 KB Output is correct
2 Correct 80 ms 484 KB Output is correct
3 Correct 104 ms 488 KB Output is correct
4 Correct 131 ms 492 KB Output is correct