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<bits/stdc++.h>
#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define inf 1e18
#define fi first
#define se second
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define endl '\n'
#define pi 3.14159265359
#define TASKNAME "editor"
using namespace std;
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
const int MAXN = 3e5 + 9;
int n;
int a[MAXN];
namespace subtask1{
bool check(){
return n <= 5000;
}
int active[MAXN], link[MAXN];
void solve(){
memset(active, false, sizeof(active));
FOR(i, 1, n){
active[i] = true;
link[i] = 0;
if (a[i] < 0){
FORD(j, i - 1, 1){
if (active[j] and (a[j] > 0 or abs(a[j]) < abs(a[i]))) {
link[i] = j;
break;
}
}
}
int cur = i, res = 0;
while(cur > 0){
cur = link[cur];
active[cur] = !active[cur];
}
FOR(i, 1, n){
if (a[i] > 0 and active[i]) res = a[i];
}
cout << res << endl;
}
}
}
namespace subtask2{
bool check(){
FOR(i, 1, n){
if (a[i] < -1) return false;
}
return true;
}
void solve(){
vector<int> stk;
FOR(i, 1, n){
if (a[i] > 0) stk.pb(a[i]);
else stk.pop_back();
if (stk.size() > 0) cout << stk.back() << endl;
else cout << 0 << endl;
}
}
}
struct IT{
struct Node{
int mx = -inf;
};
vector<Node> st;
int sz = 0;
IT(int _sz = 0){
sz = _sz;
st.resize(sz * 4 + 9);
}
Node mergeNode(Node &a, Node &b){
Node res;
res.mx = max(a.mx, b.mx);
return res;
}
void updatePos(int node, int l, int r, int pos, int val){
if (l == r){
st[node].mx = val;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid) updatePos(node << 1, l, mid, pos, val);
else updatePos(node << 1 | 1, mid + 1, r, pos, val);
st[node] = mergeNode(st[node << 1], st[node << 1 | 1]);
}
int minIndexWithLowerbound(int node, int l, int r, int u, int v, int lb){
int mid = (l + r) >> 1;
if (l >= u and r <= v){
if (st[node].mx < lb) return -1;
if (l == r) return l;
if (st[node << 1].mx >= lb){
return minIndexWithLowerbound(node << 1, l, mid, u, v, lb);
}
else {
return minIndexWithLowerbound(node << 1 | 1, mid + 1, r, u, v, lb);
}
}
if (l > v or r < u) return -1;
int g1 = minIndexWithLowerbound(node << 1, l, mid, u, v, lb);
int g2 = minIndexWithLowerbound(node << 1 | 1, mid + 1, r, u, v, lb);
return ((g1 == -1) ? g2 : g1);
}
void updatePos(int pos, int val){
updatePos(1, 0, sz, pos, val);
}
int minIndexWithLowerbound(int u, int v, int lb){
if (u > v) return -1;
return minIndexWithLowerbound(1, 0, sz, u, v, lb);
}
};
namespace subtask3{
bool check(){
return true;
}
IT undone;
int curState[MAXN];
int dfs(int u){
int w = (a[u] > 0) ? 0 : abs(a[u]);
int f = undone.minIndexWithLowerbound(u + 1, n, w + 1);
curState[u] = 1;
while (f != -1 and curState[u] == 1) {
undone.updatePos(f, -inf);
curState[u] = curState[u] & (!dfs(f));
f = undone.minIndexWithLowerbound(u + 1, n , w + 1);
}
return curState[u];
}
void solve(){
undone = IT(n + 9);
FOR(i, 1, n){
if (a[i] < 0) undone.updatePos(i, -a[i]);
}
FORD(i, n, 1){
if (a[i] > 0){
dfs(i);
}
}
int res = 0;
FOR(i, 1, n - 1){
cout << 0 << endl;
}
FORD(i, n, 1){
if (a[i] > 0 and curState[i] == 1) {
cout << a[i] << endl;
return;
}
}
cout << 0 << endl;
return;
}
}
main()
{
fast;
if (fopen(TASKNAME".inp","r")){
freopen(TASKNAME".inp","r",stdin);
freopen(TASKNAME".out","w",stdout);
}
cin >> n;
FOR(i, 1, n){
cin >> a[i];
}
//
if (subtask1::check()) return subtask1::solve(), 0;
if (subtask2::check()) return subtask2::solve(), 0;
if (subtask3::check()) return subtask3::solve(), 0;
}
/**
Warning:
- MLE / TLE?
- Gioi han mang?
- Gia tri max phai luon gan cho -INF
- long long co can thiet khong?
- tran mang.
- code can than hon
- Nho sinh test de tranh RTE / TLE
--> Coi lai truoc khi nop
**/
Compilation message (stderr)
edi.cpp: In function 'void subtask3::solve()':
edi.cpp:182:13: warning: unused variable 'res' [-Wunused-variable]
182 | int res = 0;
| ^~~
edi.cpp: At global scope:
edi.cpp:196:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
196 | main()
| ^~~~
edi.cpp: In function 'int main()':
edi.cpp:200:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
200 | freopen(TASKNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
edi.cpp:201:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
201 | freopen(TASKNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |