#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{" << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> void chmax(T& a, const T b) { a=max(a,b); }
template <typename T> void chmin(T& a, const T b) { a=min(a,b); }
const int MAXN = 2505;
int n;
pi A[MAXN];
int M[MAXN];
int lf[MAXN];
int rg[MAXN];
struct minnode {
int s,e,m;
pi val;
minnode *l, *r;
minnode (int _s, int _e) {
s=_s;e=_e;m=(s+e)/2;
if(s!=e){
l=new minnode(s,m);
r=new minnode(m+1,e);
}
}
void set(int x, pi nval) {
if(s==e){
val=nval;
return;
}
if(x>m)r->set(x,nval);
else l->set(x,nval);
val=min(l->val,r->val);
}
pi query(int x, int y) {
if(s==x&&e==y)return val;
if(x>m)return r->query(x,y);
else if(y<=m)return l->query(x,y);
else return min(l->query(x,m), r->query(m+1,y));
}
} *seglf;
struct maxnode {
int s,e,m;
pi val;
maxnode *l, *r;
maxnode (int _s, int _e) {
s=_s;e=_e;m=(s+e)/2;
if(s!=e){
l=new maxnode(s,m);
r=new maxnode(m+1,e);
}
}
void set(int x, pi nval) {
if(s==e){
val=nval;
return;
}
if(x>m)r->set(x,nval);
else l->set(x,nval);
val=max(l->val,r->val);
}
pi query(int x, int y) {
if(s==x&&e==y)return val;
if(x>m)return r->query(x,y);
else if(y<=m)return l->query(x,y);
else return max(l->query(x,m), r->query(m+1,y));
}
} *segrg;
int depthl[MAXN];
int depthr[MAXN];
int memo[MAXN][MAXN];
int q;
struct SparseTable {
vector<vector<int>> ST;
int N,K;
inline int msb(unsigned int x) { return 32-__builtin_clz(x); }
SparseTable(int _N, int a[]){
N=_N;
K=msb(n);
ST.resize(K);
ST[0]=vector<int>(a,a+N);
FOR(k,1,K){
for(int i=0;i+(1<<k)<=N;++i){
ST[k].pb(min(ST[k-1][i], ST[k-1][i+(1<<(k-1))]));
}
}
}
int query(int x, int y){
int k=msb(y-x+1)-1;
return min(ST[k][x],ST[k][y-(1<<k)+1]);
}
} *mostL,*mostR,*bestM;
int dpdp(int l, int r){
if(~memo[l][r])return memo[l][r];
db2(l,r);
if(l==1&&r==n)return 0;
int &ans=memo[l][r];
ans=oo;
ans=min({
1+dpdp(mostL->query(l,r),r),
1+dpdp(l,-mostR->query(l,r)),
1+bestM->query(l,r)
});
return ans;
}
int LL[MAXN], RR[MAXN];
int32_t main()
{
IAMSPEED
cin >> n;
FOR(i,1,n)cin>>A[i].f>>A[i].s;
FOR(i,1,n)LL[i]=A[i].f;
FOR(i,1,n)RR[i]=-A[i].s;
mostL=new SparseTable(n+1,LL);
mostR=new SparseTable(n+1,RR);
seglf=new minnode(0,n);
segrg=new maxnode(0,n);
seglf->set(1,{1,1});
FOR(i,2,n){
if(A[i].f==1){
lf[i]=1;
depthl[i]=1;
}
else {
if(A[i].f>i-1){
depthl[i]=oo;
lf[i]=n+1;
} else{ // L[i]<=i-1
pi res=seglf->query(A[i].f,i-1);
lf[i]=res.s;
depthl[i]=depthl[res.s]+1;
}
}
seglf->set(i,{lf[i],i});
}
segrg->set(n,pi(n,n));
DEC(i,n-1,1){
if(A[i].s==n){
rg[i]=n;
depthr[i]=1;
}
else {
if(i+1>A[i].s){
depthr[i]=oo;
rg[i]=0;
} else{
pi res=segrg->query(i+1,A[i].s);
rg[i]=res.s;
depthr[i]=depthr[res.s]+1;
}
}
segrg->set(i,{rg[i],i});
}
dba(lf,1,n);
dba(rg,1,n);
dba(depthl,1,n);
dba(depthr,1,n);
SparseTable TL=SparseTable(n+1,depthl);
SparseTable TR=SparseTable(n+1,depthr);
FOR(i,1,n){
db3(i,A[i].f,A[i].s);
M[i]=TL.query(A[i].f,A[i].s)+TR.query(A[i].f,A[i].s);
}
bestM=new SparseTable(n+1,M);
cin>>q;
memset(memo,-1,sizeof memo);
while(q--){
int x; cin >> x;
int res=dpdp(x,x);
if(res>=oo)cout<<-1<<'\n';
else cout<<res<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
49364 KB |
Output is correct |
2 |
Correct |
19 ms |
49364 KB |
Output is correct |
3 |
Correct |
19 ms |
49348 KB |
Output is correct |
4 |
Runtime error |
4 ms |
2388 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
49428 KB |
Output is correct |
2 |
Correct |
20 ms |
49476 KB |
Output is correct |
3 |
Correct |
18 ms |
49364 KB |
Output is correct |
4 |
Correct |
20 ms |
49424 KB |
Output is correct |
5 |
Correct |
20 ms |
49420 KB |
Output is correct |
6 |
Correct |
21 ms |
49364 KB |
Output is correct |
7 |
Correct |
20 ms |
49480 KB |
Output is correct |
8 |
Correct |
23 ms |
49396 KB |
Output is correct |
9 |
Correct |
21 ms |
49440 KB |
Output is correct |
10 |
Correct |
19 ms |
49364 KB |
Output is correct |
11 |
Correct |
19 ms |
49612 KB |
Output is correct |
12 |
Correct |
20 ms |
49664 KB |
Output is correct |
13 |
Correct |
20 ms |
49620 KB |
Output is correct |
14 |
Correct |
19 ms |
49612 KB |
Output is correct |
15 |
Correct |
22 ms |
49712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
49428 KB |
Output is correct |
2 |
Correct |
20 ms |
49476 KB |
Output is correct |
3 |
Correct |
18 ms |
49364 KB |
Output is correct |
4 |
Correct |
20 ms |
49424 KB |
Output is correct |
5 |
Correct |
20 ms |
49420 KB |
Output is correct |
6 |
Correct |
21 ms |
49364 KB |
Output is correct |
7 |
Correct |
20 ms |
49480 KB |
Output is correct |
8 |
Correct |
23 ms |
49396 KB |
Output is correct |
9 |
Correct |
21 ms |
49440 KB |
Output is correct |
10 |
Correct |
19 ms |
49364 KB |
Output is correct |
11 |
Correct |
19 ms |
49612 KB |
Output is correct |
12 |
Correct |
20 ms |
49664 KB |
Output is correct |
13 |
Correct |
20 ms |
49620 KB |
Output is correct |
14 |
Correct |
19 ms |
49612 KB |
Output is correct |
15 |
Correct |
22 ms |
49712 KB |
Output is correct |
16 |
Correct |
26 ms |
51284 KB |
Output is correct |
17 |
Correct |
25 ms |
51372 KB |
Output is correct |
18 |
Correct |
22 ms |
51412 KB |
Output is correct |
19 |
Correct |
23 ms |
51284 KB |
Output is correct |
20 |
Correct |
46 ms |
51636 KB |
Output is correct |
21 |
Correct |
27 ms |
51540 KB |
Output is correct |
22 |
Correct |
20 ms |
51412 KB |
Output is correct |
23 |
Correct |
28 ms |
51400 KB |
Output is correct |
24 |
Correct |
22 ms |
51420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
49428 KB |
Output is correct |
2 |
Correct |
20 ms |
49476 KB |
Output is correct |
3 |
Correct |
18 ms |
49364 KB |
Output is correct |
4 |
Correct |
20 ms |
49424 KB |
Output is correct |
5 |
Correct |
20 ms |
49420 KB |
Output is correct |
6 |
Correct |
21 ms |
49364 KB |
Output is correct |
7 |
Correct |
20 ms |
49480 KB |
Output is correct |
8 |
Correct |
23 ms |
49396 KB |
Output is correct |
9 |
Correct |
21 ms |
49440 KB |
Output is correct |
10 |
Correct |
19 ms |
49364 KB |
Output is correct |
11 |
Correct |
19 ms |
49612 KB |
Output is correct |
12 |
Correct |
20 ms |
49664 KB |
Output is correct |
13 |
Correct |
20 ms |
49620 KB |
Output is correct |
14 |
Correct |
19 ms |
49612 KB |
Output is correct |
15 |
Correct |
22 ms |
49712 KB |
Output is correct |
16 |
Correct |
26 ms |
51284 KB |
Output is correct |
17 |
Correct |
25 ms |
51372 KB |
Output is correct |
18 |
Correct |
22 ms |
51412 KB |
Output is correct |
19 |
Correct |
23 ms |
51284 KB |
Output is correct |
20 |
Correct |
46 ms |
51636 KB |
Output is correct |
21 |
Correct |
27 ms |
51540 KB |
Output is correct |
22 |
Correct |
20 ms |
51412 KB |
Output is correct |
23 |
Correct |
28 ms |
51400 KB |
Output is correct |
24 |
Correct |
22 ms |
51420 KB |
Output is correct |
25 |
Correct |
18 ms |
49432 KB |
Output is correct |
26 |
Correct |
19 ms |
49404 KB |
Output is correct |
27 |
Correct |
23 ms |
51336 KB |
Output is correct |
28 |
Correct |
37 ms |
51452 KB |
Output is correct |
29 |
Correct |
68 ms |
51640 KB |
Output is correct |
30 |
Correct |
36 ms |
51540 KB |
Output is correct |
31 |
Correct |
24 ms |
51308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
49364 KB |
Output is correct |
2 |
Correct |
19 ms |
49364 KB |
Output is correct |
3 |
Correct |
19 ms |
49348 KB |
Output is correct |
4 |
Runtime error |
4 ms |
2388 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |