QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#723921 | #4428. Fence | maspy | AC ✓ | 418ms | 132808kb | C++23 | 10.3kb | 2024-11-08 03:37:23 | 2024-11-08 03:37:23 |
Judging History
answer
#line 2 "/home/maspy/library/my_template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ll8 = __int128;
using ld = long double;
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 vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
IN(name)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
IN(name)
#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__))))
#define FOR(i, n) for (ll i = 0; (i) < (ll)(n); ++(i))
#define FOR3(i, m, n) for (ll i = (m); (i) < (ll)(n); ++(i))
#define FOR_R(i, n) for (ll i = (ll)(n)-1; (i) >= 0; --(i))
#define FOR3_R(i, m, n) for (ll i = (ll)(n)-1; (i) >= (ll)(m); --(i))
#define FORIN(x, A) for (auto&& x : A)
#define FOR_nCk(s, n, k) \
for (ll s = (1 << k) - 1, tmp_var = 0; s < (1 << n); \
tmp_var = s | (s - 1), s = (tmp_var + 1) | (((~tmp_var & -~tmp_var) - 1) >> (__builtin_ctz(s) + 1)))
#define FOR_SUB(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
using ull = unsigned long long;
using uint = unsigned int;
int popcnt(uint x) {
return __builtin_popcount(x);
}
int popcnt(int x) {
return __builtin_popcount(x);
}
int popcnt(ull x) {
return __builtin_popcountll(x);
}
int popcnt(ll x) {
return __builtin_popcountll(x);
}
int bsr(uint x) {
return 31 - __builtin_clz(x);
}
int bsr(ull x) {
return 63 - __builtin_clzll(x);
}
int ctz(int x) {
return __builtin_ctz(x);
}
int ctz(ll x) {
return __builtin_ctzll(x);
}
int ctz(ull x) {
return __builtin_ctzll(x);
}
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define SUM(v) accumulate(all(v), 0LL)
#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())
template <class T> T ceil(T x, T y) {
assert(y >= 1);
return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
assert(y >= 1);
return (x > 0 ? x / y : (x - y + 1) / y);
}
#define INT(...) \
int __VA_ARGS__; \
IN(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
IN(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
IN(__VA_ARGS__)
#define CHR(...) \
char __VA_ARGS__; \
IN(__VA_ARGS__)
#define DBL(...) \
long double __VA_ARGS__; \
IN(__VA_ARGS__)
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(long double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &a) {for(auto &i : a) scan(i);}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
scan(head);
IN(tail...);
}
ll isqrt(ll n) {
ll x = n, y = (n + 1) / 2;
while (y < x) { tie(x, y) = mp(y, (y + n / y) / 2); }
return x;
}
vi s_to_vi(string S, char first_char='a'){
vi A(S.size());
FOR(i, S.size()){
A[i] = S[i] - first_char;
}
return A;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& A) {
os << A.fi << " " << A.se;
return os;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& A) {
for (size_t i = 0; i < A.size(); i++) {
if(i) os << " ";
os << A[i];
}
return os;
}
void print() {
cout << "\n";
}
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
cout << head;
if (sizeof...(Tail)) cout << " ";
print(forward<Tail>(tail)...);
}
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
void YES(bool t = 1) { cout << YESNO[t] << endl; }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { cout << YesNo[t] << endl; }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { cout << yesno[t] << endl; }
void no(bool t = 1) { yes(!t); }
template <typename T>
vector<T> cumsum(vector<T> A) {
ll N = A.size();
vector<T> B(N + 1);
B[0] = T(0);
FOR(i, N) { B[i + 1] = B[i] + A[i]; }
return B;
}
vi bin_count(vi& A, ll size) {
vi C(size);
for (auto&& x : A) {
++C[x];
}
return C;
}
template <typename T>
vi argsort(vector<T>& A){
vi ids(A.size());
iota(all(ids), 0);
sort(all(ids), [&](ll i, ll j){
return A[i] < A[j] || (A[i] == A[j] && i < j);
});
return ids;
}
ll binary_search(function<bool(ll)> check, ll ok, ll ng) {
while (abs(ok - ng) > 1) {
auto x = (ng + ok) / 2;
if (check(x))
ok = x;
else
ng = x;
}
return ok;
}
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); }
template <typename T>
vc<T> merge_sort(vc<T>& A, vc<T>& B) {
vc<T> C;
C.reserve(A.size() + B.size());
merge(all(A), all(B), back_inserter(C));
return C;
}
#line 2 "main.cpp"
#line 3 "/home/maspy/library/ds/segtree.hpp"
template <typename T>
struct SegTree{
using F = function<T(T,T)>;
int N_;
int N;
F seg_f;
T T_unit;
vector<T> dat;
SegTree(F f,T T_unit): seg_f(f), T_unit(T_unit) {}
void init(int n_){
N_ = n_;
N=1;
while(N<n_) N<<=1;
dat.assign(N<<1,T_unit);
}
void build(const vector<T> &v){
init(v.size());
FOR(i, v.size()) dat[N + i] = v[i];
FOR3_R(i, 1, N){
dat[i] = seg_f(dat[i<<1 | 0], dat[i<<1 | 1]);
}
}
void set(int i, T x){
assert(i < N_);
dat[i += N] = x;
while(i >>= 1){
dat[i] = seg_f(dat[i<<1 | 0],dat[i<<1 | 1]);
}
}
T fold(int L, int R){
assert(L <= R);
assert(R <= N_);
T vl = T_unit, vr = T_unit;
L += N;
R += N;
while(L < R){
if(L & 1) vl = seg_f(vl, dat[L++]);
if(R & 1) vr = seg_f(dat[--R], vr);
L >>= 1;
R >>= 1;
}
return seg_f(vl, vr);
}
template<class F>
int max_right(int L, F &check){
assert(0<=L && L <= N_ && check(T_unit));
if(L==N_) return N_;
L += N;
T sm = T_unit;
do {
while(L%2==0) L>>=1;
if(!check(seg_f(sm, dat[L]))){
while(L < N) {
L = 2 * L;
if(check(seg_f(sm, dat[L]))){
sm = seg_f(sm, dat[L]);
L++;
}
}
return L - N;
}
sm = seg_f(sm, dat[L]);
L++;
} while((L&-L)!=L);
return N_;
}
template<class F>
int min_left(int R, F &check){
assert(0<=R && R <= N_ && check(T_unit));
if(R==0) return 0;
R += N;
T sm = T_unit;
do {
--R;
while(R>1 && (R%2)) R>>=1;
if(!check(seg_f(dat[R], sm))){
while(R < N) {
R = 2 * R + 1;
if(check(seg_f(dat[R], sm))){
sm = seg_f(dat[R], sm);
R--;
}
}
return R + 1 - N;
}
sm = seg_f(dat[R], sm);
} while((R&-R)!=R);
return 0;
}
};
#line 4 "main.cpp"
void solve() {
LL(N);
VEC(ll, A, N);
// A の値 → インデックス
const int K = MAX(A);
/*
b-1 -> b で商が変わるものの index
*/
vc<vi> change(K + 1);
FOR(i, N) {
FOR3(b, 1, A[i]) {
ll x = (A[i] + b - 1) / b;
ll y = (A[i] + b) / (b + 1);
if (x != y) change[b + 1].eb(i);
}
}
struct E {
ll n;
ll od_p;
ll od_q;
ll ev_p;
ll ev_q;
};
SegTree<E> seg(
[](E x, E y) {
E z;
z.n = x.n + y.n;
if (x.n % 2 == 0) {
z.od_p = x.od_p + y.od_p;
z.od_q = x.od_q + y.od_q;
z.ev_p = x.ev_p + y.ev_p;
z.ev_q = x.ev_q + y.ev_q;
} else {
z.od_p = x.od_p + y.ev_p;
z.od_q = x.od_q + y.ev_q;
z.ev_p = x.ev_p + y.od_p;
z.ev_q = x.ev_q + y.od_q;
}
return z;
},
E({0, 0, 0, 0, 0}));
auto to_data = [&](ll x, ll n) -> E {
if (n % 2 == 0) {
return E({n, n / 2, 0, -n / 2, x});
} else {
return E({n, -n / 2, x, n / 2, 0});
}
};
seg.init(N);
vc<E> seg_raw(N);
FOR(i, N) { seg_raw[i] = to_data(A[i], A[i]); }
seg.build(seg_raw);
FOR3(b, 1, K + 1) {
FORIN(i, change[b]) {
ll n = (A[i] + b - 1) / b;
seg.set(i, to_data(A[i], n));
}
auto e = seg.fold(0, N);
// print(e.n, e.od_p, e.od_q, e.ev_p, e.ev_q);
ll ANS = e.od_p * b + e.od_q;
print(ANS);
}
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << setprecision(15);
LL(T);
FOR(_, T) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 418ms
memory: 71832kb
input:
5 333834 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
500000 500000 499995 499987 500000 500032 499996 499987 500032 500000 499994 499998 499981 499996 500080 500090 500077 500032 499980 499915 500035 499941 500055 499923 500000 499980 499935 500042 500174 499905 500002 499998 500218 499899 499965 500010 500144 500242 499839 499915 499987 500010 500122...
result:
ok 2500000 lines
Test #2:
score: 0
Accepted
time: 243ms
memory: 23828kb
input:
5 48356 1 1 2 2 1 1 1 1 2 4 8 4 1 2 128 4 1 1 2 1 1 2 2 1 1 1 4 1 1 1 2 1 2 8 8 1 1 1 1 8 1 2 1 2 1 1 1 1 8 1 2 1 2 1 2 1 2 1 1 8 1 2 4 1 1 1 1 1 4 1 2 1 1 1 2 1 2 64 1 256 1 1 1 1 1 2 32 1 1 2 1 1 2 2 1 1 1 1 1 1 4 1 1 2 2 1 1 1 1 1 1 2 1 2 256 4 1 1 2 1 2 1 2 2 1 1 2 2 2 2 1 1 1 2 1 32 1 1 1 4 1 1...
output:
500000 499939 500012 499925 499701 499996 499780 499879 500247 499914 500226 500164 500084 500054 499449 499819 500132 500378 500533 500517 499941 499527 500493 500808 500635 499680 500903 500128 500111 500413 499462 500244 500233 499885 499983 500105 499925 500185 499961 499466 500376 500081 498569...
result:
ok 987898 lines
Test #3:
score: 0
Accepted
time: 163ms
memory: 29964kb
input:
5 100000 1 10 9 6 2 9 5 8 6 8 3 10 10 4 8 10 5 4 8 1 3 3 10 6 8 2 1 1 7 4 4 7 3 3 2 7 3 8 4 8 8 8 9 7 1 6 7 5 1 6 5 6 10 7 1 7 10 4 9 6 7 3 4 2 7 7 8 9 7 1 8 4 1 6 10 1 3 8 8 5 6 4 10 5 10 3 4 1 8 2 8 4 6 1 7 2 10 2 2 3 3 3 4 6 6 6 6 7 8 8 8 9 9 9 9 9 10 10 4 5 10 3 1 5 6 7 9 5 3 2 2 3 1 2 1 6 1 1 6...
output:
275038 275208 276333 274460 274715 278680 279579 271029 278720 274871 251536 251586 251550 251533 251687 251362 251680 251736 251758 251640 252228 251127 251367 250702 251853 250779 250632 250725 251045 251774 251515 249096 250101 253104 247732 248767 248976 249529 253658 252323 252069 251541 251651...
result:
ok 1010971 lines
Test #4:
score: 0
Accepted
time: 324ms
memory: 132808kb
input:
5 1413 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1...
output:
499496 499496 499494 499848 499491 500202 499487 500553 499498 500905 499497 501264 499498 501599 499446 501961 499443 502306 499413 502649 499443 503017 499414 503329 499499 503710 499498 504049 499501 504360 499310 504777 499233 505155 499499 505440 499170 505761 499498 506160 499091 506583 499505...
result:
ok 502830 lines
Test #5:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
5 1 1 1 3 2 1 1 2 1 2 2 3 1
output:
1 2 2 3 1 2 1 2 3 3
result:
ok 10 lines
Extra Test:
score: 0
Extra Test Passed