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;
#define fileio() freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define fio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define allr(x) x.rbegin(), x.rend()
#define cmprs(x) sort(all(x)),x.erase(unique(all(x)),x.end())
#define endl "\n"
#define sp " "
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define rz resize
#define sz(a) (int)(a.size())
#define clr(a) a.clear()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef tuple<int, int, int> tpi;
typedef tuple<ll, ll, ll> tpl;
typedef pair<double, ll> pdl;
typedef pair<double, int> pdi;
const int dx[] = {1,-1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAX = 202020; // PLZ CHK!
struct segtr{
//0-indexed!
vector<ll> tr, a;
int n;
void rst(int sz) {
n=sz;
tr.resize((n+1)<<1);
}
void upd(int i, ll v) {
tr[i+n] = max(tr[i+n], v); i+=n; //AWARE OF UPD POLICY!!
for (i>>=1; i; i>>=1) tr[i] = max(tr[i<<1], tr[i<<1|1]);
}
ll qry(int l, int r) {
ll ret = -LINF;
for (l+=n, r+=n; l<=r; l>>=1, r>>=1) {
if (l&1) ret = max(ret, tr[l++]);
if (~r&1) ret = max(ret, tr[r--]);
}
return ret;
}
};
typedef array<int,3> arr;
int N,Q,D[MAX];
pii A[MAX];
arr C[MAX];
int ans[MAX];
int main() {
fio();
cin>>N>>Q;
vector<int> Y;
Y.pb(0);
for (int i=1; i<=N; i++) {
int x,y; cin>>x>>y;
A[i]={x,y};
Y.pb(y);
}
for (int i=1; i<=Q; i++) {
int x,y; cin>>x>>y;
C[i]={x,y,i};
Y.pb(y);
}
sort(A+1,A+N+1,[](pii a, pii b){
if (a.F==b.F) return a.S>b.S;
return a.F<b.F;
});
sort(C+1,C+Q+1,[](auto a, auto b){
if (a[0]==b[0]) return a[1]>b[1];
return a[0]<b[0];
});
cmprs(Y);
for (int i=1; i<=N; i++) A[i].S=lb(all(Y),A[i].S)-Y.begin()+1;
for (int i=1; i<=Q; i++) C[i][1]=lb(all(Y),C[i][1])-Y.begin()+1;
segtr seg; seg.rst(MAX);
for (int i=N; i>=1; i--) {
D[i]=seg.qry(0,A[i].S)+1;
seg.upd(A[i].S,D[i]);
}
segtr segq; segq.rst(MAX);
for (int i=Q,j=N; i>=1; i--) {
auto [a,b,idx]=C[i];
while (j>=1 && a<=A[j].F) segq.upd(A[j].S,D[j]), j--;
ans[idx]=segq.qry(0,b);
}
for (int i=1; i<=Q; i++) cout<<ans[i]<<endl;
return 0;
}
# | 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... |