QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#123856 | #5374. 数圈 | 1234567890 | 100 ✓ | 248ms | 12600kb | C++14 | 3.2kb | 2023-07-13 20:33:43 | 2023-07-13 20:33:46 |
Judging History
answer
/*
灏忓簾鐗╋紝杩欓兘涓嶄細 /cf
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define inf (ll)1e9
#define pii pair <ll, ll>
#define fr first
#define se second
const ll mod = 1e9 + 7;
char buf[1 << 21], *p1 = buf, *p2 = buf;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 18, stdin), p1 == p2) ? EOF : *p1++)
inline ll read() {
ll x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9') f = ((ch == '-') ? -1 : f), ch = getchar();
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
inline void write(ll x) {
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
ll n;
ll a[100005], s[100005], id[100005];
ll bs1[100005], bs2[100005];
ll ts1[100005], ts2[100005];
vector <ll> G;
struct BIT_TREE {
inline ll lowbit(ll x) {
return x & (-x);
}
ll bit[200005];
inline void add(ll x, ll y) {
for(ll i = x; i <= 2 * n; i += lowbit(i)) bit[i] += y;
}
inline ll Sum(ll x) {
ll Ans = 0;
for(ll i = x; i; i -= lowbit(i)) Ans += bit[i];
return Ans;
}
inline ll get_sum(ll l, ll r) {
return Sum(r) - Sum(l - 1);
}
inline void clear() {
for(ll i = 0; i <= 2 * n; i++) bit[i] = 0;
}
}Tr;
int main() {
// freopen("circle.in", "r", stdin);
// freopen("circle.out", "w", stdout);
ll T = read();
while(T--) {
n = read();
for(ll i = 1; i <= n; i++) a[i] = read(), s[i] = s[i-1] + a[i], id[i] = i;
if(s[n] <= 0) {
ll fl = 1;
for(ll i = 1; i <= n; i++) fl &= !a[i];
if(fl) puts("0");
else puts("-1");
continue;
}
G.clear(), G.reserve(2 * n);
for(ll i = 1; i <= n; i++) {
bs1[i] = (ll)floor(1.0 * (s[i] - 1) / s[n]);
bs2[i] = (ll)floor(1.0 * s[i] / s[n]);
ts1[i] = s[i] - 1 - bs1[i] * s[n];
ts2[i] = s[i] - bs2[i] * s[n];
G.push_back(ts1[i]), G.push_back(ts2[i]);
}
sort(G.begin(), G.end()), G.erase(unique(G.begin(), G.end()), G.end());
for(ll i = 1; i <= n; i++) {
ts1[i] = lower_bound(G.begin(), G.end(), ts1[i]) - G.begin() + 1;
ts2[i] = lower_bound(G.begin(), G.end(), ts2[i]) - G.begin() + 1;
}
ll Ans = 0;
// for(ll i = 1; i <= n; i++) {
// for(ll j = 1; j <= n; j++) {
// if(s[i] <= s[j]) continue;
// Ans += (i < j) + (s[i] - s[j] - 1) / s[n];
// }
// }
sort(id + 1, id + 1 + n, [&] (ll A, ll B) {return s[A] < s[B];});
//s[i] > s[j] && i < j
Tr.clear();
for(ll i = 1, j; i <= n; i = j + 1) {
j = i;
while(j < n && s[id[j+1]] == s[id[j]]) j++;
for(ll k = i; k <= j; k++) Ans += Tr.get_sum(id[k], 2 * n);
for(ll k = i; k <= j; k++) Tr.add(id[k], 1);
}
//sum of bs1 - bs2
for(ll i = 1, j; i <= n; i = j + 1) {
j = i;
while(j < n && s[id[j+1]] == s[id[j]]) j++;
for(ll k = i; k <= j; k++) {
Ans += bs1[id[k]] * (i - 1);
Ans -= bs2[id[k]] * (n - j);
}
}
// //ts1 < ts2
Tr.clear();
for(ll i = 1, j; i <= n; i = j + 1) {
j = i;
while(j < n && s[id[j+1]] == s[id[j]]) j++;
for(ll k = i; k <= j; k++) Ans -= Tr.get_sum(ts1[id[k]] + 1, 2 * n);
for(ll k = i; k <= j; k++) Tr.add(ts2[id[k]], 1);
}
write(Ans), putchar('\n');
}
return 0;
}
/*
1
3
5 2 -2
5 7 5
0 1 0
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 2ms
memory: 9664kb
input:
10 3 2 -9 -4 3 4 6 0 3 3 -10 0 3 7 -6 3 3 -6 7 10 3 -3 9 -2 3 6 1 -2 3 5 2 -2 3 -9 -5 7 3 -4 -5 6
output:
-1 0 -1 3 1 4 2 1 -1 -1
result:
ok 10 lines
Subtask #2:
score: 20
Accepted
Dependency #1:
100%
Accepted
Test #2:
score: 20
Accepted
time: 2ms
memory: 11544kb
input:
10 50 -5 -5 3 8 0 0 3 8 5 -7 6 -9 5 5 2 7 -9 1 -7 5 0 10 -6 -2 -6 -8 9 7 4 3 -9 9 5 9 8 1 7 0 0 -5 -1 -3 5 -7 5 -4 6 8 -1 1 50 -6 4 3 -6 -9 -8 -5 -2 -10 7 -4 1 -1 -5 2 1 -10 8 8 7 -8 -5 3 -6 10 3 2 -1 10 0 4 -6 9 3 -6 3 9 -4 4 -2 -3 4 -9 -7 7 1 5 2 -4 5 50 2 -10 5 3 -1 1 9 -1 -5 3 -2 -10 7 0 5 -5 -1...
output:
161 -1 249 -1 21873 -1 452 -1 -1 314
result:
ok 10 lines
Subtask #3:
score: 30
Accepted
Dependency #2:
100%
Accepted
Test #3:
score: 30
Accepted
time: 6ms
memory: 9480kb
input:
10 2000 -4438 -448 2902 3873 -5348 1821 -5284 2787 -1369 -4712 3298 2808 1651 -4568 4377 870 2217 -2683 1217 120 -3854 1156 -2129 -3757 -2704 3026 -1745 -5327 -1315 405 3944 340 -1510 2213 -24 -32 -5414 -2330 760 3715 -4871 2831 1917 3148 1360 -3662 -4281 -1248 788 1334 -3401 2050 4174 3163 -2456 33...
output:
90206708583 9272643195 2640993721 148400379 20504656 2904294 -1 6666669000000 61998 67150
result:
ok 10 lines
Subtask #4:
score: 10
Accepted
Test #4:
score: 10
Accepted
time: 41ms
memory: 10408kb
input:
10 30000 -3879 -556 4570 1863 2815 -4010 2471 -270 2835 3071 -3331 -1251 -2243 4221 -5249 -4134 3376 1978 858 2545 -4207 386 3875 2029 1706 1119 3065 -3097 4399 4385 -3021 2473 2506 2157 3946 -886 3929 1478 2728 -4239 4091 -151 -4762 -2136 -1424 2162 -669 267 190 -1180 2640 -757 -2078 -1409 3165 216...
output:
121860198793245 46938573692959 57703965834328 59944493006183 81807878531011 49309954483988 78546660217267 113040545520897 33896072757379 62212580026212
result:
ok 10 lines
Subtask #5:
score: 10
Accepted
Dependency #4:
100%
Accepted
Test #5:
score: 10
Accepted
time: 42ms
memory: 12188kb
input:
10 30000 -2595 -3716 4165 858 -5266 -3829 811 -2088 3328 3550 4682 -4106 1810 -1775 -470 189 2599 -4024 2125 1382 2756 3173 1951 975 3411 389 4564 3431 -4952 4333 -2522 2676 2205 -105 -3087 -1781 4430 2299 4505 1113 -826 312 1429 52 -985 2395 2056 -2832 1613 -3243 3271 2772 -2816 -3652 4580 1365 123...
output:
-1 66307718106454 20087854603233 17527604892002 14265304369524 24031060577728 6589862789507 11905532141244 8996204627296 6585172490432
result:
ok 10 lines
Subtask #6:
score: 10
Accepted
Dependency #5:
100%
Accepted
Test #6:
score: 10
Accepted
time: 62ms
memory: 11624kb
input:
10 30000 -2244 -2644 2809 3538 -285 1898 -2058 -24 2091 2790 -2955 1099 -5143 1121 -3846 -999 -4595 3085 3334 -4501 -5091 358 -3560 -3527 4423 2862 4342 -2080 4525 2521 4106 -5224 1559 3007 -398 4417 -351 -5309 2315 3950 -1249 3651 -2944 -3367 3232 -1595 2952 -2194 4228 -4421 -4415 -88 -2072 3485 -1...
output:
85413029344138 4691174120217 246528544689 26919261466 816932685 45240604 -1 22499999825000000 1482487 1674097
result:
ok 10 lines
Subtask #7:
score: 10
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Dependency #6:
100%
Accepted
Test #7:
score: 10
Accepted
time: 248ms
memory: 12600kb
input:
10 100000 -295 3757 395 376 4467 -4688 -3479 1495 -2386 687 -1139 4137 2777 4198 2479 3744 -1902 1207 3000 -5091 1112 2776 -1673 4050 -5247 -3011 -1961 2442 -5024 3036 226 3508 2020 -1793 4283 3340 -115 2844 -3134 -847 -4850 3377 -1756 3602 4408 4043 69 -4157 -2612 3591 2041 -1034 4648 3484 -1086 12...
output:
1335845168539966 35870296157487 1936542233238 42129253701 1713350305 3538646 -1 833333331000000000 4603056 4382225
result:
ok 10 lines