Repository navigation
Expand file tree
/
Copy path1621G-G-WeightedIncreasingSubsequences.cpp
More file actions
192 lines (169 loc) · 3.95 KB
/
Copy path1621G-G-WeightedIncreasingSubsequences.cpp
File metadata and controls
192 lines (169 loc) · 3.95 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// url:https://codeforces.com/contest/1621/problem/G
// time:2022-01-03 19:27:48
// name:G-WeightedIncreasingSubsequences
#include <bits/stdc++.h>
constexpr int P = 1000000007;
using i64 = long long;
// assume -P <= x < 2P
int norm(int x) {
if (x < 0) {
x += P;
}
if (x >= P) {
x -= P;
}
return x;
}
template<class T>
T power(T a, int b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
struct Z {
int x;
Z(int x = 0) : x(norm(x)) {}
int val() const {
return x;
}
Z operator-() const {
return Z(norm(P - x));
}
Z inv() const {
assert(x != 0);
return power(*this, P - 2);
}
Z &operator*=(const Z &rhs) {
x = i64(x) * rhs.x % P;
return *this;
}
Z &operator+=(const Z &rhs) {
x = norm(x + rhs.x);
return *this;
}
Z &operator-=(const Z &rhs) {
x = norm(x - rhs.x);
return *this;
}
Z &operator/=(const Z &rhs) {
return *this *= rhs.inv();
}
friend Z operator*(const Z &lhs, const Z &rhs) {
Z res = lhs;
res *= rhs;
return res;
}
friend Z operator+(const Z &lhs, const Z &rhs) {
Z res = lhs;
res += rhs;
return res;
}
friend Z operator-(const Z &lhs, const Z &rhs) {
Z res = lhs;
res -= rhs;
return res;
}
friend Z operator/(const Z &lhs, const Z &rhs) {
Z res = lhs;
res /= rhs;
return res;
}
};
struct Info {
int mx;
Z cntmx;
Z cnt;
};
Info operator+(const Info &a, const Info &b) {
if (a.mx > b.mx) {
return {a.mx, a.cntmx, a.cnt + b.cnt};
} else if (a.mx < b.mx) {
return {b.mx, b.cntmx, a.cnt + b.cnt};
} else {
return {a.mx, a.cntmx + b.cntmx, a.cnt + b.cnt};
}
}
constexpr int N = 1 << 19;
Info info[N];
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void build(int p, int l, int r) {
info[p] = {-1, 0, 0};
if (r - l == 1) {
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
pull(p);
}
void modify(int p, int l, int r, int x, Info v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
Info rangeSum(int p, int l, int r, int x, int y) {
if (l >= y || r <= x) {
return {-1, 0, 0};
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
return rangeSum(2 * p, l, m, x, y) + rangeSum(2 * p + 1, m, r, x, y);
pull(p);
}
void solve() {
int n;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::vector<std::pair<int, int>> v;
for (int i = 0; i < n; i++) {
v.emplace_back(a[i], -i);
}
std::sort(v.begin(), v.end());
for (int i = 0; i < n; i++) {
a[i] = std::lower_bound(v.begin(), v.end(), std::pair(a[i], -i)) - v.begin();
}
build(1, 0, n);
Z ans = 0;
std::vector<Z> f(n);
for (int i = n - 1; i >= 0; i--) {
auto s = Info{i, 1, 1} + rangeSum(1, 0, n, a[i], n);
modify(1, 0, n, a[i], s);
f[i] = s.cnt - s.cntmx;
}
build(1, 0, n);
for (int i = 0; i < n; i++) {
auto s = Info{i, 1, 1} + rangeSum(1, 0, n, 0, a[i]);
modify(1, 0, n, a[i], s);
ans += f[i] * s.cnt;
}
std::cout << ans.val() << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}