This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC target ("avx2")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(c i) \
{
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef XOX
~debug()
{
cerr << endl;
}
eni(!=) cerr << boolalpha << i;
ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d)
{
ris << "" << d.first << " --> " << d.second << "";
}
sim dor(rge<c> d)
{
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &)
{
ris;
}
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#ifdef XOX
#warning Times may differ!!!
#endif
#define endl '\n'
#define pb emplace_back
#define vt vector
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int nax = 1000 * 1007, mod = 1e9 + 7;
struct Node {
int lo, hi;
Node(int x, int y) : lo(x), hi(y) {}
Node operator|(const Node &he) { return Node(min(lo, he.lo), max(hi, he.hi)); }
void operator|=(const Node &he) { *this = *this | he; }
};
struct Tree {
vt<Node> wek;
int base = 1;
Tree() {}
Tree(int n)
{
while (base < n)
base *= 2;
wek.resize(2 * base, Node(mod, -mod));
}
void pisz(int x, Node y)
{
for (x += base; x; x /= 2)
wek[x] |= y;
}
Node czyt(int a, int b)
{
Node ret(mod, -mod);
for (a += base, b += base; a <= b; a /= 2, b /= 2) {
ret |= wek[a] | wek[b];
a++, b--;
}
return ret;
}
};
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n, k, m;
cin >> n >> k >> m;
vt<vt<int>> add_right(n + 1), sub_right(n + 1), add_left(n + 1), sub_left(n + 1);
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
if (a < b) add_right[a].pb(b), sub_right[min(a + k, b)].pb(b);
else add_left[a].pb(b), sub_left[max(a - k, b)].pb(b);
}
multiset<int> secik;
vt<int> to_left(n + 1), to_right(n + 1);
for (int i = 1; i <= n; i++) {
for (int x : add_right[i])
secik.insert(x);
for (int x : sub_right[i])
secik.erase(secik.find(x));
if (!sz(secik)) to_right[i] = i;
else to_right[i] = *secik.rbegin();
}
for (int i = n; i >= 1; i--) {
for (int x : add_left[i])
secik.insert(x);
for (int x : sub_left[i])
secik.erase(secik.find(x));
if (!sz(secik)) to_left[i] = i;
else to_left[i] = *secik.begin();
}
debug() << imie(to_left);
debug() << imie(to_right);
vt<Tree> seg(18);
for (int i = 0; i <= 17; i++) {
seg[i] = Tree(n + 1);
if (i == 0) {
for (int j = 1; j <= n; j++) {
seg[i].pisz(j, Node(to_left[j], to_right[j]));
debug() << imie(j) << "po " << (1 << i) << " krokach to " << seg[i].czyt(j, j).lo << " " << seg[i].czyt(j, j).hi;
}
continue;
}
for (int j = 1; j <= n; j++) {
auto jego = seg[i - 1].czyt(j, j);
auto rozszerz = seg[i - 1].czyt(jego.lo, jego.hi);
seg[i].pisz(j, rozszerz);
debug() << imie(j) << "po " << (1 << i) << " krokach to " << rozszerz.lo << " " << rozszerz.hi;
}
}
int q;
cin >> q;
for (int qq = 0; qq < q; qq++) {
int s, t;
cin >> s >> t;
int c = 0;
Node ret(s, s);
for (int i = 17; i >= 0; i--) {
Node maybe = seg[i].czyt(ret.lo, ret.hi);
if (!(maybe.lo <= t && t <= maybe.hi)) {
c += (1 << i);
ret = maybe;
}
}
if (c + 1 == (1 << 18)) cout << -1 << endl;
else cout << c + 1 << endl;
}
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |