#include <bits/stdc++.h>
using namespace std;
#define sp << " " <<
#define int long long
#define vi vector<int>
#define pb push_back
#define F(xxx,yyy) for (int xxx=1;xxx<=yyy;xxx++)
const int N = 2e5+1;
const int MOD = 1e9;
vector<pair<int,int>> rng(N);
struct Node{
pair<int,int> r;
bool v;
Node(pair<int,int> p = {0,0},int val=0) {
r = p;
v = val;
};
};
Node t[4*N];
Node merge(Node& a,Node& b) {
if (!a.v || !b.v) return {{0,0},0};
if (a.r.first >= b.r.first && a.r.first <= b.r.second) return {{0,0},0};
if (a.r.second >= b.r.first && a.r.second <= b.r.second) return {{0,0},0};
if (b.r.first >= a.r.first && b.r.first <= a.r.second) return {{0,0},0};
if (b.r.second >= a.r.first && b.r.second <= a.r.second) return {{0,0},0};
return Node({min(a.r.first,b.r.first),max(a.r.second,b.r.second)},1);
}
void build(int node,int l,int r) {
if (l == r) {
t[node] = Node(rng[l],1);
return;
}
int m = (l+r) >> 1;
build(2*node,l,m);
build(2*node+1,m+1,r);
t[node] = merge(t[2*node],t[2*node+1]);
}
Node query(int node,int l,int r,int L,int R) {
if (l >= L && r <= R) return t[node];
int m = (l+r) >> 1;
if (m >= L && m+1 <= R) {
Node a = query(2*node,l,m,L,R);
Node b = query(2*node+1,m+1,r,L,R);
return merge(a,b);
}
else if (m>=L) return query(2*node,l,m,L,R);
else if (m+1 <= R)return query(2*node+1,m+1,r,L,R);
}
void solve() {
int n;
cin >> n;
for (int i=1;i<=n;i++) cin >> rng[i].first >> rng[i].second;
build(1,1,n);
if (n <= 5000) {
int go[n+1];
for (int i=1;i<=n;i++) {
int l = i;
int r = n;
while (l<=r) {
int m = (l+r) >> 1;
int x = query(1,1,n,i,m).v;
if (x) l = m+1;
else r = m-1;
}
go[i] = r;
}
int dp[n+1][n+1];
for (int i=1;i<=n;i++) dp[i][1] = go[i];
for (int i=2;i<=n;i++) {
for (int j=1;j<=n;j++) {
dp[j][i] = go[min(n,dp[j][i-1]+1)];
}
}
int q;
cin >> q;
while (q--) {
int a,b;
cin >> a >> b;
int l = 1;
int r = n;
while (l<=r) {
int m = (l+r) >> 1;
if (dp[a][m] >= b) r = m-1;
else l = m+1;
}
cout << l << endl;
}
}
else {
;
}
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
Compilation message
Main.cpp: In function 'Node query(long long int, long long int, long long int, long long int, long long int)':
Main.cpp:52:1: warning: control reaches end of non-void function [-Wreturn-type]
52 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
22360 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
330 ms |
212304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
330 ms |
212304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
42 ms |
22364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
22360 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |