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 = 2e5 + 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], T[N << 1];
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;
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) 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));
}
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;
}
cin >> q; for(int i = 1; i <= q; i ++){
int l, r; cin >> l >> r; Q[r].pb({l, i});
}
int ptr = 1;
le[1] = 1;
build(1, 1, n);
update(1, 1, n, L[1], R[1], 1);
for(int i = 2; i <= n; i ++){
while(ptr < i && get(1, 1, n, L[i], R[i]) > 0){
update(1, 1, n, L[ptr], R[ptr], -1);
ptr++;
}
update(1, 1, n, L[i], R[i], 1);
le[i] = ptr;
// cerr << i << " " << le[i] << " o\n";
}
build(1, 1, n);
stack<int> sk;
for(int i = 1; i <= n; i ++){
int tmp = 0;
while(!sk.empty() && sk.top() >= le[i]){
tmp++;
sk.pop();
}
sk.push(le[i]);
update(1, 1, n, 1, le[i] - 1, - tmp + 1);
gan(1, 1, n, le[i], i, 1);
for(auto [r, id] : Q[i]) kq[id] = get(1, 1, n, r, r);
}
for(int i = 1; i <= q; i ++) cout << kq[i] << "\n";
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen(task ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | 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... |