# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
229843 | frodakcin | Editor (BOI15_edi) | C++11 | 0 ms | 0 KiB |
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 <set>
#include <cassert>
struct upd
{
public:
int t, l;
bool operator < (upd o) const {return l > o.l || !(o.l > l) && t < o.t;}
};
const int MN = 3e5 + 100;
int N, a[MN], f[MN], B;
upd b[MN];
std::set<int> c, h;
bool d[MN], g[MN];
std::vector<int> e[MN];
void dfs(int n, int v)
{
f[n] = v;
for(int x:e[n])
dfs(x, v);
}
int main(void)
{
scanf("%d", &N);
for(int i=1;i<=N;++i)
scanf("%d", a+i);
for(int i=1;i<=N;++i)
{
if(a[i]<0)
b[B++] = {i, -a[i]};
c.insert(i);
}
std::sort(b, b+B);
for(int i=0;i<B;++i)
{
//printf("B VALS: %d %d\n", b[i].t, b[i].l);
auto v = c.lower_bound(b[i].t);
assert(v != c.begin());
if(d[b[i].t])
e[*--v].push_back(b[i].t);
else
{
e[*--v].push_back(b[i].t);
d[*v] = d[b[i].t] = 1;
c.erase(c.erase(v));
}
}
for(int i=1;i<=N;++i)
if(a[i]>=0)
dfs(i, i);
h.insert(0);
for(int i=1;i<=N;++i)
{
if(g[f[i]])
h.erase(f[i]);
else
h.insert(f[i]);
g[f[i]]^=1;
printf("%d\n", a[*std::prev(h.end())]);
}
return 0;
}