C
dfs。
。
#include "bits/stdc++.h"
using namespace std;
using i64 = int64_t;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<int>> g(n);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
vector<i64> sum(n), d(n), ans(n);
function<void(int, int, int)> dfs = [&](int x, int p, int dep) {
sum[x] = a[x];
for (auto y : g[x]) {
if (y != p) {
dfs(y, x, dep + 1);
sum[x] += sum[y];
}
}
if (dep - k > 0) {
d[dep - k - 1] += sum[x];
}
ans[x] += sum[x] - d[dep];
d[dep] = 0;
};
dfs(0, -1, 0);
for (int i = 0; i < n; i++) {
cout << ans[i] << " \n"[i == n - 1];
}
return 0;
}
D
单调栈,前缀和。
。
#include "bits/stdc++.h"
using namespace std;
using i64 = int64_t;
constexpr int P = 998244353;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
auto work = [&]() {
vector<i64> sum0(n + 1), sum1(n + 2);
for (int i = 0; i < n; i++) {
sum0[i + 1] = ((sum0[i] + a[i]) % P + P) % P;
}
for (int i = 0; i <= n; i++) {
sum1[i + 1] = ((sum1[i] + sum0[i]) % P + P) % P;
}
vector<int> l(n), r(n), st;
for (int i = 0; i < n; i++) {
while (!st.empty() && a[st.back()] < a[i]) {
st.pop_back();
}
l[i] = st.empty() ? -1 : st.back();
st.push_back(i);
}
st = {};
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.back()] <= a[i]) {
st.pop_back();
}
r[i] = st.empty() ? n : st.back();
st.push_back(i);
}
i64 res = 0;
for (int i = 0; i < n; i++) {
i64 v = (a[i] % P + P) % P;
i64 sum = ((sum1[r[i] + 1] - sum1[i + 1] + P) % P * (i - l[i]) % P - (sum1[i + 1] - sum1[l[i] + 1] + P) % P * (r[i] - i) % P + P) % P;
res = (res + v * sum % P) % P;
}
return res;
};
i64 ans0 = work();
for (int i = 0; i < n; i++) {
a[i] = -a[i];
}
i64 ans1 = work();
cout << (ans0 - ans1 + P) % P << '\n';
return 0;
}
E
对每个 合法区间 加起来。
。
#include "bits/stdc++.h"
using namespace std;
using i64 = int64_t;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, l, r;
cin >> n >> l >> r;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
i64 ans = 0;
for (int i = 0; i < n; i++) {
int li = lower_bound(a.begin(), a.end(), l - a[i]) - a.begin();
int ri = upper_bound(a.begin(), a.end(), r - a[i]) - a.begin();
ans += ri - li - (li <= i && i < ri);
}
cout << ans / 2 << '\n';
return 0;
}
F
。
#include "bits/stdc++.h"
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> sum(n + 1);
for (int i = 0; i < n; i++) {
string s;
cin >> s;
sum[i + 1] = sum[i] + (s == "byl");
}
int ans = 0;
for (int l = 0; l < n; l++) {
for (int r = l + 1; r <= n; r++) {
ans += (r - l - sum[r] + sum[l] < sum[r] - sum[l]);
}
}
cout << ans << '\n';
return 0;
}
G
使用 。
。
#include "bits/stdc++.h"
using namespace std;
using i64 = int64_t;
#include "ext/pb_ds/assoc_container.hpp"
using namespace __gnu_pbds;
template <typename Key, typename Cmp_Fn = less<Key>>
class Set : public tree<Key, null_type, Cmp_Fn, rb_tree_tag,
tree_order_statistics_node_update> {};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
int x = 0, y = 0;
Set<pair<i64, int>> s;
s.insert({0, 0});
i64 ans = 0;
for (int i = 0; i < n; i++) {
string t;
cin >> t;
t == "byl" ? x++ : y++;
ans += s.order_of_key({x - i64(k) * y, -1});
s.insert({x - i64(k) * y, i + 1});
}
cout << ans << '\n';
return 0;
}
H
构造的数组只含有 。
。
#include "bits/stdc++.h"
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k, s;
cin >> n >> k >> s;
if (n > 1 && k == 1) {
cout << "NO\n";
return 0;
}
vector<int> a(n);
for (int i = 0; i < n; i += k) {
for (int j = 0; j < k && i + j < n; j++) {
a[i + j] = (j % 2) + 1;
}
if (k % 2 == 1 && i > 1) {
a[i - 1] = 3;
}
}
if (accumulate(a.begin(), a.end(), 0LL) > s) {
cout << "NO\n";
return 0;
}
cout << "YES\n";
for (int i = 0; i < n; i++) {
cout << a[i] << " \n"[i == n - 1];
}
return 0;
}
J
全排列。
#include "bits/stdc++.h"
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector a(7, vector<int>(6));
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 6; j++) {
cin >> a[i][j];
a[i][j]--;
}
}
vector<int> p(7);
iota(p.begin(), p.end(), 0);
do {
vector<int> b;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if (a[p[i]][j] == a[p[6]][i]) {
b.push_back(a[p[i]][(j + 1) % 6]);
b.push_back(a[p[i]][(j - 1 + 6) % 6]);
break;
}
}
}
int cnt = 0;
for (int i = 0; i < 12; i++) {
cnt += (b[i] == b[(i + 1) % 12]);
}
if (cnt == 6) {
cout << "YES\n";
return 0;
}
} while (next_permutation(p.begin(), p.end()));
cout << "NO\n";
return 0;
}