-
Notifications
You must be signed in to change notification settings - Fork 9
/
1517E-E-GroupPhoto.cpp
100 lines (98 loc) · 2.99 KB
/
1517E-E-GroupPhoto.cpp
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
// url:https://codeforces.com/contest/1517/problem/E
// time:2021-04-23 18:39:17
// name:E-GroupPhoto
#include <bits/stdc++.h>
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
constexpr int P = 998244353;
template <typename T>
struct Fenwick {
const int n;
std::vector<T> a;
Fenwick(int n) : n(n), a(n) {}
void add(int x, T v) {
for (int i = x + 1; i <= n; i += i & -i) {
a[i - 1] += v;
}
}
T sum(int x) {
T ans = 0;
for (int i = x; i > 0; i -= i & -i) {
ans += a[i - 1];
}
return ans;
}
T rangeSum(int l, int r) {
return sum(r) - sum(l);
}
};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
int n;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
if (n == 1) {
std::cout << "1\n";
continue;
}
i64 ans = 0;
for (int p = 0; p < 2; p++) {
for (int c = 0; c < 2; c++) {
int delta = 0;
if (c) {
delta += a.back();
}
if (p) {
delta -= a[0];
}
int L = p, R = n - c;
std::vector<i64> pre(n + 1), alt(n + 1);
pre[L] = 0;
for (int i = L; i < R; i++) {
pre[i + 1] = pre[i] + a[i];
}
for (int i = L; i < R; i++) {
alt[i + 1] = a[i] - alt[i];
}
std::vector<i64> x(n), y(n);
std::vector<i64> v;
for (int i = L + 1; i < R; i++) {
x[i] = delta + pre[i] - alt[i];
y[i] = pre[R] - pre[i] - alt[i];
v.push_back(x[i]);
v.push_back(y[i]);
}
std::sort(v.begin(), v.end());
v.erase(std::unique(v.begin(), v.end()), v.end());
int m = v.size();
Fenwick<int> T[2] {Fenwick<int>(m), Fenwick<int>(m)};
for (int i = L + 1; i < R; i++) {
x[i] = std::lower_bound(v.begin(), v.end(), x[i]) - v.begin();
y[i] = std::lower_bound(v.begin(), v.end(), y[i]) - v.begin();
}
for (int i = L + 1; i < R; i++) {
T[i % 2].add(x[i], 1);
ans += T[i % 2].sum(y[i]);
}
if (!c && !p) {
for (int i = 0; i <= n; i++) {
if (pre[n] - pre[i] < pre[i]) {
ans++;
}
}
}
}
}
ans %= P;
std::cout << ans << "\n";
}
return 0;
}