#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
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) {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Runtime error |
194 ms |
75144 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
436 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
3 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
3 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
436 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
3 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
3 ms |
4444 KB |
Output is correct |
16 |
Correct |
8 ms |
23388 KB |
Output is correct |
17 |
Correct |
151 ms |
24860 KB |
Output is correct |
18 |
Correct |
71 ms |
24924 KB |
Output is correct |
19 |
Correct |
10 ms |
24480 KB |
Output is correct |
20 |
Correct |
883 ms |
25432 KB |
Output is correct |
21 |
Correct |
216 ms |
25176 KB |
Output is correct |
22 |
Correct |
5 ms |
24924 KB |
Output is correct |
23 |
Correct |
7 ms |
24920 KB |
Output is correct |
24 |
Correct |
6 ms |
24920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
436 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
3 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
3 ms |
4444 KB |
Output is correct |
16 |
Correct |
8 ms |
23388 KB |
Output is correct |
17 |
Correct |
151 ms |
24860 KB |
Output is correct |
18 |
Correct |
71 ms |
24924 KB |
Output is correct |
19 |
Correct |
10 ms |
24480 KB |
Output is correct |
20 |
Correct |
883 ms |
25432 KB |
Output is correct |
21 |
Correct |
216 ms |
25176 KB |
Output is correct |
22 |
Correct |
5 ms |
24924 KB |
Output is correct |
23 |
Correct |
7 ms |
24920 KB |
Output is correct |
24 |
Correct |
6 ms |
24920 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
8 ms |
24152 KB |
Output is correct |
28 |
Correct |
145 ms |
25180 KB |
Output is correct |
29 |
Correct |
872 ms |
25444 KB |
Output is correct |
30 |
Correct |
216 ms |
25284 KB |
Output is correct |
31 |
Correct |
6 ms |
24152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Runtime error |
194 ms |
75144 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |