for (int i = 0; i < n; i++) { for (auto [l, r, t] : f[i]) { seg.rangeApply(l, r + 1, t); } auto info = seg.rangeQuery(0, n); if (info.min == 0) { ans += info.cnt - i; } }
std::cout << ans << "\n";
F. SumofNumbers
尽量均分,余数小于等于 $k$,然后 dp 一下就可以了。
H. AnotherGooseGooseDuckProblem
1 2 3 4 5
voidsolve(){ int l, r, b, k; cin >> l >> r >> b >> k; cout << 1ll * (b * ((l + b - 1) / b)) * k << "\n"; }
using i64 = longlong; template <typename T> structFenwick { int n; std::vector<T> a; Fenwick(int n = 0) { init(n); } voidinit(int n){ this->n = n; a.assign(n, T()); } voidadd(int x, T v){ for (int i = x + 1; i <= n; i += i & -i) { a[i - 1] += v; } } T sum(int x){ auto ans = T(); for (int i = x; i > 0; i -= i & -i) { ans += a[i - 1]; } return ans; } T rangeSum(int l, int r){ returnsum(r) - sum(l); } intkth(T k){ int x = 0; for (int i = 1 << std::__lg(n); i; i /= 2) { if (x + i <= n && k >= a[x + i - 1]) { x += i; k -= a[x - 1]; } } return x; } };
voidsolve(){ int n, m, k; std::cin >> n >> m >> k; std::vector<int> a(n), b(m), pos(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; a[i]--; pos[a[i]] = i; } for (int i = 0; i < m; i++) { std::cin >> b[i]; b[i]--; } std::vector<int> len(k); for (int i = 0; i < k; i++) { std::cin >> len[i]; } std::sort(len.begin(), len.end()); std::multiset s(len.begin(), len.end()); std::vector<int> del, l(n), r(n); std::vector<bool> isdel(n); for (int i = 0, j = 0; i < n; i++) { if (j < m && a[i] == b[j]) { j++; } else { isdel[i] = true; del.push_back(a[i]); } } if (del.size() != n - m) { std::cout << "NO\n"; return; } std::vector<int> stk{-1}; for (int i = 0; i < n; i++) { int lo = 0, hi = stk.size() - 1; while (lo < hi) { int x = (lo + hi + 1) / 2; if (a[i] > a[stk[x]]) { hi = x - 1; } else { lo = x; } } l[i] = stk[lo]; if (!isdel[i]) { stk.resize(lo + 1); stk.push_back(i); } } stk = {n}; for (int i = n - 1; i >= 0; i--) { int lo = 0, hi = stk.size() - 1; while (lo < hi) { int x = (lo + hi + 1) / 2; if (a[i] > a[stk[x]]) { hi = x - 1; } else { lo = x; } } r[i] = stk[lo]; if (!isdel[i]) { stk.resize(lo + 1); stk.push_back(i); } } std::sort(del.begin(), del.end()); Fenwick<int> fen(n); for (int i = 0; i < n; i++) { fen.add(i, 1); } for (int i = int(del.size()) - 1; i >= 0; i--) { int j = pos[del[i]]; int cnt = fen.rangeSum(l[j] + 1, r[j]); auto it = s.upper_bound(cnt); if (it == s.begin()) { std::cout << "NO\n"; return; } s.erase(std::prev(it)); fen.add(j, -1); } std::cout << "YES\n"; }
intmain(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int t; std::cin >> t; while (t--) { solve(); } return0; }