This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
typedef pair<int,int> pi;
int n,a[600005];
int lp[300005], rp[300005];
struct rmaxq{
int lim;
pi tree[1650000];
void init(int n){
for(lim = 1; lim <= n; lim <<= 1);
}
void add(int x, pi v){
x += lim;
tree[x] = v;
while(x > 1){
x >>= 1;
tree[x] = max(tree[2*x],tree[2*x+1]);
}
}
pi q(int s, int e){
s += lim;
e += lim;
pi r(0,0);
while(s < e){
if(s%2 == 1) r = max(r,tree[s++]);
if(e%2 == 0) r = max(r,tree[e--]);
s >>= 1;
e >>= 1;
}
if(s == e) r = max(r,tree[s]);
return r;
}
}rmaxq;
struct rminq{
int lim;
pi tree[1650000];
void init(int n){
fill(tree,tree+1650000,pi(1e9,1e9));
for(lim = 1; lim <= n; lim <<= 1);
}
void add(int x, pi v){
x += lim;
tree[x] = v;
while(x > 1){
x >>= 1;
tree[x] = min(tree[2*x],tree[2*x+1]);
}
}
pi q(int s, int e){
s += lim;
e += lim;
pi r(1e9,1e9);
while(s < e){
if(s%2 == 1) r = min(r,tree[s++]);
if(e%2 == 0) r = min(r,tree[e--]);
s >>= 1;
e >>= 1;
}
if(s == e) r = min(r,tree[s]);
return r;
}
}rminq;
int lab[300005];
void dfs(int x, int l){
lab[x] = l;
rmaxq.add(lp[x],pi(-1,-1));
rminq.add(rp[x],pi(1e9,1e9));
while (1) {
pi t = rmaxq.q(lp[x]+1,rp[x]-1);
if(t.first > rp[x]){
dfs(t.second,3-l);
}
else break;
}
while (1) {
pi t = rminq.q(lp[x]+1,rp[x]-1);
if(t.first < lp[x]){
dfs(t.second,3-l);
}
else break;
}
}
vector<int> v1, v2, st;
vector<pi> tmp;
bool eval(vector<int> &v){
tmp.clear();
st.clear();
for (auto &i : v){
tmp.push_back(pi(lp[i],i));
tmp.push_back(pi(rp[i],-i));
}
sort(tmp.begin(),tmp.end());
for (auto &i : tmp){
if(i.second > 0){
st.push_back(i.second);
}
else{
if(st.empty() || st.back() != -i.second) return 0;
st.pop_back();
}
}
return 1;
}
int main(){
scanf("%d",&n);
for (int i=1; i<=2*n; i++) {
scanf("%d",&a[i]);
if(lp[a[i]] == 0) lp[a[i]] = i;
else rp[a[i]] = i;
}
rmaxq.init(2*n);
rminq.init(2*n);
for (int i=1; i<=n; i++){
rmaxq.add(lp[i],pi(rp[i],i));
rminq.add(rp[i],pi(lp[i],i));
}
for (int i=1; i<=n; i++){
if(!lab[i]) dfs(i,1);
}
for (int i=1; i<=n; i++) {
if(lab[i] == 1){
v1.push_back(i);
}
else{
v2.push_back(i);
}
}
if(!eval(v1) || !eval(v2)){
puts("IMPOSSIBLE");
return 0;
}
for (int i=1; i<=2*n; i++) {
putchar(lab[a[i]] == 1 ? '^' : 'v');
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |