QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#708294 | #8574. Swirly Sort | maspy | AC ✓ | 40ms | 10136kb | C++23 | 25.6kb | 2024-11-03 21:12:28 | 2024-11-03 21:12:28 |
Judging History
answer
#line 1 "/home/maspy/compro/library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else
// https://codeforces.com/blog/entry/96344
#pragma GCC optimize("Ofast,unroll-loops")
// いまの CF だとこれ入れると動かない?
// #pragma GCC target("avx2,popcnt")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;
template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'010'000'000;
template <>
constexpr ll infty<ll> = 2'020'000'000'000'000'000;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * 2'000'000'000'000'000'000;
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); }
int popcnt_mod_2(int x) { return __builtin_parity(x); }
int popcnt_mod_2(u32 x) { return __builtin_parity(x); }
int popcnt_mod_2(ll x) { return __builtin_parityll(x); }
int popcnt_mod_2(u64 x) { return __builtin_parityll(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>
T floor(T a, T b) {
return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
T q = floor(x, y);
return {q, x - q * y};
}
template <typename T, typename U>
T SUM(const vector<U> &A) {
T sm = 0;
for (auto &&a: A) sm += a;
return sm;
}
#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) {
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(vc<T> &que) {
T a = que.back();
que.pop_back();
return a;
}
template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
if (check_ok) assert(check(ok));
while (abs(ok - ng) > 1) {
auto x = (ng + ok) / 2;
(check(x) ? ok : ng) = 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;
(check(x) ? ok : ng) = 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;
}
template <typename T, typename... Vectors>
void concat(vc<T> &first, const Vectors &... others) {
vc<T> &res = first;
(res.insert(res.end(), others.begin(), others.end()), ...);
}
#endif
#line 1 "/home/maspy/compro/library/other/io.hpp"
#define FASTIO
#include <unistd.h>
// https://judge.yosupo.jp/submission/21623
namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf
uint32_t pil = 0, pir = 0, por = 0;
struct Pre {
char num[10000][4];
constexpr Pre() : num() {
for (int i = 0; i < 10000; i++) {
int n = i;
for (int j = 3; j >= 0; j--) {
num[i][j] = n % 10 | '0';
n /= 10;
}
}
}
} constexpr pre;
inline void load() {
memcpy(ibuf, ibuf + pil, pir - pil);
pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
pil = 0;
if (pir < SZ) ibuf[pir++] = '\n';
}
inline void flush() {
fwrite(obuf, 1, por, stdout);
por = 0;
}
void rd(char &c) {
do {
if (pil + 1 > pir) load();
c = ibuf[pil++];
} while (isspace(c));
}
void rd(string &x) {
x.clear();
char c;
do {
if (pil + 1 > pir) load();
c = ibuf[pil++];
} while (isspace(c));
do {
x += c;
if (pil == pir) load();
c = ibuf[pil++];
} while (!isspace(c));
}
template <typename T>
void rd_real(T &x) {
string s;
rd(s);
x = stod(s);
}
template <typename T>
void rd_integer(T &x) {
if (pil + 100 > pir) load();
char c;
do
c = ibuf[pil++];
while (c < '-');
bool minus = 0;
if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
if (c == '-') { minus = 1, c = ibuf[pil++]; }
}
x = 0;
while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; }
if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
if (minus) x = -x;
}
}
void rd(int &x) { rd_integer(x); }
void rd(ll &x) { rd_integer(x); }
void rd(i128 &x) { rd_integer(x); }
void rd(u32 &x) { rd_integer(x); }
void rd(u64 &x) { rd_integer(x); }
void rd(u128 &x) { rd_integer(x); }
void rd(double &x) { rd_real(x); }
void rd(long double &x) { rd_real(x); }
void rd(f128 &x) { rd_real(x); }
template <class T, class U>
void rd(pair<T, U> &p) {
return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T>
void rd_tuple(T &t) {
if constexpr (N < std::tuple_size<T>::value) {
auto &x = std::get<N>(t);
rd(x);
rd_tuple<N + 1>(t);
}
}
template <class... T>
void rd(tuple<T...> &tpl) {
rd_tuple(tpl);
}
template <size_t N = 0, typename T>
void rd(array<T, N> &x) {
for (auto &d: x) rd(d);
}
template <class T>
void rd(vc<T> &x) {
for (auto &d: x) rd(d);
}
void read() {}
template <class H, class... T>
void read(H &h, T &... t) {
rd(h), read(t...);
}
void wt(const char c) {
if (por == SZ) flush();
obuf[por++] = c;
}
void wt(const string s) {
for (char c: s) wt(c);
}
void wt(const char *s) {
size_t len = strlen(s);
for (size_t i = 0; i < len; i++) wt(s[i]);
}
template <typename T>
void wt_integer(T x) {
if (por > SZ - 100) flush();
if (x < 0) { obuf[por++] = '-', x = -x; }
int outi;
for (outi = 96; x >= 10000; outi -= 4) {
memcpy(out + outi, pre.num[x % 10000], 4);
x /= 10000;
}
if (x >= 1000) {
memcpy(obuf + por, pre.num[x], 4);
por += 4;
} else if (x >= 100) {
memcpy(obuf + por, pre.num[x] + 1, 3);
por += 3;
} else if (x >= 10) {
int q = (x * 103) >> 10;
obuf[por] = q | '0';
obuf[por + 1] = (x - q * 10) | '0';
por += 2;
} else
obuf[por++] = x | '0';
memcpy(obuf + por, out + outi + 4, 96 - outi);
por += 96 - outi;
}
template <typename T>
void wt_real(T x) {
ostringstream oss;
oss << fixed << setprecision(15) << double(x);
string s = oss.str();
wt(s);
}
void wt(int x) { wt_integer(x); }
void wt(ll x) { wt_integer(x); }
void wt(i128 x) { wt_integer(x); }
void wt(u32 x) { wt_integer(x); }
void wt(u64 x) { wt_integer(x); }
void wt(u128 x) { wt_integer(x); }
void wt(double x) { wt_real(x); }
void wt(long double x) { wt_real(x); }
void wt(f128 x) { wt_real(x); }
template <class T, class U>
void wt(const pair<T, U> val) {
wt(val.first);
wt(' ');
wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
if constexpr (N < std::tuple_size<T>::value) {
if constexpr (N > 0) { wt(' '); }
const auto x = std::get<N>(t);
wt(x);
wt_tuple<N + 1>(t);
}
}
template <class... T>
void wt(tuple<T...> tpl) {
wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt(' ');
wt(val[i]);
}
}
template <class T>
void wt(const vector<T> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt(' ');
wt(val[i]);
}
}
void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
wt(head);
if (sizeof...(Tail)) wt(' ');
print(forward<Tail>(tail)...);
}
// gcc expansion. called automaticall after main.
void __attribute__((destructor)) _d() { flush(); }
} // namespace fastio
using fastio::read;
using fastio::print;
using fastio::flush;
#if defined(LOCAL)
#define SHOW(...) SHOW_IMPL(__VA_ARGS__, SHOW6, SHOW5, SHOW4, SHOW3, SHOW2, SHOW1)(__VA_ARGS__)
#define SHOW_IMPL(_1, _2, _3, _4, _5, _6, NAME, ...) NAME
#define SHOW1(x) print(#x, "=", (x)), flush()
#define SHOW2(x, y) print(#x, "=", (x), #y, "=", (y)), flush()
#define SHOW3(x, y, z) print(#x, "=", (x), #y, "=", (y), #z, "=", (z)), flush()
#define SHOW4(x, y, z, w) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w)), flush()
#define SHOW5(x, y, z, w, v) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v)), flush()
#define SHOW6(x, y, z, w, v, u) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v), #u, "=", (u)), flush()
#else
#define SHOW(...)
#endif
#define INT(...) \
int __VA_ARGS__; \
read(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
read(__VA_ARGS__)
#define U32(...) \
u32 __VA_ARGS__; \
read(__VA_ARGS__)
#define U64(...) \
u64 __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 3 "main.cpp"
#line 1 "/home/maspy/compro/library/convex/slope_trick/slope_trick_0.hpp"
// 最初に作った最もシンプルなやつを一応残しておく
struct Slope_Trick_0 {
static constexpr ll LMIN = -infty<ll>;
static constexpr ll RMAX = infty<ll>;
pq<ll> que_l;
pqg<ll> que_r;
ll add_l, add_r;
i128 min_f; // infty を足し引きしても壊れないように i128 にする
Slope_Trick_0() : add_l(0), add_r(0), min_f(0) {}
int size() { return len(que_l) + len(que_r); }
tuple<ll, ll, i128> get_min() { return {top_L(), top_R(), min_f}; }
void add_const(ll a) { min_f += a; }
// O(|a| log N)
void add_linear(ll a, ll b) {
min_f += b;
FOR(max<int>(a, 0)) {
ll x = pop_L();
min_f += x;
push_R(x);
}
FOR(max<int>(-a, 0)) {
ll x = pop_R();
min_f -= x;
push_L(x);
}
}
// (a-x)+
void add_a_minus_x(ll a) {
min_f += max<ll>(0, a - top_R());
push_R(a), push_L(pop_R());
}
// (x-a)+
void add_x_minus_a(ll a) {
min_f += max<ll>(0, top_L() - a);
push_L(a), push_R(pop_L());
}
// |x-a|
void add_abs(ll a) { add_a_minus_x(a), add_x_minus_a(a); }
// 増加側を消して、減少側のみにする
void clear_right() { que_r = pqg<ll>(); }
// 減少側を消して、増加側のみにする
void clear_left() { que_l = pq<ll>(); }
void shift(const ll &a) { add_l += a, add_r += a; }
// g(x) = min_{x-b <= y <= x-a} f(y)
void sliding_window_minimum(const ll &a, const ll &b) { add_l += a, add_r += b; }
// O(size log(size))
i128 eval(ll x) {
i128 y = min_f;
pq<ll> que_l_copy = que_l;
pqg<ll> que_r_copy = que_r;
while (len(que_l_copy)) { y += max<ll>(0, (POP(que_l_copy) + add_l) - x); }
while (len(que_r_copy)) { y += max<ll>(0, x - (POP(que_r_copy) + add_r)); }
return y;
}
void push_R(const ll &x) { que_r.emplace(x - add_r); }
void push_L(const ll &x) { que_l.emplace(x - add_l); }
ll top_R() {
if (que_r.empty()) que_r.emplace(RMAX);
return que_r.top() + add_r;
}
ll top_L() {
if (que_l.empty()) que_l.emplace(LMIN);
return que_l.top() + add_l;
}
ll pop_R() {
ll res = top_R();
que_r.pop();
return res;
}
ll pop_L() {
ll res = top_L();
que_l.pop();
return res;
}
#ifdef FASTIO
void debug() {
vi left, right;
pq<ll> que_l_copy = que_l;
pqg<ll> que_r_copy = que_r;
while (len(que_l_copy)) { left.eb(POP(que_l_copy) + add_l); }
while (len(que_r_copy)) { right.eb(POP(que_r_copy) + add_r); }
sort(all(left));
sort(all(right));
print("min_f", min_f, "left", left, "right", right);
}
#endif
};
#line 2 "/home/maspy/compro/library/ds/fenwicktree/fenwicktree_01.hpp"
#line 2 "/home/maspy/compro/library/alg/monoid/add.hpp"
template <typename E>
struct Monoid_Add {
using X = E;
using value_type = X;
static constexpr X op(const X &x, const X &y) noexcept { return x + y; }
static constexpr X inverse(const X &x) noexcept { return -x; }
static constexpr X power(const X &x, ll n) noexcept { return X(n) * x; }
static constexpr X unit() { return X(0); }
static constexpr bool commute = true;
};
#line 3 "/home/maspy/compro/library/ds/fenwicktree/fenwicktree.hpp"
template <typename Monoid>
struct FenwickTree {
using G = Monoid;
using MX = Monoid;
using E = typename G::value_type;
int n;
vector<E> dat;
E total;
FenwickTree() {}
FenwickTree(int n) { build(n); }
template <typename F>
FenwickTree(int n, F f) {
build(n, f);
}
FenwickTree(const vc<E>& v) { build(v); }
void build(int m) {
n = m;
dat.assign(m, G::unit());
total = G::unit();
}
void build(const vc<E>& v) {
build(len(v), [&](int i) -> E { return v[i]; });
}
template <typename F>
void build(int m, F f) {
n = m;
dat.clear();
dat.reserve(n);
total = G::unit();
FOR(i, n) { dat.eb(f(i)); }
for (int i = 1; i <= n; ++i) {
int j = i + (i & -i);
if (j <= n) dat[j - 1] = G::op(dat[i - 1], dat[j - 1]);
}
total = prefix_sum(m);
}
E prod_all() { return total; }
E sum_all() { return total; }
E sum(int k) { return prefix_sum(k); }
E prod(int k) { return prefix_prod(k); }
E prefix_sum(int k) { return prefix_prod(k); }
E prefix_prod(int k) {
chmin(k, n);
E ret = G::unit();
for (; k > 0; k -= k & -k) ret = G::op(ret, dat[k - 1]);
return ret;
}
E sum(int L, int R) { return prod(L, R); }
E prod(int L, int R) {
chmax(L, 0), chmin(R, n);
if (L == 0) return prefix_prod(R);
assert(0 <= L && L <= R && R <= n);
E pos = G::unit(), neg = G::unit();
while (L < R) { pos = G::op(pos, dat[R - 1]), R -= R & -R; }
while (R < L) { neg = G::op(neg, dat[L - 1]), L -= L & -L; }
return G::op(pos, G::inverse(neg));
}
vc<E> get_all() {
vc<E> res(n);
FOR(i, n) res[i] = prod(i, i + 1);
return res;
}
void add(int k, E x) { multiply(k, x); }
void multiply(int k, E x) {
static_assert(G::commute);
total = G::op(total, x);
for (++k; k <= n; k += k & -k) dat[k - 1] = G::op(dat[k - 1], x);
}
void set(int k, E x) { add(k, G::op(G::inverse(prod(k, k + 1)), x)); }
template <class F>
int max_right(const F check, int L = 0) {
assert(check(G::unit()));
E s = G::unit();
int i = L;
// 2^k 進むとダメ
int k = [&]() {
while (1) {
if (i % 2 == 1) { s = G::op(s, G::inverse(dat[i - 1])), i -= 1; }
if (i == 0) { return topbit(n) + 1; }
int k = lowbit(i) - 1;
if (i + (1 << k) > n) return k;
E t = G::op(s, dat[i + (1 << k) - 1]);
if (!check(t)) { return k; }
s = G::op(s, G::inverse(dat[i - 1])), i -= i & -i;
}
}();
while (k) {
--k;
if (i + (1 << k) - 1 < len(dat)) {
E t = G::op(s, dat[i + (1 << k) - 1]);
if (check(t)) { i += (1 << k), s = t; }
}
}
return i;
}
// check(i, x)
template <class F>
int max_right_with_index(const F check, int L = 0) {
assert(check(L, G::unit()));
E s = G::unit();
int i = L;
// 2^k 進むとダメ
int k = [&]() {
while (1) {
if (i % 2 == 1) { s = G::op(s, G::inverse(dat[i - 1])), i -= 1; }
if (i == 0) { return topbit(n) + 1; }
int k = lowbit(i) - 1;
if (i + (1 << k) > n) return k;
E t = G::op(s, dat[i + (1 << k) - 1]);
if (!check(i + (1 << k), t)) { return k; }
s = G::op(s, G::inverse(dat[i - 1])), i -= i & -i;
}
}();
while (k) {
--k;
if (i + (1 << k) - 1 < len(dat)) {
E t = G::op(s, dat[i + (1 << k) - 1]);
if (check(i + (1 << k), t)) { i += (1 << k), s = t; }
}
}
return i;
}
template <class F>
int min_left(const F check, int R) {
assert(check(G::unit()));
E s = G::unit();
int i = R;
// false になるところまで戻る
int k = 0;
while (i > 0 && check(s)) {
s = G::op(s, dat[i - 1]);
k = lowbit(i);
i -= i & -i;
}
if (check(s)) {
assert(i == 0);
return 0;
}
// 2^k 進むと ok になる
// false を維持して進む
while (k) {
--k;
E t = G::op(s, G::inverse(dat[i + (1 << k) - 1]));
if (!check(t)) { i += (1 << k), s = t; }
}
return i + 1;
}
int kth(E k, int L = 0) {
return max_right([&k](E x) -> bool { return x <= k; }, L);
}
};
#line 4 "/home/maspy/compro/library/ds/fenwicktree/fenwicktree_01.hpp"
struct FenwickTree_01 {
int N, n;
vc<u64> dat;
FenwickTree<Monoid_Add<int>> bit;
FenwickTree_01() {}
FenwickTree_01(int n) { build(n); }
template <typename F>
FenwickTree_01(int n, F f) {
build(n, f);
}
void build(int m) {
N = m;
n = ceil<int>(N + 1, 64);
dat.assign(n, u64(0));
bit.build(n);
}
template <typename F>
void build(int m, F f) {
N = m;
n = ceil<int>(N + 1, 64);
dat.assign(n, u64(0));
FOR(i, N) { dat[i / 64] |= u64(f(i)) << (i % 64); }
bit.build(n, [&](int i) -> int { return popcnt(dat[i]); });
}
int sum_all() { return bit.sum_all(); }
int sum(int k) { return prefix_sum(k); }
int prefix_sum(int k) {
int ans = bit.sum(k / 64);
ans += popcnt(dat[k / 64] & ((u64(1) << (k % 64)) - 1));
return ans;
}
int sum(int L, int R) {
if (L == 0) return prefix_sum(R);
int ans = 0;
ans -= popcnt(dat[L / 64] & ((u64(1) << (L % 64)) - 1));
ans += popcnt(dat[R / 64] & ((u64(1) << (R % 64)) - 1));
ans += bit.sum(L / 64, R / 64);
return ans;
}
void add(int k, int x) {
if (x == 1) add(k);
if (x == -1) remove(k);
}
void add(int k) {
dat[k / 64] |= u64(1) << (k % 64);
bit.add(k / 64, 1);
}
void remove(int k) {
dat[k / 64] &= ~(u64(1) << (k % 64));
bit.add(k / 64, -1);
}
int kth(int k, int L = 0) {
if (k >= sum_all()) return N;
k += popcnt(dat[L / 64] & ((u64(1) << (L % 64)) - 1));
L /= 64;
int mid = 0;
auto check = [&](auto e) -> bool {
if (e <= k) chmax(mid, e);
return e <= k;
};
int idx = bit.max_right(check, L);
if (idx == n) return N;
k -= mid;
u64 x = dat[idx];
int p = popcnt(x);
if (p <= k) return N;
k = binary_search([&](int n) -> bool { return (p - popcnt(x >> n)) <= k; },
0, 64, 0);
return 64 * idx + k;
}
int next(int k) {
int idx = k / 64;
k %= 64;
u64 x = dat[idx] & ~((u64(1) << k) - 1);
if (x) return 64 * idx + lowbit(x);
idx = bit.kth(0, idx + 1);
if (idx == n || !dat[idx]) return N;
return 64 * idx + lowbit(dat[idx]);
}
int prev(int k) {
if (k == N) --k;
int idx = k / 64;
k %= 64;
u64 x = dat[idx];
if (k < 63) x &= (u64(1) << (k + 1)) - 1;
if (x) return 64 * idx + topbit(x);
idx = bit.min_left([&](auto e) -> bool { return e <= 0; }, idx) - 1;
if (idx == -1) return -1;
return 64 * idx + topbit(dat[idx]);
}
};
#line 3 "/home/maspy/compro/library/seq/inversion.hpp"
template <typename T>
ll inversion(vc<T> A) {
int N = len(A);
if (A.empty()) return 0;
ll ANS = 0;
FenwickTree_01 bit(N);
auto I = argsort(A);
for (auto& i: I) {
ANS += bit.sum_all() - bit.sum(i);
bit.add(i, 1);
}
return ANS;
}
// i 番目:A_i が先頭になるように rotate したときの転倒数
template <typename T, bool SMALL = false>
vi inversion_rotate(vc<T>& A) {
const int N = len(A);
if (!SMALL) {
auto key = A;
UNIQUE(key);
for (auto&& x: A) x = LB(key, x);
}
ll K = MAX(A) + 1;
ll ANS = 0;
FenwickTree<Monoid_Add<int>> bit(K);
for (auto&& x: A) {
ANS += bit.sum(x + 1, K);
bit.add(x, 1);
}
vi res(N);
FOR(i, N) {
res[i] = ANS;
ll x = A[i];
ANS = ANS + bit.sum(x + 1, K) - bit.prefix_sum(x);
}
return res;
}
// inv[i][j] = inversion A[i:j) であるような (N+1, N+1) テーブル
template <typename T>
vvc<int> all_range_inversion(vc<T>& A) {
int N = len(A);
vv(int, dp, N + 1, N + 1);
FOR_R(L, N + 1) FOR(R, L + 2, N + 1) {
dp[L][R] = dp[L][R - 1] + dp[L + 1][R] - dp[L + 1][R - 1];
if (A[L] > A[R - 1]) ++dp[L][R];
}
return dp;
}
template <typename T>
ll inversion_between(vc<T> A, vc<T> B) {
int N = len(A);
map<T, vc<int>> MP;
FOR(i, N) MP[B[i]].eb(i);
vc<int> TO(N);
FOR_R(i, N) {
auto& I = MP[A[i]];
if (I.empty()) return -1;
TO[i] = POP(I);
}
return inversion(TO);
}
#line 6 "main.cpp"
void solve() {
LL(N, K);
VEC(int, A, N);
auto sub = [&](vc<int> A) -> ll {
Slope_Trick_0 X;
for (auto& x: A) {
X.clear_right();
X.add_abs(x);
}
return X.min_f;
};
if (K == 1) return print(sub(A));
if (K == N) {
ll ANS = infty<ll>;
FOR(N) {
rotate(A.begin(), A.begin() + 1, A.end());
chmin(ANS, sub(A));
}
return print(ANS);
}
if (K % 2 == 0) { return print(0); }
auto I = argsort(A);
A = rearrange(A, I);
if (inversion(I) % 2 == 0) return print(0);
ll ANS = infty<ll>;
FOR(i, N - 1) chmin(ANS, A[i + 1] - A[i]);
print(ANS);
}
signed main() {
INT(T);
FOR(T) solve();
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3580kb
input:
4 4 1 6 4 3 7 4 2 6 4 3 7 4 3 6 4 3 7 4 4 6 4 3 7
output:
3 0 1 2
result:
ok 4 number(s): "3 0 1 2"
Test #2:
score: 0
Accepted
time: 6ms
memory: 3868kb
input:
10000 4 3 524728 254456 277709 19127 15 11 360089 525234 862619 897281 336644 910706 75922 708901 754517 734744 94169 326125 746826 846063 159956 4 2 140105 792522 40264 514789 12 2 270333 888927 500833 9065 936673 982631 332435 751429 607700 840339 804685 416612 8 7 119416 689632 517277 673646 8262...
output:
23253 7691 0 0 15986 278544 0 0 0 0 0 2022 0 0 0 9260 0 0 51255 0 0 277173 480146 0 658 4525 0 0 0 0 0 266148 0 767231 5853 0 0 121885 0 788638 0 0 0 779611 0 5881 0 0 0 0 517074 0 0 0 210836 454586 662851 0 781542 0 0 864957 175421 0 0 0 0 0 0 0 541010 0 0 15407 0 0 3413333 0 0 0 0 19677 30305 3095...
result:
ok 10000 numbers
Test #3:
score: 0
Accepted
time: 7ms
memory: 4048kb
input:
10000 1 1 2246872 10 1 6956989 9799253 5103131 200665 8599026 7743840 6622177 9299599 4771199 2388897 1 1 4115997 2 1 2246219 2946703 1 1 8870887 5 4 4465846 6917492 4431029 8967539 33631 11 11 5721411 592798 6930331 6862401 4082972 2094477 1505423 2091687 9125024 8518457 361077 4 2 8818946 4073615 ...
output:
0 23312638 0 0 0 0 17919297 0 543116 0 0 783241 0 44991 0 0 0 4721212 0 11367749 0 0 421992 0 4267974 0 0 0 0 0 0 0 0 1172214 0 0 0 0 0 9209019 0 0 5365348 922347 463528 10588447 0 0 0 2144103 17190623 19634388 142708 6877382 0 0 0 0 0 0 0 0 0 1477236 0 0 0 0 0 0 820573 0 0 0 3767488 8960620 0 10561...
result:
ok 10000 numbers
Test #4:
score: 0
Accepted
time: 6ms
memory: 3776kb
input:
10000 2 1 45149197 33261068 5 1 55118470 16401191 17389202 89510782 34998353 5 5 63464501 21878886 62995140 27832883 54891420 7 2 85638582 825401 81784814 21532809 30150850 21800436 94882138 2 2 83299527 63718695 3 1 89482904 64518505 91301116 1 1 82256621 1 1 30148988 3 1 68107859 50635233 8682010 ...
output:
11888129 93229708 35162257 0 0 24964399 0 0 59425849 0 22479259 5248308 0 0 0 41327792 0 207654 0 0 0 0 0 0 68210620 0 0 194925 0 0 73467281 33859825 122226 74005621 26201400 0 119179402 0 0 0 0 0 0 0 816171 0 0 0 25910307 3451662 0 4390900 0 0 0 22895459 0 0 0 102364933 0 346465 0 0 0 0 0 58190487 ...
result:
ok 10000 numbers
Test #5:
score: 0
Accepted
time: 6ms
memory: 4132kb
input:
10000 3 2 171561989 326460559 568826834 4 1 606735910 34072129 203284467 873417326 1 1 436249551 3 2 866901680 525830568 142746353 14 11 742357529 676987595 673771185 430647518 327098063 734643932 300528112 859509055 593973771 842011838 467635682 368399037 810057370 576054534 3 2 822197945 247906143...
output:
0 572663781 0 0 3216410 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 696031608 0 559141551 975330575 0 0 0 0 8269118 0 1645243801 0 0 0 0 0 480200189 444013173 1223177234 0 17211999 0 0 0 364069727 178337070 5296068017 0 0 0 0 709411979 0 2420843006 192213554 238004067 0 0 0 0 0 0 0 0 2626223 249160097 1183427809 ...
result:
ok 10000 numbers
Test #6:
score: 0
Accepted
time: 26ms
memory: 4212kb
input:
10000 11 1 719994 371444 177473 165906 258003 388506 556396 344249 756298 954668 668450 37 1 154783 680471 27958 18537 684073 711211 924310 535353 766034 510375 800832 64509 814950 546211 134844 3096 877063 837800 722279 626142 168108 336054 747182 590551 161949 182719 495638 869503 252408 315050 73...
output:
1246424 7880204 1036121 8010082 4745853 18497805 5589959 3845644 68400 265726 6056838 7202823 250222 191468 0 15682644 2627976 25524438 8162775 8408018 23822003 11083830 6961866 3133712 1753363 7922672 9894486 13936678 134033 11202387 3724366 10564706 3652183 3657586 17282350 540513 0 11530473 0 397...
result:
ok 10000 numbers
Test #7:
score: 0
Accepted
time: 29ms
memory: 4232kb
input:
10000 47 1 523059617 328055632 781822408 940589336 455730858 863296197 437030049 658265760 184918189 919959759 734943860 873150203 125605001 626490418 539172080 208944909 697529825 878806549 470244556 457799266 672503197 560798341 691187776 875798951 961299047 680353009 720548746 61867822 924965167 ...
output:
11720848879 20340520024 12486685858 1461710069 6313943138 31665376872 5928212025 1808940644 3787048058 2036744900 1426130451 2146871925 11531760949 4856941583 1425720379 2321337929 1320345528 1327213403 1405424545 8384107070 18369882866 11356718569 0 5267428466 636832484 16157808062 22122796995 6468...
result:
ok 10000 numbers
Test #8:
score: 0
Accepted
time: 40ms
memory: 10136kb
input:
1 300000 1 651102162 912784062 280750844 923640913 5054227 251711260 332945151 660425685 157943264 621946056 560328273 748436147 275105264 126861296 617420574 581173721 857525507 142861415 241368020 281859612 367236952 16104994 157324711 178667188 39607861 614078909 490652942 864292286 305586786 135...
output:
74906657059416
result:
ok 1 number(s): "74906657059416"
Test #9:
score: 0
Accepted
time: 23ms
memory: 4860kb
input:
1 100000 3 602375124 875161080 566363405 109583770 377899457 133319437 709759298 996340891 319888403 539626667 428135630 351615811 924398920 83068878 810553330 758478111 982972289 862464949 529972433 57016896 968624390 364490851 764434932 444421051 683535040 10290976 549700807 263440426 278436542 86...
output:
0
result:
ok 1 number(s): "0"
Test #10:
score: 0
Accepted
time: 10ms
memory: 4000kb
input:
2000 31 3 698810268 462004468 967891238 433071494 190111534 782757983 927040377 733347091 50578740 573583315 417745137 643098900 938483951 301712753 279989662 232161232 536757345 660117956 31789717 511162930 3842769 548739126 343996527 258630727 230356299 750829976 945790410 493372865 997436190 6316...
output:
0 4951 0 0 347770 15452560 5060292 6992 0 2720779 0 0 0 882725 53291879 0 939124 33861516 4405688 0 1781066 186310 28661615 7484769 0 0 28536937 0 1706581 580060 0 13419408 0 1909152 931874 1062952 230576 0 0 0 0 0 0 0 4263895 69366 1426433 0 0 0 0 642345 142664 0 0 2956752 513249 0 0 2993400 0 3895...
result:
ok 2000 numbers
Test #11:
score: 0
Accepted
time: 3ms
memory: 3824kb
input:
1 100000 2 477476356 94013914 625859012 730906024 611051981 241413132 629052354 340768318 842017659 956572518 612192775 286707518 859673844 186614005 442273229 796755030 179975139 893706537 184792656 362272473 138300473 94847574 221835090 267842108 416763558 818622772 70940568 971541146 497308964 29...
output:
0
result:
ok 1 number(s): "0"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3996kb
input:
2000 26 2 924003662 871229099 358864902 330638562 825068362 163072451 54148927 515858219 543157954 724050502 993751520 272486178 839501706 220518782 220448393 36073704 527761327 75640326 325605198 974218035 740338158 430636621 569817145 280599500 428848698 961283863 58 2 822664646 398219282 18343378...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 2000 numbers
Test #13:
score: 0
Accepted
time: 8ms
memory: 4044kb
input:
18555 1 1 1 1 1 2 1 1 3 1 1 4 1 1 5 2 1 1 1 2 2 1 1 2 1 1 2 2 2 1 2 2 1 1 3 2 2 1 3 2 1 1 4 2 2 1 4 2 1 1 5 2 2 1 5 2 1 2 1 2 2 2 1 2 1 2 2 2 2 2 2 2 1 2 3 2 2 2 3 2 1 2 4 2 2 2 4 2 1 2 5 2 2 2 5 2 1 3 1 2 2 3 1 2 1 3 2 2 2 3 2 2 1 3 3 2 2 3 3 2 1 3 4 2 2 3 4 2 1 3 5 2 2 3 5 2 1 4 1 2 2 4 1 2 1 4 2 ...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0 0 3 0 2 0 1 0 0 0 0 0 4 0 3 0 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 1 0 1 0 0 0 0 0 0 0 0 0 3 0 0 2 0 1 1 0 1 0 0 0 0 0 0 4 0 0 3 0 1 2 0 2 1 0 1 0 0 0 1 0 0 1 0 0 1 0 1 1 0 1 1 0 1 1 0 0 0 0 ...
result:
ok 18555 numbers
Test #14:
score: 0
Accepted
time: 18ms
memory: 5020kb
input:
1 100000 3 103670965 730988733 612690321 859510898 495420570 617006158 930838259 781270919 815385999 657404732 838079716 160695659 234318868 13088314 849798902 74279803 560806478 676215272 38150915 858044286 667276814 994119002 698379276 449347578 713488988 862195451 930978455 362968914 387608599 23...
output:
1
result:
ok 1 number(s): "1"
Test #15:
score: 0
Accepted
time: 28ms
memory: 3668kb
input:
1 547 547 887954172 384424822 567917975 563531012 392348181 376515730 505356706 322940528 557433012 539277779 668097503 50062339 993650038 144040651 646365158 970239629 830237517 873351107 148978812 731914101 440991373 825216126 275919951 769751372 819414582 351059 208172908 119263053 998452139 5550...
output:
132549084799
result:
ok 1 number(s): "132549084799"
Test #16:
score: 0
Accepted
time: 27ms
memory: 3768kb
input:
1 546 546 4305572 374272421 88147916 554446056 930778731 238301666 878297114 646261853 194755102 312781870 225149932 872612884 331270197 903800554 944512335 156116735 840475042 491427606 984195 990974885 73381054 192504546 4969187 310585949 951311970 425024295 590896048 818902913 464200784 158688832...
output:
127477085741
result:
ok 1 number(s): "127477085741"
Test #17:
score: 0
Accepted
time: 0ms
memory: 3868kb
input:
100 157 23 846216104 325976483 76823620 279509806 242790881 9676858 792345559 322056553 632488502 444723554 70422962 27172325 26697435 40309279 992120464 751627327 499482549 277429832 736386595 471708200 940778720 417787658 306890639 248789992 1849130 203226467 672298989 938207958 331602581 31374660...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18748 0 0 0 44618 0 0 1734 0 0 0 37551 0 0 0 0 9738 79158 497404 0 0 0 0 0 0 0 0 32019 0 0 123787 10539 178749 0 0 0 86176 0 115850 34488 0 0 0 0 153492 0 0 0 0 0 0 0 1010455 0 363342 60486 0 233210 0 0 0 0 1145801 0 0 93911 0 0 0 0 0 0 0 0 17885 0 0 0 0 0 0 0 0 320335
result:
ok 100 numbers
Test #18:
score: 0
Accepted
time: 2ms
memory: 3852kb
input:
10 1928 24 971407775 628894325 731963982 822804784 450968417 430302156 982631932 161735902 880895728 923078537 707723857 189330739 910286918 802329211 404539679 303238506 317063340 492686568 773361868 125660016 650287940 839296263 462224593 492601449 384836991 191890310 576823355 782177068 404011431...
output:
0 0 1065 0 0 0 75358 0 39 5
result:
ok 10 numbers
Extra Test:
score: 0
Extra Test Passed