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 lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const double EPS = 0.00001;
const int INF = 1e9+500;
const int N = 2505;
const int ALPH = 26;
const int LGN = 25;
const int MOD = 1e9+7;
int n,m,q;
vector<int> que;
vector<int> dp;
int ans[N][N];
struct SegTree {
vector<int> data;
int sz;
SegTree(int sz) {
this->sz = sz;
data.assign(4*(sz + 5), INF);
}
void reset() {
data.assign(4*(sz + 5), INF);
}
void update(int tl, int tr, int v, int ind, int val) {
if(tl == tr) {
data[v] = val;
return;
}
int tm = (tl + tr) >> 1;
if(ind <= tm) {
update(tl, tm, v*2, ind, val);
}
else {
update(tm + 1, tr, v*2+1, ind, val);
}
data[v] = min(data[v*2], data[v*2+1]);
}
void update(int ind, int val) {
update(0, sz, 1, ind, val);
}
int query(int tl, int tr, int v, int l, int r) {
if(tl >= l && tr <= r) {
return data[v];
}
if(tl > r || tr < l) {
return INF;
}
int tm = (tl + tr) >> 1;
return min(query(tl, tm, v*2, l, r), query(tm + 1, tr, v*2+1, l, r));
}
int query(int l, int r) {
return query(0, sz, 1, l, r);
}
};
struct Inter {
int l, r, ind;
Inter(int l, int r, int i) {
this->l = l;
this->r = r;
this->ind = i;
}
Inter() {
l = 0; r = 0; ind = 0;
}
const bool operator<(const Inter &oth) {
return r - l < oth.r - oth.l;
}
void input(int i) {
cin>>l>>r;
ind = i;
}
void print() {
cout<<"INTERVAL\n";
cout<<"l:"<<l<<" r:"<<r<<" ind:"<<ind<<"\n";
}
};
vector<Inter> inter;
SegTree *segl, *segr, *segdp;
int f(int l, int r) {
if(ans[l][r] != -1) return ans[l][r];
ans[l][r] = INF;
ans[l][r] = min(ans[l][r], segdp->query(l,r));
int mxl = segl->query(l,r);
int mxr = -segr->query(l,r);
// cout<<"l:"<<l<<" r:"<<r<<"\n";
// cout<<"segdp:"<<segdp->query(l,r)<<"\n";
// cout<<"mxl:"<<mxl<<" mxr:"<<mxr<<"\n";
if(mxr > r) {
ans[l][r] = min(ans[l][r], f(l, mxr) + 1);
}
if(mxl < l) {
ans[l][r] = min(ans[l][r], f(mxl, r) + 1);
}
return ans[l][r];
}
void prec() {
segdp = new SegTree(n); //min
segl = new SegTree(n); //min
segr = new SegTree(n); //max
dp.assign(n+3, 0);
sort(inter.rbegin(), inter.rend());
for(auto c : inter) {
segl->update(c.ind, c.l);
segr->update(c.ind, -c.r);
}
for(int i = 0; i<=n; i++) {
for(int j = 0; j<=n; j++) {
ans[i][j] = -1;
}
}
ans[1][n] = 0;
for(auto c : inter) {
//c.print();
dp[c.ind] = f(c.l, c.r) + 1;
segdp->update(c.ind, dp[c.ind]);
}
}
inline void solve() {
cin>>n;
inter.assign(n, Inter());
REP(i,n) {
inter[i].input(i + 1);
}
cin>>q;
que.assign(q+3, 0);
REP(i,q) {
cin>>que[i + 1];
}
prec();
for(int i = 1; i<=q; i++) {
auto cur = que[i];
if(dp[cur] >= INF) dp[cur] = -1;
cout<<dp[cur]<<"\n";
}
}
signed main() {
//fastio();
int test = 1;
//cin>>test;
while(test--) {
solve();
}
}
Compilation message (stderr)
passport.cpp: In function 'void solve()':
passport.cpp:9:26: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
9 | #define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)
| ^
passport.cpp:145:5: note: in expansion of macro 'REP'
145 | REP(i,n) {
| ^~~
passport.cpp:9:26: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
9 | #define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)
| ^
passport.cpp:150:5: note: in expansion of macro 'REP'
150 | REP(i,q) {
| ^~~
# | 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... |