이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define pb push_back
using namespace std;
const int N = 1e6+6,inf = 1e8;
int F[N],ANS[N];
vector<pii> Q[N];
void qer(int v,int tl,int tr,int l,int r,int QL,int QR) {
if (l > tr || r < tl) return;
if (l <= tl && r >= tr) {
Q[v].pb({QL,QR});
return;
}
int tm = (tl + tr)/2;
qer(v*2,tl,tm,l,r,QL,QR);
qer(v*2+1,tm+1,tr,l,r,QL,QR);
}
// x >= PX, y-minimal
// x <= PX, y max
int cans=INT_MAX;
int t[2][4*N],timer;
int *H[100000000];
int val[100000000];
void change(int&x,int c) {
++timer;
H[timer] = &x;
val[timer] = x;
x = c;
}
void rollback() {
(*H[timer]) = val[timer];
timer--;
}
void add(int v,int tl,int tr,int pos,int val,int type) {
if (tl == tr) {
if (type == 0)
change(t[0][v],min(t[0][v],val));
else change(t[1][v],max(t[1][v],val));
// t[0][v] += val;
return;
}
int tm = (tl + tr)/2;
if (pos <= tm) {
add(v*2,tl,tm,pos,val,type);
}
else add(v*2+1,tm+1,tr,pos,val,type);
if (type == 0)
change(t[0][v],min(t[0][v*2],t[0][v*2+1]));
else
change(t[1][v],max(t[1][v*2],t[1][v*2+1]));
//t[v] = t[v*2] + t[v*2+1];
}
int get(int v,int tl,int tr,int l,int r,int type) {
if (l > tr || r < tl) {
if (type == 0) return inf;
else return -inf;
}
if (l <= tl && r >= tr) return t[type][v];
int tm = (tl + tr)/2;
if (type == 0)
return min(get(v*2,tl,tm,l,r,type),get(v*2+1,tm+1,tr,l,r,type));
else return max(get(v*2,tl,tm,l,r,type),get(v*2+1,tm+1,tr,l,r,type));
}
void dfs(int v,int tl,int tr) {
int in_ans = cans;
int start_time = timer;
for (auto i1 : Q[v]) {
int l = i1.fi;
int r = i1.se;
cans = min(cans, -l + get(1,1,1e6,r,1e6,0));
cans = min(cans, r - get(1,1,1e6,1,l,1));
add(1,1,1e6,l,r,0); // l-ze patara r-ebshi udidesi l, r-ze did l-ebshi umciresi r
add(1,1,1e6,r,l,1);
// UPD_X(1,1,1e6,1);
}
if (tl != tr) {
int tm = (tl + tr)/2;
dfs(v*2,tl,tm);
dfs(v*2+1,tm+1,tr);
}else {
if (F[tl])
ANS[tl] = cans;//,cout << cans <<'s'<<'\n';
}
while(start_time != timer) {
rollback();
}/*
for (auto i1 : Q[v]) {
int l = i1.fi;
int r = i1.se;
// PX = r;
// PY = r;
// cans = min(cans, -l + get_x(1,1,1e6);
// PX = l;
// cans = min(cans, r - GET_X(1,1,1e6));
PX = l;
PY = r;
upd_x(1,1,1e6,-1); // l-ze patara r-ebshi udidesi l, r-ze did l-ebshi umciresi r
PX = r;
PY = l;
UPD_X(1,1,1e6,-1);
}*/
cans = in_ans;
}
main() {
int q;
cin >> q;
multiset<int> L,R;
multiset<int> AL[1000004],AR[1000004];
map<pii,vector<int> > cnt;
for (int i = 1; i < 4*1000000;i ++)
t[0][i] = inf, t[1][i] = -inf;
for (int i = 1; i <= q; i++) {
char c;
int l,r;
cin >> c >> l >> r;
if (c == 'A') {
cnt[{l,r}].pb(i);
L.insert(l);
R.insert(r);
AR[l].insert(r);
AL[r].insert(l);
}else {
qer(1,1,q,cnt[{l,r}].back(),i-1,l,r);
cnt[{l,r}].pop_back();
if (cnt[{l,r}].empty())
cnt.erase({l,r});
// query.push_back({cnt[{l,r}].back(),i})
AL[r].erase(AL[r].find(l));
AR[l].erase(AR[l].find(r));
// AL[r].erase(find(all(AL[r]),l));
// AR[l].erase(find(all(AR[l]),r));
L.erase(L.find(l));
R.erase(R.find(r));
}
l = *L.rbegin();
r = *R.begin();
int len = max(0,r - l);
if (len) {
int l1 = l, r1 = r;
r = *AR[l1].begin();
l = *AL[r1].rbegin();
ANS[i] = r-l;
// cout << r - l <<'\n';
}else {
F[i] = 1;
}}
for (auto&i : cnt) {
for (int j : i.second)
qer(1,1,q,j,q,i.fi.fi,i.fi.se);
}
dfs(1,1,q);
for (int i = 1; i <= q; i++)
cout << ANS[i] <<'\n';
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp:127:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
127 | main() {
| ^
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |