//
#ifndef __SIZEOF_INT128__
#define __SIZEOF_INT128__
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define rep(i, p, k) for(int i(p); i < (k); ++i)
#define per(i, p, k) for(int i(p); i > (k); --i)
#define sz(x) (int)(x).size()
#define sc static_cast
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef __int128_t lll;
//#define int ll
template <typename T = int> using par = std::pair <T, T>;
#define fi first
#define se second
#define test int _number_of_tests(in()); while(_number_of_tests--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb emplace_back
struct Timer {
string name{""};
time_point<high_resolution_clock> end, start{high_resolution_clock::now()};
duration<float, std::milli> dur;
Timer() = default;
Timer(string nm): name(nm) {}
~Timer() {
end = high_resolution_clock::now(); dur= end - start;
cout << "@" << name << "> " << dur.count() << " ms" << '\n';
}
};
template <typename T = int> inline T in()
{
static T x;
std::cin >> x;
return x;
}
std::string yn(bool b)
{
if(b) return "YES\n";
else return "NO\n";
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par);
template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector <T>& wek)
{
for(const auto& i : wek)out << i << ' ';
return out;
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par)
{
out << '{'<<par.first<<", "<<par.second<<"}";
return out;
}
#define show(x) cerr << #x << " = " << x << '\n';
constexpr int pol = 1<<10, grafs = pol*2, poc = pol*2-1, kon = pol*2-2, cel = pol*2-3;
vector <pair <int, int>> gr[grafs], agr[grafs];
void kra(int a, int b, int c){
// cerr << a << " -> " << b << '\n';
gr[a].pb(b, c);
agr[b].pb(a, c);
}
int odl[10][grafs];
void dijkstra(int o, int g){
priority_queue <par <int>, vector <par<int>>, greater<par<int>>> kul;
rep(i, 0, grafs)odl[g][i] = 1e9;
kul.push(pair{0, o});
while(sz(kul)){
auto [d, w] = kul.top(); kul.pop();
// cerr << d << ' ' << w << '\n';
if(odl[g][w] != 1e9)continue;
odl[g][w] = d;
for(auto [i, j]: agr[w])if(odl[g][i] > d + j)kul.push(pair{d+j, i});
}
}
std::int32_t main()
{
std::cout.tie(nullptr); //for luck
std::cin.tie(nullptr); std::ios_base::sync_with_stdio(0);
int n(in());
vector <int> l(n+1), r(n+1);
rep(i, 1 ,n+1)cin >> l[i] >> r[i];
vector <int> v(n+10);
iota(all(v), pol);
while(v[0] > 1){
for(auto i: v)kra(i/2, i, 0);
vector <int> w;
for(auto i: v)if(!sz(w) || w.back() != i/2)w.pb(i/2);
v = w;
}
rep(i, 1, n+1){
if(l[i] == 1)kra(i+pol, poc, 0);
if(r[i] == n)kra(i+pol, kon, 0);
int a(l[i] + pol-1), b(r[i] + pol+1);
while(a+1 != b){
if(a % 2 == 0)kra(i+pol, a+1, 1);
if(b % 2 == 1)kra(i+pol, b-1, 1);
a /= 2; b /= 2;
}
}
dijkstra(poc, 0);
dijkstra(kon, 1);
rep(i, 1, n+1)kra(i+pol, cel, odl[0][i+pol] +odl[1][i+pol]);
dijkstra(cel, 2);
// rep(i, 1, n+1)cout << odl[0][i+pol] << ' ';
// cout << '\n';
// rep(i, 1, n+1)cout << odl[1][i+pol] << ' ';
// cout << '\n';
// rep(i, 1, n+1)cout << odl[2][i+pol] << ' ';
// cout << '\n';
test{
int c(odl[2][in()+pol]+1);
cout << (c >= int(1e9) ? -1 : c) << '\n';
}
return 0;
}
# | 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... |