This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
template<class T>
int len(T &a){
return a.size();
}
using ll = long long;
using pii = pair<int,int>;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
string fileio = "";
mt19937 mt(time(nullptr));
const int mod = 1e9 + 7;
const int inf = 1e9;
const ll infl = 1e18;
const int maxn = 1e5 + 1;
struct Node{
int l, r, i;
};
struct Sparsemin{
vector<vector<Node>> t;
int n, logn;
void init(int _n){
n = _n;
logn = 32 - __builtin_clz(n);
t.assign(logn, vector<Node> (n));
}
Node merge(Node a, Node b){
if(a.l < b.l) return a;
if(b.l < a.l) return b;
if(a.r > b.r) return a;
if(b.r > a.r) return b;
if(a.i < b.i) return a;
return b;
}
void build(vector<int> &l, vector<int> &r){
init(len(l));
for(int i = 0; i < n; i ++){
t[0][i] = {l[i], r[i], i};
}
for(int j = 1; j < logn; j ++){
for(int i = 0; i + (1 << j) - 1 < n; i ++){
t[j][i] = merge(t[j - 1][i], t[j - 1][i + (1 << (j - 1))]);
}
}
}
int get(int l, int r){
int k = 31 - __builtin_clz(r - l + 1);
return merge(t[k][l], t[k][l + (1 << k)]).i;
}
};
struct Sparsemax{
vector<vector<Node>> t;
int n, logn;
void init(int _n){
n = _n;
logn = 32 - __builtin_clz(n);
t.assign(logn, vector<Node> (n));
}
Node merge(Node a, Node b){
if(a.r > b.r) return a;
if(b.r > a.r) return b;
if(a.l < b.l) return a;
if(b.l < a.l) return b;
if(a.i > b.i) return a;
return b;
}
void build(vector<int> &l, vector<int> &r){
init(len(l));
for(int i = 0; i < n; i ++){
t[0][i] = {l[i], r[i], i};
}
for(int j = 1; j < logn; j ++){
for(int i = 0; i + (1 << j) - 1 < n; i ++){
t[j][i] = merge(t[j - 1][i], t[j - 1][i + (1 << (j - 1))]);
}
}
}
int get(int l, int r){
int k = 31 - __builtin_clz(r - l + 1);
//cout << l << ' ' << r << ' ' << k << endl;
return merge(t[k][l], t[k][l + (1 << k)]).i;
}
};
void solve(){
int n; cin >> n;
vector<int> l(n), r(n);
for(int i = 0; i < n; i ++){
cin >> l[i] >> r[i];
l[i] --; r[i] --;
}
Sparsemax tmx;
tmx.build(l, r);
int q; cin >> q;
while(q --){
int x; cin >> x;
int cnt = 1; x--;
while(r[x] != n - 1){
int nx = tmx.get(l[x], r[x]);
if(r[nx] == r[x]){
cnt = -1; break;
}
x = nx;
cnt ++;
}
cout << cnt << '\n';
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
auto start = chrono::high_resolution_clock::now();
#ifndef LOCAL
if(fileio.size()){
freopen((fileio + ".in").c_str(), "r", stdin);
freopen((fileio + ".out").c_str(), "w", stdout);
}
#endif
int testcases = 1;
#ifdef Tests
cin >> testcases;
#endif
while(testcases --){
solve();
cout << '\n';
#ifdef LOCAL
cout << "_________________________" << endl;
#endif
}
#ifdef LOCAL
auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
cout << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
#endif
return 0;
}
Compilation message (stderr)
misspelling.cpp: In function 'int main()':
misspelling.cpp:146:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
146 | auto start = chrono::high_resolution_clock::now();
| ^~~~~
misspelling.cpp:149:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
149 | freopen((fileio + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
misspelling.cpp:150:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
150 | freopen((fileio + ".out").c_str(), "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... |