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>
using namespace std;
int N;
int l[200100],r[200100];
queue<pair<int,int>> q;
int d0[200100];
int dn[200100];
int ans[200100];
int Q;
const int inf = 1e9;
vector<pair<int,int>> v;
struct node{
int s,e,m;
vector<int> v;
node *l,*r;
node(int S, int E){
s = S;
e = E;
m = (s + e)/2;
v = {};
if(s != e){
l = new node(s,m);
r = new node(m + 1, e);
}
}
void update(int S, int E, int k){
if(S <= s && e <= E){
v.push_back(k);
return;
}
if(S <= m){
l -> update(S,E,k);
}
if(m < E){
r -> update(S,E,k);
}
}
void pop(int i, int k){
while(!v.empty()){
q.push({v.back(),k});
v.pop_back();
}
if(s == e) return;
if(i <= m) l -> pop(i,k);
else r -> pop(i,k);
}
}*root;
struct fode{
int s,e,m;
int v;
fode *l, *r;
fode(int S, int E, int *lst){
s = S;
e = E;
m = (s + e)/2;
if(s == e){
if(lst[s] != -1) v = lst[s];
else v = inf;
}
else{
l = new fode(s,m,lst);
r = new fode(m + 1,e,lst);
v = min(l -> v, r -> v);
}
}
int query(int S, int E){
if(S <= s && e <= E) return v;
int V = inf;
if(S <= m) V = min(V,l -> query(S,E));
if(m < E) V = min(V,r -> query(S,E));
return V;
}
}*noot, *ooot;
int main(){
scanf(" %d",&N);
root = new node(0,N-1);
for(int i = 0; i < N; i++){
scanf(" %d",&l[i]);
scanf(" %d",&r[i]);
l[i]--;
r[i]--;
}
for(int i = 0; i < N; i++){
root -> update(l[i],r[i],i);
d0[i] = -1;
}
root -> pop(0,1);
while(!q.empty()){
pair<int,int> ii = q.front();
q.pop();
int i = ii.first;
if(d0[i] != -1) continue;
d0[i] = ii.second;
root -> pop(i,ii.second + 1);
}
root = new node(0,N-1);
for(int i = 0; i < N; i++){
root -> update(l[i],r[i],i);
dn[i] = -1;
}
root -> pop(N - 1, 1);
while(!q.empty()){
pair<int,int> ii = q.front();
q.pop();
int i = ii.first;
if(dn[i] != -1) continue;
dn[i] = ii.second;
root -> pop(i,ii.second + 1);
}
ooot = new fode(0,N-1,d0);
noot = new fode(0,N-1,dn);
root = new node(0,N-1);
for(int i = 0; i < N; i++){
int num = 1;
if(d0[i] >= 0 && dn[i] >= 0) num += min(d0[i] - 1, ooot -> query(l[i],r[i])) + min(dn[i] - 1, noot -> query(l[i],r[i]));
else num = inf;
if(num <= inf - 10) v.push_back({num,i});
root -> update(l[i],r[i],i);
ans[i] = -1;
}
sort(v.begin(),v.end());
auto it = 0;
while(!q.empty() || it != v.size()){
pair<int,int> ii;
if(it == N){
ii = q.front();
q.pop();
}
else if(q.empty()){
ii = make_pair(v[it].second,v[it].first);
it++;
}
else{
if(q.front().second <= v[it].first){
ii = q.front();
q.pop();
}
else{
ii = make_pair(v[it].second,v[it].first);
it++;
}
}
int i = ii.first;
if(ans[i] != -1) continue;
ans[i] = ii.second;
root -> pop(i,ans[i] + 1);
}
scanf(" %d",&Q);
for(int i = 0 ; i < Q; i++){
int h;
scanf(" %d",&h);
h--;
printf("%d\n",ans[h]);
}
}
Compilation message (stderr)
passport.cpp: In function 'int main()':
passport.cpp:167:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
167 | while(!q.empty() || it != v.size()){
| ~~~^~~~~~~~~~~
passport.cpp:96:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | scanf(" %d",&N);
| ~~~~~^~~~~~~~~~
passport.cpp:101:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | scanf(" %d",&l[i]);
| ~~~~~^~~~~~~~~~~~~
passport.cpp:102:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | scanf(" %d",&r[i]);
| ~~~~~^~~~~~~~~~~~~
passport.cpp:197:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
197 | scanf(" %d",&Q);
| ~~~~~^~~~~~~~~~
passport.cpp:201:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
201 | scanf(" %d",&h);
| ~~~~~^~~~~~~~~~
# | 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... |