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("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define double long double
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
#define fi first
#define se second
#define all(x) x.begin(), x.end()
// mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
// int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}
const int NM = 4e5 + 5;
const int LG = 30 + 5;
const int inf = 1e9;
vector<int> v;
int n, q, sz, up[NM][LG];
struct segtree
{
int dt[4 * NM], lz[4 * NM];
void prop(int x, int l, int r)
{
if (!lz[x])
return;
int m = l + r >> 1;
dt[x << 1] += lz[x] * (m - l + 1);
lz[x << 1] += lz[x];
dt[x << 1 | 1] += lz[x] * (r - m);
lz[x << 1 | 1] += lz[x];
lz[x] = 0;
}
void upd(int l, int r, int v, int x = 1, int lx = 1, int rx = sz)
{
if (l > rx || lx > r)
return;
if (lx >= l && rx <= r)
{
dt[x] += v * (rx - lx + 1);
lz[x] += v;
return;
}
prop(x, lx, rx);
int m = lx + rx >> 1;
upd(l, r, v, x << 1, lx, m);
upd(l, r, v, x << 1 | 1, m + 1, rx);
dt[x] = dt[x << 1] + dt[x << 1 | 1];
}
int get(int l, int r, int x = 1, int lx = 1, int rx = sz)
{
if (l > rx || lx > r)
return 0;
if (lx >= l && rx <= r)
return dt[x];
prop(x, lx, rx);
int m = lx + rx >> 1;
return get(l, r, x << 1, lx, m) + get(l, r, x << 1 | 1, m + 1, rx);
}
}st;
struct range
{
int l, r;
range() : l(0), r(0){}
}a[NM];
int mp(int x)
{
return upper_bound(v.begin(), v.end(), x) - v.begin();
}
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i].l >> a[i].r;
v.push_back(a[i].l);
v.push_back(a[i].r);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
sz = v.size();
for (int i = 1; i <= n; i++)
{
a[i].l = mp(a[i].l);
a[i].r = mp(a[i].r);
}
int r = 0;
for (int i = 1; i <= n; i++)
{
while (r < n && !st.get(a[r + 1].l, a[r + 1].r))
{
r++;
st.upd(a[r].l, a[r].r, 1);
}
up[i][0] = r + 1;
st.upd(a[i].l, a[i].r, -1);
}
up[n + 1][0] = n + 1;
for (int j = 1; (1 << j) <= n + 1; j++)
for (int i = 1; i <= n + 1; i++)
up[i][j] = up[up[i][j - 1]][j - 1];
cin >> q;
while (q--)
{
int l, r;
cin >> l >> r;
int res = 0, cur = l;
for (int i = __lg(n); i >= 0; i--)
if (up[cur][i] <= r)
{
res += (1 << i);
cur = up[cur][i];
}
cout << res + 1 << endl;
}
}
signed main()
{
fastIO
if (fopen("in.txt", "r"))
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
int tc = 1;
// cin >> tc;
while (tc--)
solve();
}
Compilation message (stderr)
Main.cpp: In member function 'void segtree::prop(long long int, long long int, long long int)':
Main.cpp:34:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
34 | int m = l + r >> 1;
| ~~^~~
Main.cpp: In member function 'void segtree::upd(long long int, long long int, long long int, long long int, long long int, long long int)':
Main.cpp:52:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int m = lx + rx >> 1;
| ~~~^~~~
Main.cpp: In member function 'long long int segtree::get(long long int, long long int, long long int, long long int, long long int)':
Main.cpp:64:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | int m = lx + rx >> 1;
| ~~~^~~~
Main.cpp: In function 'int main()':
Main.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | freopen("in.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:134:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | freopen("out.txt", "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... |