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 fi first
#define se second
#define sz(x) ((int)(x).size())
using namespace std;
using lli = long long;
using pii = pair<int,int>;
using pli = pair<lli,int>;
using pll = pair<lli,lli>;
const int MAX = 1e8;
int n,m,q;
vector<pair<int,pii>> ti_a[300001];
vector<pair<int,pii>> init() {
vector<pair<int,pii>> ret;
for(int i=1;i<=m;i++) {
sort(ti_a[i].begin(),ti_a[i].end());
for(int j=0,k=0;j<sz(ti_a[i]);j=k) {
int mx = ti_a[i][j].se.se, mn = ti_a[i][j].se.fi;
for(k=j;k<sz(ti_a[i]) && ti_a[i][j].fi == ti_a[i][k].fi &&
ti_a[i][k].se.fi<=mx+1;k++) mx = max(mx, ti_a[i][k].se.se);
//printf("%d %d\n",ti_a[i][j].fi, ti_a[i][k].fi);
//printf("#%d : %d %d %d\n",i,mn,mx,k);
ret.push_back({mn, pii(ti_a[i][j].fi, i)});
ret.push_back({mx+1, pii(ti_a[i][j].fi, -i)});
}
}
return ret;
}
set<int> pt[300001];
set<pair<int,pii>> rs;
int ac;
void ins(int i,int pos) {
auto &tr=pt[i];
if(tr.empty()) {
ac++;
rs.insert({pos,pii(i,-1)});
rs.insert({pos,pii(i,1)});
} else {
if(*tr.begin() > pos) {
rs.erase({*tr.begin(), pii(i, -1)});
rs.insert({pos, pii(i,-1)});
}
if(*prev(tr.end()) < pos) {
rs.erase({*prev(tr.end()), pii(i,1)});
rs.insert({pos, pii(i,1)});
}
}
tr.insert(pos);
}
void del(int i,int pos) {
auto &tr = pt[i];
if(sz(tr)==1) {
rs.erase({*tr.begin(), pii(i,-1)});
rs.erase({*tr.begin(), pii(i,1)});
ac--;
tr.erase(tr.begin());
} else {
if(*tr.begin() == pos) {
rs.erase({*tr.begin(), pii(i,-1)});
rs.insert({*next(tr.begin()), pii(i,-1)});
}
if(*prev(tr.end()) == pos) {
rs.erase({*prev(tr.end()), pii(i,1)});
rs.insert({*prev(prev(tr.end())), pii(i,1)});
}
tr.erase(pos);
}
}
int get(int pos) {
if(ac < m) return -1;
else return max(pos - rs.begin()->fi, prev(rs.end())->fi - pos);
}
int ans[300001];
int main() {
scanf("%d%d%d",&n,&m,&q);
for(int i=0;i<n;i++) {
int x,t,a,b;
scanf("%d%d%d%d",&x,&t,&a,&b);
ti_a[t].push_back({x,pii(a,b)});
}
vector<pair<int,pii>> sa = init(),qa;
for(int i=0;i<q;i++) {
int t,x;
scanf("%d%d",&x,&t);
qa.push_back({t, pii(x, i)});
}
sort(qa.begin(),qa.end());
sort(sa.begin(),sa.end());
//for(auto &it:sa) printf("(%d %d %d) ",it.fi,it.se.fi,it.se.se);
//puts("");
for(int i=0,j=0;i<q;i++) {
for(;j<sz(sa) && sa[j].fi <= qa[i].fi;j++) {
if(sa[j].se.se<0) del(-sa[j].se.se, sa[j].se.fi);
else ins(sa[j].se.se, sa[j].se.fi);
}
if(ac < m) ans[qa[i].se.se] = -1;
else ans[qa[i].se.se] = get(qa[i].se.fi);
}
for(int i=0;i<q;i++) printf("%d\n",ans[i]);
return 0;
}
Compilation message (stderr)
new_home.cpp: In function 'int main()':
new_home.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&n,&m,&q);
~~~~~^~~~~~~~~~~~~~~~~~~
new_home.cpp:83:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d",&x,&t,&a,&b);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:90:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&x,&t);
~~~~~^~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |