2021牛客多校7
题号 | 标题 | 团队的状态 |
---|---|---|
A | xay loves connected graphs | 通过 |
B | xay loves monotonicity | 通过 |
C | xay loves jumping | 未通过 |
D | xay loves matrices | 通过 |
E | xay loves nim | 通过 |
F | xay loves trees | 通过 |
G | xay loves KDT | 未通过 |
H | xay loves count | 通过 |
I | xay loves or | 通过 |
J | xay loves Floyd | 通过 |
K | xay loves sequence | 通过 |
xay loves connected graphs
// Author: HolyK
// Created: Sun Aug 8 18:17:17 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
constexpr int P(998244353);
inline void inc(int &x, int y) {
+= y;
x if (x >= P) x -= P;
}
inline int mod(LL x) {
return x % P;
}
inline int reduce(int x) {
return x >= P ? x - P : x;
}
inline int norm(int x) {
return x < 0 ? x + P : x;
}
int fpow(int x, int k = P - 2) {
int r = 1;
for (; k; k >>= 1, x = 1LL * x * x % P) {
if (k & 1) r = 1LL * r * x % P;
}
return r;
}
using Info = std::array<int, 2>;
&operator+=(Info &a, Info b) {
Info (a[0], b[0]);
inc(a[1], b[1]);
increturn a;
}
&operator-=(Info &a, Info b) {
Info (a[0], P - b[0]);
inc(a[1], P - b[1]);
increturn a;
}
operator+(Info a, Info b) {
Info return a += b;
}
operator-(Info a, Info b) {
Info return a -= b;
}
operator*(Info a, Info b) {
Info return {mod(1LL * a[0] * b[0]), mod(1LL * a[0] * b[1] + 1LL * a[1] * b[0])};
}
operator*(Info a, LL b) {
Info return {mod(a[0] * b), mod(a[1] * b)};
}
using Poly = std::array<Info, 21>;
constexpr int N(1 << 20);
int n, len, cnt[N];
void fwt(Poly *a) {
for (int i = 1; i < len; i <<= 1) {
for (int j = 0; j < len; j += i * 2) {
for (int k = 0; k < i; k++) {
for (int p = 0; p <= n; p++) {
[i + j + k][p] += a[j + k][p];
a}
}
}
}
}
void ifwt(Poly *a) {
for (int i = 1; i < len; i <<= 1) {
for (int j = 0; j < len; j += i * 2) {
for (int k = 0; k < i; k++) {
for (int p = 0; p <= n; p++) {
[i + j + k][p] -= a[j + k][p];
a}
}
}
}
}
const auto Inv = [] {
std::array<int, 21> c;
[0] = c[1] = 1;
cfor (int i = 2; i < 21; i++) {
[i] = 1LL * (P - P / i) * c[P % i] % P;
c}
return c;
}();
(Poly a) {
Poly ln;
Poly bfor (int i = 0; i < n; i++) b[i] = a[i + 1] * (i + 1);
for (int i = 1; i < n; i++) {
for (int j = 1; j <= i; j++) {
[i] = b[i] - a[j] * b[i - j];
b}
}
for (int i = n; i; i--) {
[i] = b[i - 1] * Inv[i];
b}
[0].fill(0);
breturn b;
}
[N];
Poly f[N];
Info aint main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
= 1 << n;
len for (int i = 1; i < len; i++) {
[i] = 1 + cnt[i & i - 1];
cnt}
for (int i = 0; i < len; i++) {
[i] = {1, 0};
a}
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
int x;
std::cin >> x;
[1 << i | 1 << j] = {x + 1, x};
a}
}
for (int i = 1; i < len; i <<= 1) {
for (int j = 0; j < len; j += i * 2) {
for (int k = 0; k < i; k++) {
[i + j + k] = a[j + k] * a[i + j + k];
a}
}
}
for (int i = 0; i < len; i++) f[i][cnt[i]] = a[i];
(f);
fwtfor (int i = 0; i < len; i++) {
[i] = ln(f[i]);
f}
// ifwt(f);
// std::cout << f[len - 1][n][1] << "\n";
int ans = 0;
for (int i = 0; i < len; i++) {
if (cnt[i] & 1) {
(ans, P - f[i][n][1]);
inc} else {
(ans, f[i][n][1]);
inc}
}
if (n & 1) ans = P - ans;
std::cout << ans % P << "\n";
return 0;
}
xay loves monotonicity
线段树
// Author: HolyK
// Created: Sat Aug 7 20:25:17 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
constexpr int N(2e5 + 5);
int n, c[N], a[N];
void add(int p) {
for (; p <= n; p += p & -p) c[p] ^= 1;
}
void add(int l, int r) {
(l), add(r + 1);
add}
int ask(int p) {
int r = 0;
for (; p; p -= p & -p) r ^= c[p];
return r;
}
#define ls o << 1
#define rs o << 1 | 1
int pos[N << 2], cnt[N << 2];
int ask(int o, int l, int r, int x) {
if (a[x] > a[pos[o]]) return 0;
int ans = 0;
while (l < r) {
int m = l + r >> 1;
if (a[pos[ls]] >= a[x]) {
+= cnt[o] - cnt[ls];
ans = ls;
o = m;
r } else {
= rs;
o = m + 1;
l }
}
assert(a[l] >= a[x]);
return ans += ask(x) ^ ask(l);
}
void build(int o, int l, int r) {
if (l == r) {
[o] = l;
pos[o] = 0;
cntreturn;
}
int m = l + r >> 1;
(ls, l, m), build(rs, m + 1, r);
build[o] = a[pos[ls]] > a[pos[rs]] ? pos[ls] : pos[rs];
pos[o] = cnt[ls] + ask(rs, m + 1, r, pos[ls]);
cnt}
void update(int o, int l, int r, int x) {
if (l == r) return;
int m = l + r >> 1;
<= m ? update(ls, l, m, x) : update(rs, m + 1, r, x);
x [o] = a[pos[ls]] > a[pos[rs]] ? pos[ls] : pos[rs];
pos[o] = cnt[ls] + ask(rs, m + 1, r, pos[ls]);
cnt}
int p, s;
void solve(int o, int l, int r, int x, int y) {
if (x <= l && r <= y) {
if (!p) {
= pos[o];
p = cnt[o];
s } else if (a[pos[o]] >= a[p]) {
+= ask(o, l, r, p), p = pos[o];
s }
return;
}
int m = l + r >> 1;
if (x <= m) solve(ls, l, m, x, y);
if (y > m) solve(rs, m + 1, r, x, y);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
for (int i = 1; i <= n; i++) {
std::cin >> a[i];
}
for (int i = 1; i <= n; i++) {
int x;
std::cin >> x;
if (x) add(i, i);
}
(1, 1, n);
buildint q;
std::cin >> q;
while (q--) {
int opt, x, y;
std::cin >> opt >> x >> y;
if (opt == 1) {
[x] = y;
a(1, 1, n, x);
update} else if (opt == 2) {
(x, y);
add(1, 1, n, x);
update(1, 1, n, y);
update} else {
= s = 0;
p (1, 1, n, x, y);
solvestd::cout << s << "\n";
}
}
return 0;
}
分块
// Author: HolyK
// Created: Sun Aug 8 09:01:25 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
constexpr int N(2e5 + 5), T(768);
int n, a[N], b[N], c[N], d[N], f[N], t[N];
std::vector<int> s[N / T + 5];
void build(int x) {
auto &q = s[x];
.clear();
qint l = x * T, r = std::min(n, l + T);
for (int i = r - 1; i >= l; i--) {
while (!q.empty() && a[q.back()] < a[i]) q.pop_back();
if (q.empty()) {
[i] = t[i] = i;
f[i] = 0;
d} else {
[i] = q.back();
f[i] = t[f[i]];
t[i] = d[f[i]] + (b[i] ^ b[f[i]]);
d}
.push_back(i);
q}
std::reverse(q.begin(), q.end());
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
for (int i = 0; i < n; i++) {
std::cin >> b[i];
}
int tot = (n - 1) / T + 1;
for (int i = 0; i < tot; i++) {
(i);
build}
int q;
std::cin >> q;
while (q--) {
int opt, x, y;
std::cin >> opt >> x >> y;
if (opt == 1) {
[--x] = y;
a(x / T);
build} else if (opt == 2) {
--, y--;
xint bx = x / T, by = y / T;
if (bx == by) {
for (int i = x; i <= y; i++) b[i] ^= 1;
(bx);
build} else {
for (int i = x; i < bx * T + T; i++) b[i] ^= 1;
for (int i = by * T; i <= y; i++) b[i] ^= 1;
(bx), build(by);
buildfor (int i = bx + 1; i < by; i++) c[i] ^= 1;
}
} else {
--, y--;
xint ans = 0;
while (x <= y) {
if (x == t[x]) {
int to = y + 1;
for (int i = x / T + 1; i < tot; i++) {
if (a[s[i].back()] < a[x]) continue;
= *std::lower_bound(s[i].begin(), s[i].end(), x, [&](int i, int j) {
to return a[i] < a[j];
});
if (to <= y) {
+= b[x] ^ b[to] ^ c[x / T] ^ c[i];
ans }
break;
}
= to;
x } else if (t[x] <= y) {
+= d[x];
ans = t[x];
x } else if (f[x] <= y) {
+= b[x] ^ b[f[x]];
ans = f[x];
x } else {
break;
}
}
std::cout << ans << "\n";
}
}
return 0;
}
xay loves jumping
xay loves matrices
// Author: HolyK
// Created: Sat Aug 14 10:09:34 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
constexpr int P(998244353);
inline void inc(int &x, int y) {
+= y;
x if (x >= P) x -= P;
}
inline void dec(int &x, int y) {
-= y;
x if (x < 0) x += P;
}
inline int mod(LL x) {
return x % P;
}
int fpow(int x, int k = P - 2) {
int r = 1;
for (; k; k >>= 1, x = 1LL * x * x % P) {
if (k & 1) r = 1LL * r * x % P;
}
return r;
}
struct Z {
int x;
(int v = 0) : x(v < 0 ? v + P : v >= P ? v - P : v) {}
Z() const {
Z invassert(x);
return Z(fpow(x));
}
(int k) const {
Z powreturn Z(fpow(x, k));
}
&operator+=(const Z &r) {
Z (x, r.x);
increturn *this;
}
&operator-=(const Z &r) {
Z (x, r.x);
decreturn *this;
}
&operator*=(const Z &r) {
Z = 1LL * x * r.x % P;
x return *this;
}
&operator/=(const Z &r) {
Z = 1LL * x * fpow(r.x) % P;
x return *this;
}
operator+(const Z &r) const {
Z return Z(*this) += r;
}
operator-(const Z &r) const {
Z return Z(*this) -= r;
}
operator*(const Z &r) const {
Z return Z(*this) *= r;
}
operator/(const Z &r) const {
Z return Z(*this) /= r;
}
operator-() const {
Z return Z(P - x);
}
operator int() const {
return x;
}
};
// det(xI + A)
std::vector<Z> charPoly(std::vector<std::vector<Z>> a) {
int n = a.size();
for (int j = 0; j < n - 2; j++) {
for (int i = j + 1; i < n; i++) {
if (!a[i][j]) continue;
std::swap(a[i], a[j + 1]);
for (int k = 0; k < n; k++) {
std::swap(a[k][i], a[k][j + 1]);
}
break;
}
if (a[j + 1][j]) {
auto inv = a[j + 1][j].inv();
for (int i = j + 2; i < n; i++) {
auto c = a[i][j] * inv;
for (int k = 0; k < n; k++) a[i][k] -= a[j + 1][k] * c;
for (int k = 0; k < n; k++) a[k][j + 1] += a[k][i] * c;
}
}
}
std::vector<std::vector<Z>> h(n + 1);
[0] = {1};
hfor (int i = 0; i < n; i++) {
= 1;
Z prod [i + 1].resize(h[i].size() + 1);
hfor (int j = 0; j < h[i].size(); j++) {
[i + 1][j + 1] = h[i][j];
h[i + 1][j] += h[i][j] * a[i][i];
h}
for (int j = 0; j < i; j++) {
*= -a[i - j][i - j - 1];
prod auto c = a[i - j - 1][i] * prod;
for (int k = 0; k < h[i - j - 1].size(); k++) {
[i + 1][k] += h[i - j - 1][k] * c;
h}
}
}
return h[n];
}
int main() {
// freopen("24.in", "r", stdin);
// freopen("t.out", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, d;
std::cin >> n >> d;
++;
dstd::vector m(d, std::vector(n, std::vector<Z>(n)));
for (int i = 0; i < d; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
std::cin >> m[i][j][k].x;
}
}
}
auto row = [&](int x, int y, Z z) {
for (int i = 0; i < d; i++) {
for (int j = 0; j < n; j++) {
[i][y][j] += m[i][x][j] * z;
m}
}
};
auto col = [&](int x, int y, Z z) {
for (int i = 0; i < d; i++) {
for (int j = 0; j < n; j++) {
[i][j][y] += m[i][j][x] * z;
m}
}
};
int cnt = 0;
= 1;
Z prod auto &h = m[d - 1];
for (int i = 0, j; i < n; i++) {
while (!h[i][i]) {
for (j = i; j < n; j++) {
if (!h[j][i]) continue;
if (i != j) {
*= -1;
prod for (int k = 0; k < d; k++) {
std::swap(m[k][i], m[k][j]);
}
}
break;
}
if (j < n) break;
if (++cnt > n * (d - 1)) {
for (int i = 0; i <= n * (d - 1); i++) {
std::cout << "0" << " \n"[i == n * (d - 1)];
}
return 0;
}
for (j = i - 1; j >= 0; j--) {
if (!h[j][i]) continue;
(j, i, -h[j][i] / h[j][j]);
col}
for (int k = d - 1; k > 0; k--) {
for (j = 0; j < n; j++) {
[k][j][i] = m[k - 1][j][i];
m}
}
for (int j = 0; j < n; j++) {
[0][j][i] = 0;
m}
}
*= h[i][i];
prod = h[i][i].inv();
Z inv for (int k = 0; k < d; k++) {
for (j = 0; j < n; j++) {
[k][i][j] *= inv;
m}
}
for (j = 0; j < n; j++) {
if (j == i || !h[j][i]) continue;
(i, j, -h[j][i]);
row}
}
auto out = [&](auto a) {
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[i].size(); j++) {
std::cerr << a[i][j] << " \n"[j + 1 == a[i].size()];
}
}
};
// out(h);
// out(m[0]);
--;
dstd::vector a(n * d, std::vector<Z>(n * d));
for (int i = 0; i < d; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
[n * (d - 1) + j][n * i + k] = m[i][j][k];
a}
}
}
for (int i = n; i < n * d; i++) {
[i - n][i] = -1;
a}
// out(a);
auto f = charPoly(a);
for (auto &x : f) x *= prod;
std::vector<int> g(n * d + 1);
for (int i = cnt; i < f.size(); i++) g[i - cnt] = f[i];
for (int i = 0; i <= n * d; i++) {
std::cout << g[i] << " \n"[i == n * d];
}
return 0;
}
xay loves nim
xay loves trees
// Author: HolyK
// Created: Sat Aug 7 12:43:24 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
constexpr int N(6e5 + 5);
int n, m, in[N], out[N], cnt;
std::vector<int> g[N], h[N];
std::vector<std::pair<int*, int>> rec;
int max[N << 2], tag[N << 2];
void record(int &x) {
.emplace_back(&x, x);
rec}
#define ls o << 1
#define rs o << 1 | 1
// void pushup(int o) {
// record(max[o]);
// max[o] = std::max({max[ls], max[rs], tag[o]});
// }
void update(int o, int l, int r, int x, int y, int z) {
(max[o]);
record[o] = z;
maxif (x <= l && r <= y) {
(tag[o]);
record[o] = z;
tagreturn;
}
int m = l + r >> 1;
if (x < m) update(ls, l, m, x, y, z);
if (y > m) update(rs, m, r, x, y, z);
}
int ask(int o, int l, int r, int x, int y) {
if (r <= x || y <= l) return 0;
if (x <= l && r <= y) {
return max[o];
}
int m = l + r >> 1;
return std::max({ask(ls, l, m, x, y), ask(rs, m, r, x, y), tag[o]});
}
void dfs2(int x, int p) {
[x] = cnt++;
infor (int y : h[x]) {
if (y == p) continue;
(y, x);
dfs2}
[x] = cnt;
out}
int s[N], top, m2, ans;
int t[1 << 21];
void up(int x) {
for (int i = m2 + x >> 1; i; i >>= 1) {
[i] = std::max(t[i << 1], t[i << 1 | 1]);
t}
}
void dfs1(int x, int p) {
[++top] = x;
s[top + m2] = ask(1, 0, n, in[x], out[x]), up(top);
tint l = 0, r = m2 * 2, rmax = 0;
for (int o = 1; r - l > 1; ) {
int m = l + r >> 1;
if (std::max(rmax, t[o << 1 | 1]) >= m) {
= o << 1 | 1;
o = m;
l } else {
(rmax, t[o << 1 | 1]);
smax= o << 1;
o = m;
r }
}
if (rmax >= l) l++;
// std::cerr << x << " " << top - l + 1 << "\n";
(ans, top - l + 1);
smaxint now = rec.size();
(1, 0, n, in[x], out[x], top);
updatefor (int y : g[x]) {
if (y == p) continue;
(y, x);
dfs1}
[top + m2] = 0, up(top);
t--;
topwhile (rec.size() > now) {
*rec.back().first = rec.back().second;
.pop_back();
rec}
}
void solve() {
std::cin >> n;
for (int i = 1; i <= n; i++) {
[i].clear();
g[i].clear();
h}
= ans = 0;
cnt for (int i = 1, x, y; i < n; i++) {
std::cin >> x >> y;
[x].push_back(y);
g[y].push_back(x);
g}
for (int i = 1, x, y; i < n; i++) {
std::cin >> x >> y;
[x].push_back(y);
h[y].push_back(x);
h}
= 1 << std::__lg(n * 2 - 1);
m = 1 << std::__lg(n * 2 + 1);
m2 (1, 0);
dfs2// std::cerr << "dfs2 end\n";
(1, 0);
dfs1std::cout << ans << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
();
solve}
return 0;
}
xay loves KDT
xay loves count
// Author: HolyK
// Created: Sat Aug 7 12:06:08 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
int c[1000006];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
for (int i = 1; i <= n; i++) {
int x;
std::cin >> x;
[x]++;
c}
= 1e6;
n = 0;
LL ans for (int i = 1; i <= n; i++) {
for (int j = 1; i * j <= n; j++) {
int k = i * j;
+= 1LL * c[i] * c[j] * c[k];
ans
}
}
std::cout << ans << "\n";
return 0;
}
xay loves or
签到。
xay loves Floyd
xay loves sequence
// Author: HolyK
// Created: Sat Aug 7 14:36:07 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
constexpr int N(2e5 + 5), K((1 << 30) - 1);
int n, q, a[N], b[N];
void brute() {
while (q--) {
int l, r, k;
std::cin >> l >> r >> k;
std::vector<int> c {a[l]};
for (int i = l + 1; i <= r; i++) {
.push_back((b[i] + k) % k);
c}
std::sort(c.begin(), c.end());
std::vector<LL> pre(r - l + 1);
for (int i = 0; i < r - l + 1; i++) {
[i] = c[i];
pre}
for (int i = 1; i < r - l + 1; i++) {
[i] += pre[i - 1];
pre}
= std::min(pre.back(), 1LL * (r - l + 1) * k - pre.back());
LL ans // std::cerr << pre.back() << " ";
for (int x = 0; x < r - l + 1; x++) {
// std::cerr << std::max(pre[x], 1LL * (r - l - x) * k - pre.back() + pre[x]) << " \n"[x == r - l];
(ans, std::max(pre[x], 1LL * (r - l - x) * k - pre.back() + pre[x]));
smin}
std::cout << ans << "\n";
// return;
}
}
struct Node {
int ls, rs, cnt;
;
LL sum} t[N * 60];
int cur;
void ins(int &o, int l, int r, int x, int y) {
[++cur] = t[o], o = cur;
t[o].cnt++;
t[o].sum += y;
tif (r - l == 1) return;
int m = l + r >> 1;
< m ? ins(t[o].ls, l, m, x, y) : ins(t[o].rs, m, r, x, y);
x }
int cnt;
;
LL sumvoid ask(int p, int q, int l, int r, int x, int y) {
// std::cerr << " ?? " << l << " " << r << " " << x << " " << y << "\n";
if (r <= x || y <= l || t[p].cnt - t[q].cnt == 0) return;
if (x <= l && r <= y) {
+= t[p].cnt - t[q].cnt;
cnt += t[p].sum - t[q].sum;
sum return;
}
int m = l + r >> 1;
if (x < m) ask(t[p].ls, t[q].ls, l, m, x, y);
if (y > m) ask(t[p].rs, t[q].rs, m, r, x, y);
}
int root[N][2];
[N];
LL negSumint negCnt[N];
std::vector<int> c[2];
signed main() {
// freopen("k.in", "r", stdin);
// freopen("k.out", "w", stdout);
double sta = (double)clock() / CLOCKS_PER_SEC;
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n >> q;
for (int i = 1; i <= n; i++) std::cin >> a[i], b[i] = a[i];
for (int i = n; i; i--) b[i] -= b[i - 1];
for (int i = 2; i <= n; i++) {
[i] = negSum[i - 1];
negSum[i] = negCnt[i - 1];
negCntif (b[i] < 0) {
[i] += b[i], negCnt[i]++;
negSum}
[b[i] >= 0].push_back(b[i]);
c}
for (auto &v : c) {
std::sort(v.begin(), v.end());
.resize(std::unique(v.begin(), v.end()) - v.begin());
v}
auto lb = [&](int i, int x) {
int l = 0, r = c[i].size();
while (l < r) {
int m = l + r >> 1;
if (c[i][m] < x) {
= m + 1;
l } else {
= m;
r }
}
return l;
};
for (int i = 2; i <= n; i++) {
[i][0] = root[i - 1][0];
root[i][1] = root[i - 1][1];
rootint k = b[i] >= 0;
(root[i][k], 0, c[k].size(), lb(k, b[i]), b[i]);
ins}
int pos0 = lb(1, 0), posk;
while (q--) {
int l, r, k;
std::cin >> l >> r >> k;
= lb(0, -k);
posk = a[r] + 1LL * (negCnt[r] - negCnt[l]) * k;
LL s int n = r - l + 1;
auto cal = [&](int x) {
int pre = n - x;
int low = 0, high = k;
while (low < high) {
int mid = low + high >> 1;
= 0;
cnt (root[r][0], root[l][0], 0, c[0].size(), posk, lb(0, mid - k + 1));
ask(root[r][1], root[l][1], 0, c[1].size(), pos0, lb(1, mid + 1));
askif (a[l] <= mid) cnt++;
if (cnt < pre) {
= mid + 1;
low } else {
= mid;
high }
}
// std::cerr << "??\n";
= 0;
cnt = 0;
sum (root[r][0], root[l][0], 0, c[0].size(), posk, lb(0, low - k));
ask+= 1LL * cnt * k;
sum (root[r][1], root[l][1], 0, c[1].size(), pos0, lb(1, low));
askif (a[l] < low) cnt++, sum += a[l];
assert(cnt <= n - x);
+= 1LL * (n - x - cnt) * low;
sum // std::cerr << n - x << " " << low << " " << cnt << "\n";
return sum + std::max(0LL, 1LL * x * k - s);
};
= (s + k - 1) / k;
LL pos (pos, r - l + 1);
smin// std::cerr << "pos : " << pos << "\n";
= cal(pos);
LL ans
if (pos) smin(ans, cal(pos - 1));
std::cout << ans << "\n";
}
std::cerr << "time : " << (double)clock() / CLOCKS_PER_SEC - sta << "\n";
return 0;
}
最后修改于 2021-08-19