This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()
using namespace std;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 1e6 + 5;
int block = 555;
//const ll oo = 5e18;
int n, L[N], R[N], q, kq[N], le[N];
vector<pii> Q[N];
int st[N << 2], lz[N << 2], g[N << 2];
void build(int i,int l,int r){
if(l == r){
st[i] = 0;
g[i] = -1;
lz[i] = 0;
return;
}
int mid = (l + r) / 2;
build(i << 1, l, mid);
build(i << 1|1, mid + 1, r);
st[i] = lz[i] = 0, g[i] = -1;
}
void dolz(int i){
if(g[i] != -1){
g[i << 1] = g[i << 1|1] = st[i << 1] = st[i << 1|1] = g[i];
lz[i << 1] = lz[i << 1|1] = 0;
g[i] = -1;
}
if(lz[i]){
st[i << 1] += lz[i];
if(g[i << 1] != -1) g[i << 1] += lz[i];
else lz[i << 1] += lz[i];
st[i << 1|1] += lz[i];
if(g[i << 1|1] != -1) g[i << 1|1] += lz[i];
else lz[i << 1|1] += lz[i];
lz[i] = 0;
}
}
void update(int i,int l,int r,int u, int v,int x){
if(l > r || l > v || r < u) return;
if(u <= l && r <= v){
st[i] += x;
// cerr << l << " " << r << " " << i << " " << st[i] << " " << x << " d\n";
if(g[i] != -1) g[i] += x;
else lz[i] += x;
return;
}
int mid = (l + r) / 2;
dolz(i);
update(i << 1, l, mid, u, v, x);
update(i << 1|1, mid + 1, r, u, v, x);
st[i] = max(st[i << 1], st[i << 1|1]);
}
void gan(int i,int l,int r,int u,int v,int x){
if(l > r || l > v || r < u) return;
if(u <= l && r <= v){
g[i] = x;
lz[i] = 0;
st[i] = x;
return;
}
int mid = (l + r) / 2;
dolz(i);
gan(i << 1, l, mid, u, v, x);
gan(i << 1|1, mid + 1, r, u, v, x);
st[i] = max(st[i << 1], st[i << 1|1]);
}
int get(int i,int l,int r,int u,int v){
if(l > r || l > v || r < u) return 0;
if(u <= l && r <= v){
// cerr << i << " " << st[i] << " " << u << " " << v << "\n";
return st[i];
}
int mid = (l + r) / 2;
dolz(i);
return max(get(i << 1, l, mid, u, v), get(i << 1|1, mid + 1, r, u, v));
}
int f[N][22], d[N];
vector<int> p[N];
void dfs(int u,int v){
if(v == 0) for(int i = 0; i <= 20; i ++) f[u][i] = u;
else{
f[u][0] = v;
for(int i = 1; i <= 20; i ++) f[u][i] = f[f[u][i - 1]][i - 1];
}
for(auto j : p[u]){
d[j] = d[u] + 1;
dfs(j, u);
}
}
int fin(int l,int r){
int kq = r, u = r;
for(int i = 20; i >= 0; i --){
if(f[u][i] >= l){
kq = f[u][i];
u = f[u][i];
}
}
return d[r] - d[kq] + 1;
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> n;
vector<int> rrh;
for(int i = 1; i <= n; i ++){
cin >> L[i] >> R[i];
rrh.pb(L[i]);
rrh.pb(R[i]);
}
sort(all(rrh)); rrh.resize(unique(all(rrh)) - rrh.begin());
for(int i = 1; i <= n; i ++){
L[i] = lower_bound(all(rrh), L[i]) - rrh.begin() + 1;
R[i] = lower_bound(all(rrh), R[i]) - rrh.begin() + 1;
}
int m = (int)rrh.size();
int ptr = 1;
le[1] = 1;
p[le[1] - 1].pb(1);
build(1, 1, m);
update(1, 1, m, L[1], R[1], 1);
for(int i = 2; i <= n; i ++){
while(ptr < i && get(1, 1, m, L[i], R[i]) > 0){
update(1, 1, m, L[ptr], R[ptr], -1);
ptr++;
}
update(1, 1, m, L[i], R[i], 1);
le[i] = ptr;
p[le[i] - 1].pb(i);
// cerr << i << " " << le[i] - 1 << " t\n";
}
dfs(0, 0);
cin >> q; for(int i = 1; i <= q; i ++){
int l, r; cin >> l >> r; cout << fin(l, r) << "\n";
}
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
125 | freopen(task ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:126:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | freopen(task ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |