//#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];
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
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 |
1 |
Runtime error |
586 ms |
91872 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23644 KB |
Output is correct |
2 |
Correct |
12 ms |
23640 KB |
Output is correct |
3 |
Correct |
12 ms |
23644 KB |
Output is correct |
4 |
Correct |
13 ms |
23644 KB |
Output is correct |
5 |
Correct |
13 ms |
23640 KB |
Output is correct |
6 |
Correct |
7 ms |
21596 KB |
Output is correct |
7 |
Correct |
8 ms |
21596 KB |
Output is correct |
8 |
Correct |
7 ms |
21340 KB |
Output is correct |
9 |
Correct |
10 ms |
21700 KB |
Output is correct |
10 |
Correct |
7 ms |
21336 KB |
Output is correct |
11 |
Correct |
9 ms |
23388 KB |
Output is correct |
12 |
Correct |
9 ms |
23388 KB |
Output is correct |
13 |
Correct |
9 ms |
23388 KB |
Output is correct |
14 |
Correct |
10 ms |
23388 KB |
Output is correct |
15 |
Correct |
10 ms |
23424 KB |
Output is correct |
16 |
Correct |
4 ms |
21084 KB |
Output is correct |
17 |
Correct |
7 ms |
21596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23644 KB |
Output is correct |
2 |
Correct |
12 ms |
23640 KB |
Output is correct |
3 |
Correct |
12 ms |
23644 KB |
Output is correct |
4 |
Correct |
13 ms |
23644 KB |
Output is correct |
5 |
Correct |
13 ms |
23640 KB |
Output is correct |
6 |
Correct |
7 ms |
21596 KB |
Output is correct |
7 |
Correct |
8 ms |
21596 KB |
Output is correct |
8 |
Correct |
7 ms |
21340 KB |
Output is correct |
9 |
Correct |
10 ms |
21700 KB |
Output is correct |
10 |
Correct |
7 ms |
21336 KB |
Output is correct |
11 |
Correct |
9 ms |
23388 KB |
Output is correct |
12 |
Correct |
9 ms |
23388 KB |
Output is correct |
13 |
Correct |
9 ms |
23388 KB |
Output is correct |
14 |
Correct |
10 ms |
23388 KB |
Output is correct |
15 |
Correct |
10 ms |
23424 KB |
Output is correct |
16 |
Correct |
4 ms |
21084 KB |
Output is correct |
17 |
Correct |
7 ms |
21596 KB |
Output is correct |
18 |
Correct |
71 ms |
26276 KB |
Output is correct |
19 |
Correct |
68 ms |
25972 KB |
Output is correct |
20 |
Correct |
74 ms |
26200 KB |
Output is correct |
21 |
Correct |
67 ms |
26024 KB |
Output is correct |
22 |
Correct |
70 ms |
26104 KB |
Output is correct |
23 |
Correct |
61 ms |
24012 KB |
Output is correct |
24 |
Correct |
78 ms |
24144 KB |
Output is correct |
25 |
Correct |
66 ms |
23940 KB |
Output is correct |
26 |
Correct |
62 ms |
23948 KB |
Output is correct |
27 |
Correct |
60 ms |
23680 KB |
Output is correct |
28 |
Correct |
54 ms |
25440 KB |
Output is correct |
29 |
Correct |
57 ms |
25424 KB |
Output is correct |
30 |
Correct |
58 ms |
25428 KB |
Output is correct |
31 |
Correct |
65 ms |
25384 KB |
Output is correct |
32 |
Correct |
59 ms |
25284 KB |
Output is correct |
33 |
Correct |
4 ms |
21084 KB |
Output is correct |
34 |
Correct |
55 ms |
24148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
567 ms |
95232 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
586 ms |
91872 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |