結果
問題 | No.2101 [Cherry Alpha N] ずっとこの数列だったらいいのに |
ユーザー | Manuel1024 |
提出日時 | 2022-12-11 07:53:01 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 649 ms / 6,000 ms |
コード長 | 2,275 bytes |
コンパイル時間 | 959 ms |
コンパイル使用メモリ | 82,596 KB |
実行使用メモリ | 19,748 KB |
最終ジャッジ日時 | 2024-10-15 06:05:29 |
合計ジャッジ時間 | 26,747 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 42 |
ソースコード
#include <iostream> #include <vector> #include <algorithm> using namespace std; using ll = long long; struct P{ int t; int op; int id; P(int t, int op, int id): t(t), op(op), id(id){} }; template <typename T> struct FenwickTree{ const int n; vector<T> arr; FenwickTree() = default; FenwickTree(int n): n(n), arr(vector<T>(n+1, 0)){} void add(int ind, T x){ for(int i = ind+1; i <= n; i += i & (-i)){ arr[i] += x; } } T sum_sub(int end){ T res = 0; for(int i = end; i > 0; i -= i & (-i)){ res += arr[i]; } return res; } T sum(int start, int end){ return sum_sub(end) - sum_sub(start); } int lower_bound(T w){ if(w <= 0) return 0; int x = 0; int k = 1 << 30; while(k > n) k /= 2; for(; k > 0; k /= 2){ if(x+k <= n && arr[x+k] < w){ w -= arr[x+k]; x += k; } } return x; } }; int main(){ int n; cin >> n; vector<int> a(n), t(n); for(int i = 0; i < n; i++) cin >> a[i] >> t[i]; int q; cin >> q; vector<int> d(q), l(q), r(q); for(int i = 0; i < q; i++){ cin >> d[i] >> l[i] >> r[i]; l[i]--; } vector<P> queries; for(int i = 0; i < n; i++){ if(a[i] == 0) continue; queries.emplace_back(t[i], 0, i); queries.emplace_back(a[i]+t[i]-1, 1, i); } for(int i = 0; i < q; i++) queries.emplace_back(d[i], 2, i); sort(queries.begin(), queries.end(), [](const P &x, const P &y){ if(x.t != y.t) return x.t < y.t; else return x.op < y.op; }); vector<ll> ans(q); FenwickTree<int> cnt(n); FenwickTree<ll> sum(n); for(int i = 0; i < n; i++) sum.add(i, a[i]); for(auto &it: queries){ if(it.op == 0){ sum.add(it.id, t[it.id]-1); cnt.add(it.id, 1); }else if(it.op == 1){ sum.add(it.id, -sum.sum(it.id, it.id+1)); cnt.add(it.id, -1); }else{ ans[it.id] = sum.sum(l[it.id], r[it.id]); ans[it.id] -= 1LL*it.t*cnt.sum(l[it.id], r[it.id]); } } for(auto &it: ans) cout << it << '\n'; return 0; }