QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#394050 | #5517. Adjacent Product Sum | georgeyucjr | AC ✓ | 16ms | 5672kb | C++23 | 28.7kb | 2024-04-19 21:55:48 | 2024-04-19 21:55:48 |
Judging History
answer
/**
* @file A.cpp
* @author georgeyucjr ([email protected])
* @brief
* @version 998244353.1145141919810
* @date 2024-04-19
*
* @copyright Copyright (c) 2024
*
*/
#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#define LOCAL
#endif
#if __cplusplus >= 201100LL
#else
#error Please use C++11 Or Higher CPP version
#endif
using ll = long long;
using ull = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using arr3 = std ::array<int, 3>;
using arr4 = std ::array<int, 4>;
using pii = std ::pair<int, int>;
#if __cplusplus >= 201400LL
#define rep(i, f, t, ...) for (std ::decay<decltype(f + t)>::type i = f, _END_##i = t, ##__VA_ARGS__; i <= _END_##i; ++i)
#define per(i, f, t, ...) for (std ::decay<decltype(f + t)>::type i = f, _END_##i = t, ##__VA_ARGS__; i >= _END_##i; --i)
#else
#define rep(i, f, t, ...) for (decltype(f + t) i = f, _END_##i = t, ##__VA_ARGS__; i <= _END_##i; ++i)
#define per(i, f, t, ...) for (decltype(f + t) i = f, _END_##i = t, ##__VA_ARGS__; i >= _END_##i; --i)
#endif
#define emb emplace_back
#define pb push_back
#define mkp make_pair
#define FILEIO(filename) freopen(filename ".in", "r", stdin), freopen(filename ".out", "w", stdout)
#ifndef INLINE_V
#if __cplusplus >= 201700LL
#define INLINE_V inline
#else
#define INLINE_V
#endif
#endif
#if __cplusplus >= 201400LL
#define EXPR constexpr
#else
#define EXPR
#endif
#if __cplusplus >= 201400LL
template <class T, class U>
inline constexpr auto ceil(T &&x, U &&y)
{
return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T, class U>
inline constexpr auto floor(T &&x, U &&y)
{
return (x > 0 ? x / y : (x - y + 1) / y);
}
template <class T, class U>
inline constexpr auto divmod(T &&x, U &&y)
{
auto &&q = floor(x, y);
return std ::pair{q, x - q * y};
}
inline constexpr auto sqr(const auto &x) { return x * x; }
inline constexpr auto Max(const auto &x) { return x; }
inline constexpr auto Min(const auto &x) { return x; }
inline constexpr auto Max(const auto &x, const auto &y, const auto &...args) { return x < y ? Max(y, args...) : Max(x, args...); }
inline constexpr auto Min(const auto &x, const auto &y, const auto &...args) { return x < y ? Min(x, args...) : Min(y, args...); }
inline constexpr void chkmax(auto &d, const auto &...x) { d = Max(d, x...); }
inline constexpr void chkmin(auto &d, const auto &...x) { d = Min(d, x...); }
#else
template <class T, class U>
inline T ceil(T &&x, U &&y)
{
return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T, class U>
inline T floor(T &&x, U &&y)
{
return (x > 0 ? x / y : (x - y + 1) / y);
}
template <class T, class U>
inline pair<T, T> divmod(T &&x, U &&y)
{
T &&q = floor(x, y);
return std ::make_pair(q, x - q * y);
}
template <class T>
inline T sqr(const T &x) { return x * x; }
template <class T, class... Args>
inline T Max(const T &x, const T &y, const Args &...args)
{
return x < y ? Max(y, args...) : Max(x, args...);
}
template <class T, class... Args>
inline T Min(const T &x, const T &y, const Args &...args)
{
return x > y ? Min(y, args...) : Min(x, args...);
}
template <class T, class... Args>
inline T chkmax(T &d, const Args &...x) { d = Max(d, x...); }
template <class T, class... Args>
inline T chkmin(T &d, const Args &...x) { d = Min(d, x...); }
#endif
#define ALL(vc) std ::begin(vc), std ::end(vc)
#if __cplusplus >= 202000LL
template <class T>
requires(std ::is_signed_v<T> || std ::is_unsigned_v<T>)
INLINE_V constexpr static T inf = std ::numeric_limits<T>::max() >> 1;
template <std ::ranges ::forward_range R>
inline constexpr auto Sum(R &&r)
{
return std ::accumulate(std ::ranges ::begin(r), std ::ranges ::end(r), 0);
}
template <std ::ranges ::forward_range R, typename T>
inline constexpr auto Sum(R &&r, T init)
{
return std ::accumulate(std ::ranges ::begin(r), std ::ranges ::end(r), init);
}
template <std ::ranges ::forward_range R>
inline constexpr int SZ(R &&x) { return std ::size(x); }
#else
template <class R>
inline EXPR auto Sum(R &&r)
{
return std ::accumulate(ALL(r), 0);
}
template <class R, typename T>
inline EXPR auto Sum(R &&r, T init)
{
return std ::accumulate(ALL(r), init);
}
template <class R>
inline EXPR auto Sum(R f, R t)
{
return std ::accumulate(f, t);
}
template <class R, typename T>
inline EXPR auto Sum(R f, R t, T init)
{
return std ::accumulate(f, t, init);
}
template <class T>
inline EXPR int SZ(T &&x) { return static_cast<int>(x.size()); }
template <class T>
INLINE_V constexpr static T inf = std ::numeric_limits<T>::max() >> 1;
#endif
template <class Os, class T>
inline Os &operator<<(Os &os, T &t)
{
for (auto ele : t)
os << ele << " ";
os << "\n";
return os;
}
template <class T>
inline constexpr void clr_cont(T &cont)
{
T nw;
cont.swap(nw);
}
#ifndef __GEORGEYUCJR_READ__
#define __GEORGEYUCJR_READ__
#if __cplusplus >= 202000LL && !defined(MACOS) && defined(__unix__)
#define CHECK_EOF 0
#if CHECK_EOF
#define ECHK0 *ptr ? *ptr++ : -1
#define ECHK1 \
if (ch == -1) \
return set(false), *this;
#define ECHK2 \
if (!*ptr) \
return set(false), *this;
#define ECHK3 and ch != -1
#define ECHK4 and*ptr
#else
#define ECHK0 *ptr++
#define ECHK1
#define ECHK2
#define ECHK3
#define ECHK4
#endif
#if !defined(MACOS)
#define USE_MMAP
#include <sys/mman.h>
#include <sys/stat.h>
#endif
#if __cpluspls >= 202000LL && !defined(MACOS)
#define DISABLE_RANGE
#endif
#if CHECK_EOF || !defined(USE_MMAP)
#define EV -1
#else
#define EV 0
#endif
namespace FastIO
{
template <typename T>
struct make_unsigned : public std ::make_unsigned<T>
{
};
template <>
struct make_unsigned<i128>
{
using type = u128;
};
template <typename T>
concept tupleLike = requires(T t) { std ::tuple_size_v<decltype(std ::tuple_cat(t))>; };
inline constexpr ull pw10(int t) { return t == 0 ? 1 : pw10(t - 1) * 10; }
std ::mt19937 mrand(std ::random_device{}());
std ::mt19937_64 mrand64(std ::random_device{}());
inline constexpr static std ::size_t bufSize = 1 << 20;
class IO;
class In
{
friend class IO;
private:
FILE *inFile;
bool status = true;
#ifdef USE_MMAP
struct stat st;
char *ptr;
int fd;
#elif !defined(LOCAL)
char buf[bufSize], *p1, *p2;
#endif
public:
#ifdef LOCAL
inline int getch() { return fgetc_unlocked(inFile); }
inline void input(FILE *f) { inFile = f, set(); }
#elif defined(USE_MMAP)
inline int getch() { return ECHK0; }
inline void input(FILE *f)
{
inFile = f;
if (inFile)
fd = fileno(inFile), fstat(fd, &st), ptr = (char *)mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0), set();
}
static constexpr auto n = []()
{
std ::array<uint32_t, 0x10000> n{};
for (int i = 0; i != 0x10000; ++i)
n[i] = -1;
constexpr uint32_t e0 = 0x01, e1 = 0x100;
int x = 0;
for (uint32_t i = 0, c0 = 0x3030; i != 10; i++, c0 += e0)
for (uint32_t j = 0, c1 = c0; j != 10; j++, c1 += e1)
n[c1] = x++;
return n;
}();
#else
inline int getch() { return (p1 == p2 ? (p2 = (p1 = buf) + fread(buf, 1, bufSize, inFile)) : 0), p1 == p2 ? -1 : *p1++; }
inline void input(FILE *f) { inFile = f, p1 = p2 = buf, set(); }
#endif
inline void input(const char *s) { input(fopen(s, "rb")); }
inline void set(bool s = true) { status = s; }
inline bool get() const { return status; }
};
class Out
{
friend class IO;
public:
FILE *outFile;
#ifndef LOCAL
char pbuf[bufSize], *pp;
#endif
public:
#ifdef LOCAL
inline void flush() { outFile ? fflush(outFile) : 0; }
inline void putch(char c) { fputc_unlocked(c, outFile); }
#else
inline void flush() { (outFile ? fwrite(pbuf, 1, pp - pbuf, outFile), fflush(outFile) : 0), pp = pbuf; }
inline void putch(char c) { ((pp - pbuf == bufSize) ? fwrite(pbuf, 1, bufSize, outFile), pp = pbuf : 0), *pp++ = c; }
#endif
inline void output(const char *s) { flush(), outFile = fopen(s, "wb"); }
inline void output(FILE *f) { flush(), outFile = f; }
};
class IO : public In, public Out
{
private:
std ::size_t precision = 12;
public:
IO(FILE * = nullptr, FILE * = nullptr);
~IO() { flush(); }
static constexpr bool blank(char);
template <typename... Args>
requires(sizeof...(Args) > 1)
IO &read(Args &...);
template <typename T>
requires(std ::is_signed_v<T> && std ::is_integral_v<T>) || std ::is_same_v<std ::decay_t<T>, i128>
IO &read(T &);
template <typename T>
requires(std ::is_unsigned_v<T> && std ::is_integral_v<T>) || std ::is_same_v<std ::decay_t<T>, u128>
IO &read(T &);
template <typename T>
requires std ::is_floating_point_v<T>
IO &read(T &);
template <typename T>
T read();
IO &read(char &);
IO &read(char *);
IO &read(std ::string &);
IO &readline(char *);
IO &readline(std ::string &);
template <tupleLike T>
IO &read(T &&);
template <std ::ranges::input_range R>
IO &read(R &&r) { return readArray(std ::forward<R>(r)); }
void setprecision(std ::size_t n = 6u) { precision = n; }
template <typename... Args>
requires(sizeof...(Args) > 1)
void write(Args &&...);
inline void write() const {}
template <typename T>
requires(std ::is_signed_v<T> && std ::is_integral_v<T>) || std ::is_same_v<std ::decay_t<T>, i128>
void write(T);
template <typename T>
requires(std ::is_unsigned_v<T> && std ::is_integral_v<T>)
void write(T);
void write(u128);
inline void write(char);
template <typename T>
requires std ::is_floating_point_v<T>
void write(T);
inline void write(bool);
void write(char *);
void write(const char *);
void write(const std ::string &);
template <typename T>
void write(std ::initializer_list<T>);
template <tupleLike T>
void write(T &&);
template <std ::ranges::input_range R>
void write(R &&);
template <typename... Args>
void writeln(Args &&...);
template <typename T>
inline void writespc(T &&x);
operator bool() const { return get(); }
} io(stdin, stdout);
template <typename... Args>
inline void writeln(Args &&...x) { io.writeln(std ::forward<Args>(x)...); }
template <typename T>
inline void writeln(std ::initializer_list<T> x) { io.writeln(x); }
#pragma endregion
#define isdigit(x) ((x) >= '0' && (x) <= '9')
IO::IO(FILE *in, FILE *out) { input(in), output(out); }
constexpr bool IO::blank(char c) { return c == ' ' || c == '\n' || c == '\r'; }
template <typename... Args>
requires(sizeof...(Args) > 1)
IO &IO::read(Args &...args)
{
if constexpr (CHECK_EOF)
(read(args) && ...);
else
(read(args), ...);
return *this;
}
template <typename T>
requires(std ::is_signed_v<T> && std ::is_integral_v<T>) || std ::is_same_v<std ::decay_t<T>, i128>
IO &IO::read(T &x)
{
x = 0;
static typename make_unsigned<T>::type t;
bool sign = false;
#ifndef USE_MMAP
int ch = getch();
while (!isdigit(ch) ECHK3)
sign = (ch == '-'), ch = getch();
ECHK1
t = 0;
while (isdigit(ch))
t = t * 10 + (ch ^ 48), ch = getch();
#else
while (!isdigit(*ptr) ECHK4)
sign = *ptr++ == '-';
ECHK2
t = *ptr++ ^ 48;
while (~n[*reinterpret_cast<std ::uint16_t *&>(ptr)])
t = t * 100 + n[*reinterpret_cast<std ::uint16_t *&>(ptr)++];
if (isdigit(*ptr))
t = t * 10 + (*ptr++ ^ 48);
#endif
x = sign ? (~t + 1) : t;
return *this;
}
template <typename T>
requires(std ::is_unsigned_v<T> && std ::is_integral_v<T>) || std ::is_same_v<std ::decay_t<T>, u128>
IO &IO::read(T &x)
{
x = 0;
#ifndef USE_MMAP
int ch = getch();
while (!isdigit(ch) ECHK3)
ch = getch();
ECHK1
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getch();
#else
while (!isdigit(*ptr) ECHK4)
ptr++;
ECHK2
x = *ptr++ ^ 48;
while (~n[*reinterpret_cast<std ::uint16_t *&>(ptr)])
x = x * 100 + n[*reinterpret_cast<std ::uint16_t *&>(ptr)++];
if (isdigit(*ptr))
x = x * 10 + (*ptr++ ^ 48);
#endif
return *this;
}
template <typename T>
requires std ::is_floating_point_v<T>
IO &IO::read(T &x)
{
x = 0;
T t = 1;
bool sign = false;
int ch = getch();
while (!isdigit(ch) ECHK3)
sign = (ch == '-'), ch = getch();
ECHK1
while (isdigit(ch))
x = x * 10 + (ch ^ 48), ch = getch();
if (ch == '.')
for (ch = getch(); isdigit(ch); ch = getch())
x += (t /= 10) * (ch ^ 48);
x = sign ? -x : x;
return *this;
}
template <typename T>
T IO::read()
{
static std ::decay_t<T> x;
return read(x), x;
}
IO &IO::read(char &ch)
{
do
ch = getch();
while (blank(ch));
ECHK1
return *this;
}
IO &IO::read(char *s)
{
int ch = getch();
while (blank(ch))
ch = getch();
ECHK1
while (!blank(ch) && ch != EV)
*s++ = ch, ch = getch();
*s = 0;
return *this;
}
IO &IO::read(std ::string &s)
{
int ch = getch();
while (blank(ch))
ch = getch();
ECHK1
s.erase();
while (!blank(ch) && ch != EV)
s.append(1, ch), ch = getch();
return *this;
}
IO &IO::readline(char *s)
{
char *t = s;
int ch = getch();
while (ch != '\n' && ch != EV)
*s++ = ch, ch = getch();
*s = 0;
if (s == t && ch != '\n')
set(false);
return *this;
}
IO &IO::readline(std ::string &s)
{
s.erase();
int ch = getch();
while (ch != '\n' && ch != EV)
s.append(1, ch), ch = getch();
if (s.empty() && ch != '\n')
set(false);
return *this;
}
template <tupleLike T>
IO &IO::read(T &&t)
{
std ::apply([&](auto &...t)
{ (read(t), ...); },
t);
return *this;
}
template <typename... Args>
requires(sizeof...(Args) > 1)
void IO::write(Args &&...args)
{
(write(std ::forward<Args>(args)), ...);
}
template <typename T>
requires(std ::is_signed_v<T> && std ::is_integral_v<T>) || std ::is_same_v<std ::decay_t<T>, i128>
void IO::write(T x)
{
static typename make_unsigned<T>::type y;
y = x;
if (x < 0)
putch('-'), write(~y + 1);
else
write(y);
}
constexpr auto D = []()
{
constexpr uint32_t e0 = 0x1, e1 = 0x100, e2 = 0x10000, e3 = 0x1000000;
std ::array<uint32_t, 10000> m{};
int x = 0;
for (uint32_t i = 0, c0 = 0x30303030; i != 10; i++, c0 += e0)
for (uint32_t j = 0, c1 = c0; j != 10; j++, c1 += e1)
for (uint32_t k = 0, c2 = c1; k != 10; k++, c2 += e2)
for (uint32_t l = 0, c3 = c2; l != 10; l++, c3 += e3)
m[x++] = c3;
return m;
}();
template <typename T>
requires(std ::is_unsigned_v<T> && std ::is_integral_v<T>)
void IO::write(T x)
{
#ifndef LOCAL
if (std ::end(pbuf) - pp < 64)
flush();
auto L = [&](int x)
{ return x == 1 ? 0 : pw10(x - 1); };
auto R = [&](int x)
{ return pw10(x) - 1; };
#define de(t) \
case L(t)... R(t): \
*reinterpret_cast<uint32_t *>(pp) = D[x / pw10((t)-4)]; \
pp += 4; \
x %= pw10((t)-4);
ull y = x;
switch (y)
{
de(18);
de(14);
de(10);
de(6);
case L(2)... R(2):
*reinterpret_cast<uint32_t *>(pp) = D[x * 100];
pp += 2;
break;
de(17);
de(13);
de(9);
de(5);
case L(1)... R(1):
*pp = static_cast<char>('0' + x);
pp += 1;
break;
default:
*reinterpret_cast<uint32_t *>(pp) = D[x / pw10(16)];
pp += 4;
x %= pw10(16);
de(16);
de(12);
de(8);
case L(4)... R(4):
*reinterpret_cast<uint32_t *>(pp) = D[x];
pp += 4;
break;
de(19);
de(15);
de(11);
de(7);
case L(3)... R(3):
*reinterpret_cast<uint32_t *>(pp) = D[x * 10];
pp += 3;
break;
}
#else
write(u128(x));
#endif
}
constexpr u128 _pw10(int x) { return x == 0 ? 1 : _pw10(x - 1) * 10; }
void IO::write(u128 x)
{
if (std ::end(pbuf) - pp < 64)
flush();
constexpr auto pw10 = _pw10;
auto L = [&](int x)
{ return x == 1 ? 0 : pw10(x - 1); };
auto R = [&](int x)
{ return pw10(x) - 1; };
#define de(t) \
case L(t)... R(t): \
*reinterpret_cast<uint32_t *>(pp) = D[x / pw10((t)-4)]; \
pp += 4; \
x %= pw10((t)-4);
switch (x)
{
de(38);
de(34);
de(30);
de(26);
de(22);
de(18);
de(14);
de(10);
de(6);
case L(2)... R(2):
*reinterpret_cast<uint32_t *>(pp) = D[x * 100];
pp += 2;
break;
de(37);
de(33);
de(29);
de(25);
de(21);
de(17);
de(13);
de(9);
de(5);
case L(1)... R(1):
*pp = static_cast<char>('0' + x);
pp += 1;
break;
de(36);
de(32);
de(28);
de(24);
de(20);
de(16);
de(12);
de(8);
case L(4)... R(4):
*reinterpret_cast<uint32_t *>(pp) = D[x];
pp += 4;
break;
default:
*reinterpret_cast<uint32_t *>(pp) = D[x / pw10(35)];
pp += 4;
x %= pw10(35);
de(35);
de(31);
de(27);
de(23);
de(19);
de(15);
de(11);
de(7);
case L(3)... R(3):
*reinterpret_cast<uint32_t *>(pp) = D[x * 10];
pp += 3;
break;
}
}
void IO::write(bool x) { putch(x ^ 48); }
void IO::write(char c) { putch(c); }
template <typename T>
requires std ::is_floating_point_v<T>
void IO::write(T x)
{
static char buf[512];
*std ::to_chars(buf, buf + 512, x, std ::chars_format::fixed, precision).ptr = 0;
write(buf);
}
void IO::write(char *s)
{
while (*s)
putch(*s++);
}
void IO::write(const char *s)
{
while (*s)
putch(*s++);
}
void IO::write(const std ::string &s) { write(s.data()); }
template <typename T>
void IO::write(std ::initializer_list<T> t)
{
auto first = std ::begin(t), last = std ::end(t);
if (first != last)
for (write(*first++); first != last; ++first)
{
putch(' ');
write(*first);
}
}
template <tupleLike T>
void IO::write(T &&t)
{
[&]<std ::size_t... I>(std ::index_sequence<I...>)
{
(..., (I == 0 ? void(0) : putch(' '), write(std ::get<I>(t))));
}(std ::make_index_sequence<std ::tuple_size_v<std ::remove_reference_t<T>>>());
}
template <std ::ranges::input_range R>
void IO::write(R &&r)
{
if constexpr (std ::is_same_v<std ::decay_t<R>, std ::string>)
return write(r.data());
auto first = std ::ranges::begin(r), last = std ::ranges::end(r);
if (first != last)
for (write(*first++); first != last; ++first)
{
putch(' ');
write(*first);
}
}
template <typename... Args>
void IO::writeln(Args &&...x) { write(std ::forward<Args>(x)...), putch('\n'); }
template <typename T>
inline void IO::writespc(T &&x) { write(x), putch('\n'); }
} // namespace FastIO
using namespace FastIO;
#else
#ifndef LUOGU
#define auto_att __attribute__((target("avx2"), optimize(3, "Ofast", "unroll-loops", "inline")))
#else
#define auto_att
#endif
namespace FastIO
{
#define endl '\n'
INLINE_V static constexpr size_t buf_size = 1 << 18;
INLINE_V static constexpr size_t integer_size = 20;
INLINE_V static constexpr size_t block_size = 10000;
static char inbuf[buf_size + 1] = {};
static char outbuf[buf_size + 1] = {};
static char block_str[block_size * 4 + 1] = {};
static constexpr uint64_t power10[] = {1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000,
100000000000,
1000000000000,
10000000000000,
100000000000000,
1000000000000000,
10000000000000000,
100000000000000000,
1000000000000000000,
10000000000000000000u};
struct Scanner
{
#ifndef LOCAL
private:
size_t pos, end;
inline void load()
{
end = fread(inbuf, 1, buf_size, stdin);
inbuf[end] = '\0';
}
inline void reload()
{
size_t len = end - pos;
memmove(inbuf, inbuf + pos, len);
end = len + fread(inbuf + len, 1, buf_size - len, stdin);
inbuf[end] = '\0';
pos = 0;
}
inline void skip_space()
{
while (inbuf[pos] <= ' ')
{
if (__builtin_expect(++pos == end, 0))
reload();
}
}
inline char get_next() { return inbuf[pos++]; }
inline char get_next_nonspace()
{
skip_space();
return inbuf[pos++];
}
public:
Scanner() { load(); }
auto_att inline void read(char &c) { c = get_next_nonspace(); }
auto_att inline void read(std ::string &s)
{
skip_space();
s = "";
do
{
size_t start = pos;
while (inbuf[pos] > ' ')
++pos;
s += std ::string(inbuf + start, inbuf + pos);
if (inbuf[pos] != '\0')
break;
reload();
} while (true);
}
template <class T>
typename std ::enable_if<std ::is_integral<T>::value, void>::type read(T &x)
{
char c = get_next_nonspace();
if (__builtin_expect(pos + integer_size >= end, 0))
reload();
bool neg = false;
if (c == '-')
neg = true, x = 0;
else
x = c & 15;
while ((c = get_next()) >= '0')
x = (x << 3) + (x << 1) + (c & 15);
if (neg)
x = -x;
}
#else
template <typename _Tp>
inline void read(_Tp &x)
{
char ch = getchar();
int f = 1;
x = 0;
for (; ch < '0' || ch > '9'; ch = getchar())
f = ch == '-' ? -1 : 1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = (x << 3) + (x << 1) + (ch ^ 48);
x *= f;
}
inline void read(char &c) { c = getchar(); }
inline void read(char *x)
{
int i = 0;
char c = ' ';
for (; c <= ' ';)
c = getchar();
for (; c > ' ';)
x[i++] = c, c = getchar();
x[i] = 0;
}
inline void read(std ::string &x)
{
x.clear();
char c = ' ';
for (; c <= ' ';)
c = getchar();
for (; c > ' ';)
x += c, c = getchar();
}
#endif
template <class Head, class... Others>
void read(Head &head, Others &...others)
{
read(head);
read(others...);
}
template <class T1, class T2>
inline void read(std ::pair<T1, T2> &x) { read(x.first), read(x.second); }
} is;
struct Printer
{
#ifndef LOCAL
private:
size_t pos = 0;
auto_att inline void flush()
{
fwrite(outbuf, 1, pos, stdout);
pos = 0;
}
inline void pre_calc()
{
for (size_t i = 0; i < block_size; ++i)
{
size_t j = 4, k = i;
while (j--)
{
block_str[i * 4 + j] = k % 10 | 48;
k /= 10;
}
}
}
static EXPR size_t get_integer_size(uint64_t n)
{
if (n >= power10[10])
{
if (n >= power10[19])
return 20;
if (n >= power10[18])
return 19;
if (n >= power10[17])
return 18;
if (n >= power10[16])
return 17;
if (n >= power10[15])
return 16;
if (n >= power10[14])
return 15;
if (n >= power10[13])
return 14;
if (n >= power10[12])
return 13;
if (n >= power10[11])
return 12;
return 11;
}
else
{
if (n >= power10[9])
return 10;
if (n >= power10[8])
return 9;
if (n >= power10[7])
return 8;
if (n >= power10[6])
return 7;
if (n >= power10[5])
return 6;
if (n >= power10[4])
return 5;
if (n >= power10[3])
return 4;
if (n >= power10[2])
return 3;
if (n >= power10[1])
return 2;
return 1;
}
}
public:
Printer() { pre_calc(); }
~Printer() { flush(); }
inline void write(char c)
{
outbuf[pos++] = c;
if (__builtin_expect(pos == buf_size, 0))
flush();
}
inline void write(const char *s)
{
while (*s != 0)
{
outbuf[pos++] = *s++;
if (__builtin_expect(pos == buf_size, 0))
flush();
}
}
inline void write(const std ::string &s)
{
for (auto c : s)
{
outbuf[pos++] = c;
if (__builtin_expect(pos == buf_size, 0))
flush();
}
}
template <class T>
typename std ::enable_if<std ::is_integral<T>::value, void>::type write(T x)
{
if (__builtin_expect(pos + integer_size >= buf_size, 0))
flush();
if (x < 0)
write('-'), x = -x;
size_t digit = get_integer_size(x);
size_t len = digit;
while (len >= 4)
{
len -= 4;
memcpy(outbuf + pos + len, block_str + ((x % block_size) << 2), 4);
x /= block_size;
}
memcpy(outbuf + pos, block_str + (x << 2) + (4 - len), len);
pos += digit;
}
#else
inline void write(char c) { putchar(c); }
inline void write(const char *x)
{
for (int i = 0; x[i]; ++i)
write(x[i]);
}
inline void write(std::string s)
{
for (auto c : s)
write(c);
}
template <typename _Tp>
inline void write(_Tp x)
{
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar(x % 10 ^ 48);
}
#endif
template <class Head, class... Others>
void write(const Head &head, const Others &...others)
{
write(head);
write(' ');
write(others...);
}
template <class... Args>
void writeln(const Args &...args)
{
write(args...);
write('\n');
}
template <class T>
inline void writespc(const T &x)
{
write(x);
write(" ");
}
template <class T1, class T2>
inline void write(const std ::pair<T1, T2> &x) { write(x.first, x.second); }
} os;
struct IO : public Scanner, public Printer
{
} io;
}; // namespace FastIO
using namespace FastIO;
#endif
#endif
using namespace std;
#warning Do not use debug.hpp.
signed main() {
int T;
io.read(T);
while (T--) {
int n;
io.read(n);
vector<int> a(n + 1);
vector<int> b(n + 1);
rep(i, 1, n) io.read(a[i]);
sort(begin(a) + 1, end(a));
int mid = 0;
while (mid + 1 <= n && a[mid + 1] <= 0)
++mid;
int l = (1 + mid) / 2, r = l + 1;
rep(i, 1, mid) {
if (i & 1)
b[l] = a[i], --l;
else
b[r] = a[i], ++r;
}
l = (mid + 1 + n) / 2, r = l + 1;
per(i, n, mid + 1) {
if ((n - i + 1) & 1)
b[l] = a[i], --l;
else
b[r] = a[i], ++r;
}
if ((b[1] < b[mid]) == (b[mid + 1] < b[n]))
reverse(begin(b) + 1, begin(b) + mid + 1);
ll ans = 0;
rep(i, 1, n)
ans += 1ll * b[i] * (i == n ? b[1] : b[i + 1]);
io.writeln(ans);
}
#if defined(LOCAL) && !defined(CPH)
cerr << "Spend time : " << clock() * 1. / CLOCKS_PER_SEC * 1000. << "ms\n";
#endif
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3848kb
input:
4 3 1 2 3 6 1 1 1 1 0 0 5 100000 100000 100000 100000 -100000 5 1 2 3 4 5
output:
11 3 10000000000 48
result:
ok 4 number(s): "11 3 10000000000 48"
Test #2:
score: 0
Accepted
time: 15ms
memory: 5496kb
input:
1 200000 11009 633591 -419208 -664908 731171 -774644 -878270 656078 -38057 -220602 -897906 670165 -765931 -612936 -583782 -549624 -644245 137209 -983054 -110583 349193 699723 -412876 -417691 810865 -474314 -200632 570810 -283481 39600 20940 218215 -408751 -507326 -961614 600863 499517 -538207 767155...
output:
66608463123493911
result:
ok 1 number(s): "66608463123493911"
Test #3:
score: 0
Accepted
time: 11ms
memory: 5524kb
input:
1 200000 7902 376928 977876 -664787 382287 -671247 263725 -875047 554163 59899 -976624 726497 617682 -387432 -960499 -703748 -644991 72374 -564962 -569121 -412123 907945 -379338 915496 665461 389485 -742294 942448 -862668 -677446 -72510 -382856 893536 128827 -596269 -572440 -532339 536145 -521524 -3...
output:
66581505433554478
result:
ok 1 number(s): "66581505433554478"
Test #4:
score: 0
Accepted
time: 11ms
memory: 5672kb
input:
1 200000 969944 -942957 346587 328855 61775 -596222 -622652 -504245 181233 -694452 -90194 -350098 1294 844554 -273993 205351 -674109 35911 887981 -929585 826563 179388 689051 -716467 450354 -746718 -220733 348937 529775 -331268 868892 -885852 -839030 834684 803928 225887 435807 575645 189798 -255705...
output:
66721135858270454
result:
ok 1 number(s): "66721135858270454"
Test #5:
score: 0
Accepted
time: 15ms
memory: 5628kb
input:
1 200000 -68015 -262842 778522 -607801 -287109 542027 -515508 -105072 871526 -483654 865940 -391840 419758 -929943 -650710 -885551 359996 -965702 340923 611878 -899901 387610 -214190 616720 235247 117080 300828 -342648 20292 -83165 747072 -521774 561331 505688 -830728 -877713 438804 -419707 -35659 7...
output:
66646767806203928
result:
ok 1 number(s): "66646767806203928"
Test #6:
score: 0
Accepted
time: 9ms
memory: 5544kb
input:
1 200000 894027 417274 -887618 -579309 -635992 645424 598116 363804 -536255 -203152 787222 531567 866594 -732810 35796 -941601 -703973 -65388 759014 -811810 -724439 595832 -180652 -78465 -945009 113803 -212463 994139 377883 -800211 -311527 -59622 828766 -788457 -493755 885764 372098 -380207 675663 8...
output:
66669581129323609
result:
ok 1 number(s): "66669581129323609"
Test #7:
score: 0
Accepted
time: 14ms
memory: 5244kb
input:
2 100000 169603 145427 -202283 -480350 -856872 -65853 -442884 -773569 -275747 -953075 873381 -155156 -519569 -351127 558958 -345448 461553 927180 -310163 -46521 -857521 -906097 -91734 875600 836439 39554 488295 162237 -570813 -456645 -876308 254421 93745 689934 -712525 31372 536079 487786 191237 747...
output:
33392172147649469 33329865049707147
result:
ok 2 number(s): "33392172147649469 33329865049707147"
Test #8:
score: 0
Accepted
time: 15ms
memory: 5512kb
input:
2 100000 166496 -139607 131578 -451857 794246 -927605 601038 660456 316473 -672574 -170486 -196898 -101105 909229 -754537 535280 -602416 -74433 -857221 -435356 381164 365348 -58196 208786 621332 -998574 -990145 -431275 -213222 -110468 65094 716574 430883 -604211 -375551 829699 -495776 -535937 -13229...
output:
33445326050536015 33363059196148572
result:
ok 2 number(s): "33445326050536015 33363059196148572"
Test #9:
score: 0
Accepted
time: 16ms
memory: 5244kb
input:
2 100000 -871463 568880 -401636 611487 445363 -852579 777884 -870669 -56457 -461776 -249205 824583 -717493 71511 868747 -555622 -603163 825881 595722 -893894 -380151 573570 -24659 548454 377854 -134776 496566 -157711 242444 -827514 -56727 -919349 698318 -933207 954943 -343604 472370 -496437 579028 -...
output:
33375458994693108 33453260311973619
result:
ok 2 number(s): "33375458994693108 33453260311973619"
Test #10:
score: 0
Accepted
time: 11ms
memory: 5448kb
input:
2 100000 90579 -751006 -67776 -423243 124850 -749182 856656 -471496 -429386 783875 637226 782840 666120 -668134 -444748 353477 430943 -175732 -986188 745643 -204689 781792 -927900 -118360 162747 -236127 -981874 213927 669738 -579410 884675 577656 604 737799 355139 -510426 -587857 -456937 325201 -996...
output:
33287772768665659 33313392958107202
result:
ok 2 number(s): "33287772768665659 33313392958107202"
Test #11:
score: 0
Accepted
time: 15ms
memory: 5264kb
input:
2 100000 -947380 -70891 -670693 -394750 -224033 389067 -1349 -2620 -773945 -935625 656582 -293754 -915416 494148 -821465 -737425 -633027 -212195 466755 287104 68848 53235 -894362 249678 982492 -309107 -460313 557194 -972672 -331307 860928 -23415 -668739 408803 692113 316272 380289 -417437 -963479 -9...
output:
33288924160392129 33379620728513712
result:
ok 2 number(s): "33288924160392129 33379620728513712"
Test #12:
score: 0
Accepted
time: 13ms
memory: 5208kb
input:
10 20000 841746 527518 595261 331297 -946901 129987 670374 -140388 -684770 309555 -302589 415564 -387435 613331 -624940 -95922 945847 -199224 24636 -565799 -72069 -395358 -523453 -511446 854898 -846967 -749453 -341866 173256 -508156 574073 878761 984359 798117 -622388 434663 264157 607113 -38776 139...
output:
6622802477773024 6640013265208007 6592100254591181 6640170208895030 6688143425864831 6705222845668074 6676830146528095 6618221005278570 6714940493280121 6724345935461679
result:
ok 10 numbers
Test #13:
score: 0
Accepted
time: 11ms
memory: 5416kb
input:
100 2000 -607224 -718287 -433045 -816611 -935719 -559217 508630 -485197 134273 -520442 886499 318527 104344 -376092 -146638 -921227 98459 -324526 -396653 39142 -536124 114612 -22769 215450 -806035 217692 -758435 -360933 -470886 -271877 -839014 683804 138195 405799 -385833 481109 198084 -145350 -4353...
output:
664320265599433 656060796084111 670847172001984 693285015575982 641806321082038 686775083044219 660836368097072 665848189708176 656774554796514 640515026177509 637922445211883 651793096312130 668305333192730 680836044550146 642790615977139 659533778528338 689986245290621 679990801733695 664398460938...
result:
ok 100 numbers
Test #14:
score: 0
Accepted
time: 9ms
memory: 5216kb
input:
1000 200 324840 875913 -750586 872247 891834 -802538 -394930 -39271 -402853 -935536 -148671 -311516 -470147 168411 428251 -412526 736882 93448 384219 -987587 -924419 974032 864821 448111 409149 -889736 17366 -198877 -314710 -952652 406856 124043 183144 -182906 -79075 308212 785198 -276953 260766 178...
output:
65021379321819 61482831940623 64907628507685 65762816459484 64637099110104 70521910685375 71017401353273 63188959323659 64923749759877 78536500128365 72214363259863 72551077515316 64319147242944 70483971638209 60920280492111 74602439250852 66241024614625 66265066071760 69062565668032 71163034443457 ...
result:
ok 1000 numbers
Test #15:
score: 0
Accepted
time: 6ms
memory: 5304kb
input:
10000 20 15812 -152393 101507 981503 -762519 70570 197040 710069 767152 -683226 691070 -756515 -396347 -388139 -327351 -323136 -451642 -327842 989160 548358 20 -344746 439864 -408284 787179 -526193 -898718 -1701 -843020 956421 697486 -822953 243028 -209174 82020 -997778 242139 -932413 -673559 176880...
output:
5836057833484 7900249490263 6741513108893 7695904868186 5905346687260 6210447934702 6528577603481 6366200793553 7285941052913 6695898759021 5336229787408 6603459571509 5516442382707 6068193077387 6836405277420 5809766642183 7914983783571 7722262525971 6863867878728 5452027662683 6336759366520 571716...
result:
ok 10000 numbers
Test #16:
score: 0
Accepted
time: 11ms
memory: 5212kb
input:
42 4761 -884836 423256 -16540 5180 626061 574189 64375 565165 891155 631808 84833 -143882 -909496 758173 204660 -700050 163672 -867390 920820 848717 -347766 -768001 437344 -21942 403333 -220324 388822 84096 -938705 527686 841315 876833 131270 836458 700753 -740379 253394 -432058 494815 -255931 -8361...
output:
1568197940334474 1627465779774764 1613876915865833 1579828038154862 1563836977436851 1608582638636216 1589836095120383 1577031292775532 1608544317984962 1617042083628598 1589929522676958 1588048186120208 1562897607148914 1551101839004540 1582371865754027 1590036248776725 1606863383861959 16117573084...
result:
ok 42 numbers
Test #17:
score: 0
Accepted
time: 11ms
memory: 5112kb
input:
69 2898 -826877 -729045 -951445 -391404 197462 798213 -288928 -157886 631013 -85218 377079 -128051 -661781 -189943 -900471 43621 -847997 400563 -695730 -932224 -245215 -395642 -559167 -118464 920363 252772 -623730 -286403 -674675 -560306 -448483 924941 210935 -834513 608008 -136257 -953207 665428 29...
output:
942714868546250 946859857816400 969037992012804 977484785384248 919489346073972 969868311124320 933818196842132 977503973543166 967593973040804 982214889485424 963286569050653 970847618560724 957508838495683 945357209274080 933598867828567 978233850926901 975379109413588 1000443792815115 98105291396...
result:
ok 69 numbers