QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#75891 | #5458. Shortest Path Query | maspy | AC ✓ | 440ms | 21696kb | C++20 | 22.2kb | 2023-02-06 16:04:52 | 2023-02-06 16:04:54 |
Judging History
answer
#line 1 "library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'000'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
#define vv(type, name, h, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) \
vector<vector<vector<type>>> name( \
h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...) \
vector<vector<vector<vector<type>>>> name( \
a, vector<vector<vector<type>>>( \
b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)
#define FOR_subset(t, s) \
for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define stoi stoll
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
template <typename T, typename U>
T ceil(T x, U y) {
return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U>
T floor(T x, U y) {
return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T, typename U>
pair<T, T> divmod(T x, U y) {
T q = floor(x, y);
return {q, x - q * y};
}
template <typename T, typename U>
T SUM(const vector<U> &A) {
T sum = 0;
for (auto &&a: A) sum += a;
return sum;
}
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()
template <typename T>
T POP(deque<T> &que) {
T a = que.front();
que.pop_front();
return a;
}
template <typename T>
T POP(pq<T> &que) {
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(pqg<T> &que) {
assert(!que.empty());
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(vc<T> &que) {
assert(!que.empty());
T a = que.back();
que.pop_back();
return a;
}
template <typename F>
ll binary_search(F check, ll ok, ll ng) {
assert(check(ok));
while (abs(ok - ng) > 1) {
auto x = (ng + ok) / 2;
tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
}
return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
FOR(iter) {
double x = (ok + ng) / 2;
tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
}
return (ok + ng) / 2;
}
template <class T, class S>
inline bool chmax(T &a, const S &b) {
return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
return (a > b ? a = b, 1 : 0);
}
// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
vc<int> A(S.size());
FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
return A;
}
template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
int N = A.size();
vector<T> B(N + 1);
FOR(i, N) { B[i + 1] = B[i] + A[i]; }
if (off == 0) B.erase(B.begin());
return B;
}
// stable sort
template <typename T>
vector<int> argsort(const vector<T> &A) {
vector<int> ids(len(A));
iota(all(ids), 0);
sort(all(ids),
[&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
return ids;
}
// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
vc<T> B(len(I));
FOR(i, len(I)) B[i] = A[I[i]];
return B;
}
#endif
#line 1 "library/other/io.hpp"
// based on yosupo's fastio
#include <unistd.h>
namespace fastio {
#define FASTIO
// クラスが read(), print() を持っているかを判定するメタ関数
struct has_write_impl {
template <class T>
static auto check(T &&x) -> decltype(x.write(), std::true_type{});
template <class T>
static auto check(...) -> std::false_type;
};
template <class T>
class has_write : public decltype(has_write_impl::check<T>(std::declval<T>())) {
};
struct has_read_impl {
template <class T>
static auto check(T &&x) -> decltype(x.read(), std::true_type{});
template <class T>
static auto check(...) -> std::false_type;
};
template <class T>
class has_read : public decltype(has_read_impl::check<T>(std::declval<T>())) {};
struct Scanner {
FILE *fp;
char line[(1 << 15) + 1];
size_t st = 0, ed = 0;
void reread() {
memmove(line, line + st, ed - st);
ed -= st;
st = 0;
ed += fread(line + ed, 1, (1 << 15) - ed, fp);
line[ed] = '\0';
}
bool succ() {
while (true) {
if (st == ed) {
reread();
if (st == ed) return false;
}
while (st != ed && isspace(line[st])) st++;
if (st != ed) break;
}
if (ed - st <= 50) {
bool sep = false;
for (size_t i = st; i < ed; i++) {
if (isspace(line[i])) {
sep = true;
break;
}
}
if (!sep) reread();
}
return true;
}
template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
bool read_single(T &ref) {
if (!succ()) return false;
while (true) {
size_t sz = 0;
while (st + sz < ed && !isspace(line[st + sz])) sz++;
ref.append(line + st, sz);
st += sz;
if (!sz || st != ed) break;
reread();
}
return true;
}
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
bool read_single(T &ref) {
if (!succ()) return false;
bool neg = false;
if (line[st] == '-') {
neg = true;
st++;
}
ref = T(0);
while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); }
if (neg) ref = -ref;
return true;
}
template <typename T,
typename enable_if<has_read<T>::value>::type * = nullptr>
inline bool read_single(T &x) {
x.read();
return true;
}
bool read_single(double &ref) {
string s;
if (!read_single(s)) return false;
ref = std::stod(s);
return true;
}
bool read_single(char &ref) {
string s;
if (!read_single(s) || s.size() != 1) return false;
ref = s[0];
return true;
}
template <class T>
bool read_single(vector<T> &ref) {
for (auto &d: ref) {
if (!read_single(d)) return false;
}
return true;
}
template <class T, class U>
bool read_single(pair<T, U> &p) {
return (read_single(p.first) && read_single(p.second));
}
template <size_t N = 0, typename T>
void read_single_tuple(T &t) {
if constexpr (N < std::tuple_size<T>::value) {
auto &x = std::get<N>(t);
read_single(x);
read_single_tuple<N + 1>(t);
}
}
template <class... T>
bool read_single(tuple<T...> &tpl) {
read_single_tuple(tpl);
return true;
}
void read() {}
template <class H, class... T>
void read(H &h, T &... t) {
bool f = read_single(h);
assert(f);
read(t...);
}
Scanner(FILE *fp) : fp(fp) {}
};
struct Printer {
Printer(FILE *_fp) : fp(_fp) {}
~Printer() { flush(); }
static constexpr size_t SIZE = 1 << 15;
FILE *fp;
char line[SIZE], small[50];
size_t pos = 0;
void flush() {
fwrite(line, 1, pos, fp);
pos = 0;
}
void write(const char val) {
if (pos == SIZE) flush();
line[pos++] = val;
}
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
void write(T val) {
if (pos > (1 << 15) - 50) flush();
if (val == 0) {
write('0');
return;
}
if (val < 0) {
write('-');
val = -val; // todo min
}
size_t len = 0;
while (val) {
small[len++] = char(0x30 | (val % 10));
val /= 10;
}
for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; }
pos += len;
}
void write(const string s) {
for (char c: s) write(c);
}
void write(const char *s) {
size_t len = strlen(s);
for (size_t i = 0; i < len; i++) write(s[i]);
}
void write(const double x) {
ostringstream oss;
oss << fixed << setprecision(15) << x;
string s = oss.str();
write(s);
}
void write(const long double x) {
ostringstream oss;
oss << fixed << setprecision(15) << x;
string s = oss.str();
write(s);
}
template <typename T,
typename enable_if<has_write<T>::value>::type * = nullptr>
inline void write(T x) {
x.write();
}
template <class T>
void write(const vector<T> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) write(' ');
write(val[i]);
}
}
template <class T, class U>
void write(const pair<T, U> val) {
write(val.first);
write(' ');
write(val.second);
}
template <size_t N = 0, typename T>
void write_tuple(const T t) {
if constexpr (N < std::tuple_size<T>::value) {
if constexpr (N > 0) { write(' '); }
const auto x = std::get<N>(t);
write(x);
write_tuple<N + 1>(t);
}
}
template <class... T>
bool write(tuple<T...> tpl) {
write_tuple(tpl);
return true;
}
template <class T, size_t S>
void write(const array<T, S> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) write(' ');
write(val[i]);
}
}
void write(i128 val) {
string s;
bool negative = 0;
if (val < 0) {
negative = 1;
val = -val;
}
while (val) {
s += '0' + int(val % 10);
val /= 10;
}
if (negative) s += "-";
reverse(all(s));
if (len(s) == 0) s = "0";
write(s);
}
};
Scanner scanner = Scanner(stdin);
Printer printer = Printer(stdout);
void flush() { printer.flush(); }
void print() { printer.write('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
printer.write(head);
if (sizeof...(Tail)) printer.write(' ');
print(forward<Tail>(tail)...);
}
void read() {}
template <class Head, class... Tail>
void read(Head &head, Tail &... tail) {
scanner.read(head);
read(tail...);
}
} // namespace fastio
using fastio::print;
using fastio::flush;
using fastio::read;
#define INT(...) \
int __VA_ARGS__; \
read(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
read(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
read(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
read(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
read(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
read(name)
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
read(name)
void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
#line 2 "library/graph/base.hpp"
template <typename T>
struct Edge {
int frm, to;
T cost;
int id;
};
template <typename T = int, bool directed = false>
struct Graph {
int N, M;
using cost_type = T;
using edge_type = Edge<T>;
vector<edge_type> edges;
vector<int> indptr;
vector<edge_type> csr_edges;
vc<int> vc_deg, vc_indeg, vc_outdeg;
bool prepared;
class OutgoingEdges {
public:
OutgoingEdges(const Graph* G, int l, int r) : G(G), l(l), r(r) {}
const edge_type* begin() const {
if (l == r) { return 0; }
return &G->csr_edges[l];
}
const edge_type* end() const {
if (l == r) { return 0; }
return &G->csr_edges[r];
}
private:
const Graph* G;
int l, r;
};
bool is_prepared() { return prepared; }
constexpr bool is_directed() { return directed; }
Graph() : N(0), M(0), prepared(0) {}
Graph(int N) : N(N), M(0), prepared(0) {}
void resize(int n) { N = n; }
void add(int frm, int to, T cost = 1, int i = -1) {
assert(!prepared);
assert(0 <= frm && 0 <= to && to < N);
if (i == -1) i = M;
auto e = edge_type({frm, to, cost, i});
edges.eb(e);
++M;
}
// wt, off
void read_tree(bool wt = false, int off = 1) { read_graph(N - 1, wt, off); }
void read_graph(int M, bool wt = false, int off = 1) {
for (int m = 0; m < M; ++m) {
INT(a, b);
a -= off, b -= off;
if (!wt) {
add(a, b);
} else {
T c;
read(c);
add(a, b, c);
}
}
build();
}
void read_parent(int off = 1) {
for (int v = 1; v < N; ++v) {
INT(p);
p -= off;
add(p, v);
}
build();
}
void build() {
assert(!prepared);
prepared = true;
indptr.assign(N + 1, 0);
for (auto&& e: edges) {
indptr[e.frm + 1]++;
if (!directed) indptr[e.to + 1]++;
}
for (int v = 0; v < N; ++v) { indptr[v + 1] += indptr[v]; }
auto counter = indptr;
csr_edges.resize(indptr.back() + 1);
for (auto&& e: edges) {
csr_edges[counter[e.frm]++] = e;
if (!directed)
csr_edges[counter[e.to]++] = edge_type({e.to, e.frm, e.cost, e.id});
}
}
OutgoingEdges operator[](int v) const {
assert(prepared);
return {this, indptr[v], indptr[v + 1]};
}
vc<int> deg_array() {
if (vc_deg.empty()) calc_deg();
return vc_deg;
}
pair<vc<int>, vc<int>> deg_array_inout() {
if (vc_indeg.empty()) calc_deg_inout();
return {vc_indeg, vc_outdeg};
}
int deg(int v) {
if (vc_deg.empty()) calc_deg();
return vc_deg[v];
}
int in_deg(int v) {
if (vc_indeg.empty()) calc_deg_inout();
return vc_indeg[v];
}
int out_deg(int v) {
if (vc_outdeg.empty()) calc_deg_inout();
return vc_outdeg[v];
}
void debug() {
print("Graph");
if (!prepared) {
print("frm to cost id");
for (auto&& e: edges) print(e.frm, e.to, e.cost, e.id);
} else {
print("indptr", indptr);
print("frm to cost id");
FOR(v, N) for (auto&& e: (*this)[v]) print(e.frm, e.to, e.cost, e.id);
}
}
private:
void calc_deg() {
assert(vc_deg.empty());
vc_deg.resize(N);
for (auto&& e: edges) vc_deg[e.frm]++, vc_deg[e.to]++;
}
void calc_deg_inout() {
assert(vc_indeg.empty());
vc_indeg.resize(N);
vc_outdeg.resize(N);
for (auto&& e: edges) { vc_indeg[e.to]++, vc_outdeg[e.frm]++; }
}
};
#line 2 "library/geo/base.hpp"
template <typename T>
struct Point {
T x, y;
Point() = default;
template <typename A, typename B>
Point(A x, B y) : x(x), y(y) {}
template <typename A, typename B>
Point(pair<A, B> p) : x(p.fi), y(p.se) {}
Point operator+(Point p) const { return {x + p.x, y + p.y}; }
Point operator-(Point p) const { return {x - p.x, y - p.y}; }
bool operator==(Point p) const { return x == p.x && y == p.y; }
Point operator-() const { return {-x, -y}; }
bool operator<(Point p) const {
if (x != p.x) return x < p.x;
return y < p.y;
}
T dot(Point other) { return x * other.x + y * other.y; }
T det(Point other) { return x * other.y - y * other.x; }
void read() { fastio::read(x), fastio::read(y); }
void write() { fastio::printer.write(pair<T, T>({x, y})); }
};
template <typename T>
int ccw(Point<T> A, Point<T> B, Point<T> C) {
T x = (B - A).det(C - A);
if (x > 0) return 1;
if (x < 0) return -1;
return 0;
}
template <typename REAL, typename T>
REAL dist(Point<T> A, Point<T> B) {
A = A - B;
T p = A.dot(A);
return sqrt(REAL(p));
}
template <typename T>
struct Line {
T a, b, c;
Line(T a, T b, T c) : a(a), b(b), c(c) {}
Line(Point<T> A, Point<T> B) {
a = A.y - B.y;
b = B.x - A.x;
c = A.x * B.y - A.y * B.x;
}
Line(T x1, T y1, T x2, T y2) : Line(Point<T>(x1, y1), Point<T>(x2, y2)) {}
template <typename U>
U eval(Point<U> P) {
return a * P.x + b * P.y + c;
}
template <typename U>
T eval(U x, U y) {
return a * x + b * y + c;
}
bool is_parallel(Line other) { return a * other.b - b * other.a == 0; }
bool is_orthogonal(Line other) { return a * other.a + b * other.b == 0; }
};
template <typename T>
struct Segment {
Point<T> A, B;
Segment(Point<T> A, Point<T> B) : A(A), B(B) {}
Segment(T x1, T y1, T x2, T y2)
: Segment(Point<T>(x1, y1), Point<T>(x2, y2)) {}
template <enable_if_t<is_integral<T>::value, int> = 0>
bool contain(Point<T> C) {
T det = (C - A).det(B - A);
if (det != 0) return 0;
return (C - A).dot(B - A) >= 0 && (C - B).dot(A - B) >= 0;
}
Line<T> to_Line() { return Line(A, B); }
};
template <typename T>
struct Circle {
Point<T> O;
T r;
Circle(Point<T> O, T r) : O(O), r(r) {}
Circle(T x, T y, T r) : O(Point<T>(x, y)), r(r) {}
};
template <typename T>
struct Polygon {
vc<Point<T>> points;
T a;
template <typename A, typename B>
Polygon(vc<pair<A, B>> pairs) {
for (auto&& [a, b]: pairs) points.eb(Point<T>(a, b));
build();
}
Polygon(vc<Point<T>> points) : points(points) { build(); }
int size() { return len(points); }
template <typename REAL>
REAL area() {
return a * 0.5;
}
template <enable_if_t<is_integral<T>::value, int> = 0>
T area_2() {
return a;
}
bool is_convex() {
FOR(j, len(points)) {
int i = (j == 0 ? len(points) - 1 : j - 1);
int k = (j == len(points) - 1 ? 0 : j + 1);
if ((points[j] - points[i]).det(points[k] - points[j]) < 0) return false;
}
return true;
}
private:
void build() {
a = 0;
FOR(i, len(points)) {
int j = (i + 1 == len(points) ? 0 : i + 1);
a += points[i].det(points[j]);
}
if (a < 0) {
a = -a;
reverse(all(points));
}
}
};
#line 2 "library/graph/reverse_graph.hpp"
template <typename T>
Graph<T, 1> reverse_graph(Graph<T, 1>& G) {
assert(G.is_directed());
Graph<T, 1> G1(G.N);
for (auto&& e: G.edges) { G1.add(e.to, e.frm, e.cost, e.id); }
G1.build();
return G1;
}
#line 6 "main.cpp"
using P = Point<ll>;
void solve() {
LL(N, M);
Graph<bool, 1> G(N);
G.read_graph(M, 1);
auto RG = reverse_graph(G);
INT(Q);
vc<pi> AB(Q);
vi ANS(Q, infty<ll>);
vvc<int> query_at(N);
FOR(q, Q) {
LL(a, b, v);
--v;
AB[q] = {a, b};
query_at[v].eb(q);
if (v == 0) ANS[q] = 0;
}
auto merge = [&](vc<P>& A, vc<P>& B) -> vc<P> {
vc<P> res;
auto add = [&](P p) -> void {
while (len(res) >= 2) {
P a = res[len(res) - 2];
P b = res[len(res) - 1];
if (ccw(a, b, p) > 0) break;
res.pop_back();
}
res.eb(p);
};
int a = 0, b = 0;
while (a < len(A) || b < len(B)) {
if (a < len(A) && b < len(B)) {
if (A[a] < B[b])
add(A[a++]);
else
add(B[b++]);
}
elif (a == len(A)) { add(B[b++]); }
else add(A[a++]);
}
return res;
};
ll LIM = 1024;
vvc<P> dp(LIM);
dp[0].eb(0, 0);
FOR(v, 1, N) {
auto& dat = dp[v % LIM];
dat.clear();
for (auto&& e: RG[v]) {
int to = e.to;
int c = e.cost;
vc<P> add = dp[to % LIM];
for (auto&& p: add) {
if (c == 0) p.x++;
if (c == 1) p.y++;
}
dat = merge(dat, add);
}
for (auto&& qid: query_at[v]) {
auto [a, b] = AB[qid];
P q(a, b);
ll i = binary_search(
[&](int i) -> bool {
if (i == 0) return 1;
return dat[i].dot(q) <= dat[i - 1].dot(q);
},
0, len(dat));
ANS[qid] = dat[i].dot(q);
}
}
for (auto&& x: ANS) print(x);
}
signed main() {
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3500kb
input:
4 4 1 2 0 1 3 1 2 4 0 3 4 1 3 3 5 2 3 2 4 2 3 4
output:
3 4 4
result:
ok 3 number(s): "3 4 4"
Test #2:
score: 0
Accepted
time: 43ms
memory: 13176kb
input:
50000 100000 1 2 1 2 3 0 3 4 1 4 5 0 5 6 1 6 7 0 7 8 1 8 9 1 9 10 0 10 11 1 11 12 1 12 13 1 13 14 0 14 15 0 15 16 0 16 17 0 17 18 1 18 19 1 19 20 0 20 21 1 21 22 0 22 23 0 23 24 1 24 25 1 25 26 0 26 27 1 27 28 0 28 29 0 29 30 0 30 31 0 31 32 1 32 33 0 33 34 1 34 35 1 35 36 1 36 37 1 37 38 0 38 39 0 ...
output:
164602050 208733870 228180204 248456409 87574800 16685198 46684062 64713954 46949896 240633535 94777502 83016099 259833741 167838804 214963500 147454419 111021650 80187604 184782450 78138570 86528820 203553394 188095596 202049239 290053220 172790198 168899028 97757186 96431009 266952297 164349486 26...
result:
ok 50000 numbers
Test #3:
score: 0
Accepted
time: 71ms
memory: 14040kb
input:
50000 100000 1 2 1 2 3 0 3 4 1 4 5 0 5 6 0 6 7 1 7 8 0 8 9 0 9 10 1 10 11 1 11 12 1 12 13 1 13 14 0 14 15 1 15 16 1 16 17 1 17 18 0 18 19 0 19 20 1 20 21 1 21 22 1 22 23 0 23 24 0 24 25 1 25 26 1 26 27 0 27 28 0 28 29 1 29 30 0 30 31 1 31 32 1 32 33 1 33 34 0 34 35 1 35 36 1 36 37 1 37 38 0 38 39 0 ...
output:
100196045 31414400 96903432 4429901 131353947 100466556 96621590 116427456 86564241 138309577 111227766 96757449 98894394 113624940 103437724 32090169 118889544 27383865 145395709 52415186 44958306 178247166 101837390 123750212 66411806 29005113 61920301 53700468 83473964 101048973 24035890 82224385...
result:
ok 50000 numbers
Test #4:
score: 0
Accepted
time: 87ms
memory: 14044kb
input:
50000 100000 1 2 1 2 3 1 3 4 1 4 5 1 5 6 1 6 7 1 7 8 0 8 9 0 9 10 0 10 11 1 11 12 0 12 13 0 13 14 1 14 15 0 15 16 1 16 17 0 17 18 0 18 19 1 19 20 0 20 21 0 21 22 0 22 23 0 23 24 1 24 25 1 25 26 0 26 27 1 27 28 1 28 29 0 29 30 0 30 31 1 31 32 1 32 33 1 33 34 1 34 35 1 35 36 1 36 37 1 37 38 0 38 39 1 ...
output:
41086586 22479083 65941117 52313422 27188549 98552837 41170647 18070874 39704907 37300025 33494097 12541197 85953980 97466762 54255520 55975530 82137682 80760412 36734523 80227468 57771407 53423371 35392992 38772417 24348238 129485865 50694526 41529532 35522018 64188507 64483980 20809109 88158268 62...
result:
ok 50000 numbers
Test #5:
score: 0
Accepted
time: 171ms
memory: 17204kb
input:
50000 100000 1 2 0 2 3 0 3 4 1 4 5 1 5 6 1 6 7 1 7 8 1 8 9 0 9 10 1 10 11 1 11 12 1 12 13 0 13 14 1 14 15 0 15 16 1 16 17 0 17 18 0 18 19 0 19 20 0 20 21 0 21 22 0 22 23 0 23 24 1 24 25 1 25 26 1 26 27 1 27 28 1 28 29 0 29 30 1 30 31 1 31 32 0 32 33 0 33 34 1 34 35 0 35 36 0 36 37 0 37 38 0 38 39 0 ...
output:
21363040 19817072 33235630 2642743 12734703 31561956 16868881 12347713 34007395 31539206 21812869 11469295 13583164 35332256 19432281 13050400 27543307 30865175 23535331 10932941 10731700 8935711 32438529 30147704 11201224 15475486 18918233 29097672 1865520 1717197 10847438 17918300 9085519 22377502...
result:
ok 50000 numbers
Test #6:
score: 0
Accepted
time: 219ms
memory: 17284kb
input:
50000 100000 1 2 1 2 3 1 3 4 1 4 5 1 5 6 1 6 7 0 7 8 1 8 9 0 9 10 1 10 11 0 11 12 1 12 13 0 13 14 1 14 15 0 15 16 1 16 17 1 17 18 0 18 19 1 19 20 1 20 21 1 21 22 0 22 23 1 23 24 0 24 25 0 25 26 0 26 27 0 27 28 0 28 29 0 29 30 1 30 31 1 31 32 0 32 33 0 33 34 0 34 35 1 35 36 1 36 37 1 37 38 0 38 39 1 ...
output:
7034164 2604311 9210306 13276159 7323558 11457505 5477798 10888155 4546292 13775110 4723690 3315532 7247352 14850537 8847725 7929292 5197554 2059467 7544756 2500593 3970752 12419699 9568962 4563223 10626816 3277034 12260607 6928168 4303017 5299690 8738156 9317082 9746787 10042419 13632702 8481147 11...
result:
ok 50000 numbers
Test #7:
score: 0
Accepted
time: 340ms
memory: 21696kb
input:
50000 100000 1 2 1 2 3 1 3 4 1 4 5 0 5 6 0 6 7 0 7 8 1 8 9 0 9 10 0 10 11 1 11 12 1 12 13 0 13 14 1 14 15 0 15 16 1 16 17 1 17 18 0 18 19 1 19 20 0 20 21 0 21 22 0 22 23 1 23 24 1 24 25 0 25 26 1 26 27 1 27 28 0 28 29 1 29 30 1 30 31 1 31 32 0 32 33 1 33 34 1 34 35 0 35 36 1 36 37 1 37 38 1 38 39 1 ...
output:
2881748 379663 1968885 883510 1429377 1317566 1691388 425238 1498644 703328 976532 252540 2157673 2046415 2184358 1823525 1052010 1808512 1132815 987060 2350191 1248681 2155738 502600 2363042 1527856 2063953 1042845 914460 1787808 1741740 1992040 730062 1592241 1369515 1084786 1140249 2712241 754218...
result:
ok 50000 numbers
Test #8:
score: 0
Accepted
time: 421ms
memory: 21624kb
input:
50000 100000 1 2 1 2 3 1 3 4 0 4 5 1 5 6 1 6 7 0 7 8 0 8 9 1 9 10 1 10 11 0 11 12 0 12 13 1 13 14 1 14 15 1 15 16 1 16 17 1 17 18 0 18 19 1 19 20 1 20 21 1 21 22 0 22 23 0 23 24 1 24 25 0 25 26 1 26 27 1 27 28 1 28 29 0 29 30 0 30 31 0 31 32 1 32 33 1 33 34 0 34 35 1 35 36 0 36 37 0 37 38 1 38 39 1 ...
output:
196425 220970 672134 128953 248040 374496 332056 388195 69312 404828 506828 470044 440937 427759 304069 412812 560795 698232 549476 191135 57468 173200 255364 763568 250616 668286 251232 71252 152194 430194 671010 10672 671999 197794 308836 393499 324938 191963 182472 234993 495528 453559 86128 9629...
result:
ok 50000 numbers
Test #9:
score: 0
Accepted
time: 440ms
memory: 21616kb
input:
50000 100000 1 2 1 2 3 1 3 4 1 4 5 1 5 6 1 6 7 0 7 8 0 8 9 0 9 10 0 10 11 1 11 12 0 12 13 1 13 14 0 14 15 0 15 16 1 16 17 1 17 18 0 18 19 1 19 20 1 20 21 0 21 22 1 22 23 1 23 24 0 24 25 0 25 26 0 26 27 0 27 28 0 28 29 0 29 30 0 30 31 0 31 32 1 32 33 0 33 34 1 34 35 1 35 36 0 36 37 0 37 38 0 38 39 1 ...
output:
468255 105020 312484 469028 46500 433476 348092 241180 416482 136152 497776 571977 530352 219162 562176 138675 705269 353652 287899 214580 13380 294272 27828 132826 749244 506820 295440 417272 370316 114926 552490 6486 834970 359255 601821 208311 337232 101539 489665 169404 93039 64173 382258 775608...
result:
ok 50000 numbers
Test #10:
score: 0
Accepted
time: 434ms
memory: 21432kb
input:
50000 100000 1 2 0 2 3 0 3 4 0 4 5 0 5 6 1 6 7 1 7 8 0 8 9 1 9 10 1 10 11 1 11 12 0 12 13 1 13 14 0 14 15 0 15 16 1 16 17 1 17 18 1 18 19 0 19 20 0 20 21 0 21 22 1 22 23 0 23 24 1 24 25 0 25 26 0 26 27 0 27 28 0 28 29 0 29 30 0 30 31 0 31 32 0 32 33 0 33 34 1 34 35 1 35 36 0 36 37 0 37 38 0 38 39 1 ...
output:
663010 158597 758130 682380 443574 514836 623881 348012 155945 542531 400539 240444 166313 307926 556860 383720 97095 367865 372270 204195 787005 79165 455970 495416 156456 33165 223166 481715 416854 524138 316025 105263 274806 781025 177352 653420 491795 743770 506808 72480 505734 444805 198372 700...
result:
ok 50000 numbers
Test #11:
score: 0
Accepted
time: 19ms
memory: 13068kb
input:
50000 99998 1 2 0 1 2 1 2 3 0 2 3 1 3 4 0 3 4 1 4 5 0 4 5 1 5 6 0 5 6 1 6 7 0 6 7 1 7 8 0 7 8 1 8 9 0 8 9 1 9 10 0 9 10 1 10 11 0 10 11 1 11 12 0 11 12 1 12 13 0 12 13 1 13 14 0 13 14 1 14 15 0 14 15 1 15 16 0 15 16 1 16 17 0 16 17 1 17 18 0 17 18 1 18 19 0 18 19 1 19 20 0 19 20 1 20 21 0 20 21 1 21...
output:
357392852 286944261 25899482 106697866 266744665 188046239 246595068 282194356 149697006 36449271 55148897 105197896 112647747 361542769 153346933 426991460 799984 17749645 256444871 329993400 275444491 344593108 81998360 305443891 233695326 82148357 92148157 17449651 36449271 34349313 175796484 354...
result:
ok 50000 numbers
Test #12:
score: 0
Accepted
time: 81ms
memory: 18300kb
input:
50000 50299 1 2 0 2 3 0 3 4 0 4 5 0 5 6 0 6 7 0 7 8 0 8 9 0 9 10 0 10 11 0 11 12 0 12 13 0 13 14 0 14 15 0 15 16 0 16 17 0 17 18 0 18 19 0 19 20 0 20 21 0 21 22 0 22 23 0 23 24 0 24 25 0 25 26 0 26 27 0 27 28 0 28 29 0 29 30 0 30 31 0 31 32 0 32 33 0 33 34 0 34 35 0 35 36 0 36 37 0 37 38 0 38 39 0 3...
output:
28885380 45069427 33432760 30206043 5611707 40119773 20814082 10397200 11616787 7910426 49163370 9368174 31732491 43615079 41538187 27076798 41917819 32019460 17871476 14080806 5899035 42800174 14990686 29049896 25022003 9316314 27915136 19878279 43675167 30658188 2990993 2704160 12805154 19507614 5...
result:
ok 50000 numbers