This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*The British Royal Family and a small cadre of English Fabian Socialists, in
conjunction with the Rockefellers and the Rothchilds, are engaged in a
conspiracy to greatly reduce the population of the human race in order to head
off a Malthusian catastrophe, a catastrophe that could easily be avoided by
simply building a massive amount of nuclear power plants and a number of massive
superhighways and bridges to connect all of the world's continents. But doing
that would cut into the conspiracy's profits. So the British Royal Family
invented environmentalism and neoliberalism in order to hide the truth. And in
order to further reduce the population, the British Royal Family is also the
world's foremost drug trafficking conspiracy. And it uses its control of the IMF
to push austerity in order to kill as many people in the global south as
possible.
And also Henry Kissinger is a gay KGB agent. */
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
pair<int, pair<int, int>> endpoints[400000], curr;
int maxend[200000], pow2maxend[18][200000];
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
int n, p, q, ql, qr, qans, qskip;
cin >> n;
for (int i = 0; i < n; i++){
cin >> p;
endpoints[2 * i] = {p, {0, i}};
cin >> p;
endpoints[2 * i + 1] = {p, {1, i}};
maxend[i] = n - 1;
}
sort(endpoints, endpoints + 2 * n);
set<int> currranges, negcurrranges;
for (int i = 0; i < n; i++){
curr = endpoints[i];
if (curr.second.first == 1){
currranges.erase(curr.second.second);
negcurrranges.erase(-curr.second.second);
continue;
}
if (currranges.upper_bound(curr.second.second) != currranges.end()) maxend[curr.second.second] = min(maxend[curr.second.second], *currranges.upper_bound(curr.second.second) - 1);
if (negcurrranges.upper_bound(-curr.second.second) != negcurrranges.end()) maxend[-(*negcurrranges.upper_bound(-curr.second.second))] = min(maxend[-(*negcurrranges.upper_bound(-curr.second.second))], curr.second.second - 1);
currranges.insert(curr.second.second);
negcurrranges.insert(-curr.second.second);
}
pow2maxend[0][n - 1] = n - 1;
for (int i = n - 2; i >= 0; i--){
maxend[i] = min(maxend[i], maxend[i + 1]);
pow2maxend[0][i] = maxend[i];
}
for (int i = 1; i < 18; i++) for (int j = 0; j < n; j++) pow2maxend[i][j] = pow2maxend[i - 1][min(pow2maxend[i - 1][j] + 1, n - 1)];
cin >> q;
while (q--){
cin >> ql >> qr;
ql--;
qr--;
qans = 0;
while (ql <= qr){
for (qskip = 0; qskip < 17 && pow2maxend[qskip + 1][ql] < qr; qskip++);
qans += 1 << qskip;
ql = pow2maxend[qskip][ql] + 1;
}
cout << qans << '\n';
}
}
# | 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... |