結果
問題 | No.877 Range ReLU Query |
ユーザー |
![]() |
提出日時 | 2020-03-28 09:08:54 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 787 ms / 2,000 ms |
コード長 | 2,061 bytes |
コンパイル時間 | 2,814 ms |
コンパイル使用メモリ | 116,120 KB |
最終ジャッジ日時 | 2025-01-09 10:39:08 |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 20 |
ソースコード
#include <iostream> #include <vector> #include <queue> #include <map> using namespace std; using ll = long long; using pii = pair<ll, ll>; #define rep(i,n) for(int i=0;i<(n);++i) #include <functional> template<class T> struct SegTree { private: int size; vector<T> v; using F = function<T(T,T)>; F f; T default_value; public: SegTree(int n, T default_value, F f) : default_value(default_value), f(f) { size = 1; while (size < n) size *= 2; v.resize(2 * size - 1, default_value); } void update(int index, T val) { index = index + size - 1; v[index] = val; while (index > 0) { index = (index - 1) / 2; v[index] = f(v[2 * index + 1], v[2 * index + 2]); } } T query(int l, int r) { return query(l, r, 0, size, 0); } T query(int l, int r, int cur_l, int cur_r, int index) { // [l, r)に対するクエリ if (cur_r <= l || r <= cur_l) return default_value; if (l <= cur_l && cur_r <= r) return v[index]; int mid = (cur_l + cur_r) / 2; return f(query(l, r, cur_l, mid, 2 * index + 1), query(l, r, mid, cur_r, 2 * index + 2)); } }; int main() { int N; cin >> N; int Q; cin >> Q; priority_queue<pair<pii, int>> q; rep(i, N) { int a; cin >> a; q.emplace(pii(a, 0), i); } map<int, vector<pii>> m; rep(i, Q) { int t; cin >> t; int l, r, x; cin >> l >> r >> x; --l; q.emplace(pii(x, 1), i); m[x].emplace_back(l, r); } SegTree<ll> T(N, 0, [](ll a, ll b){return a + b;}); SegTree<ll> T2(N, 0, [](ll a, ll b){return a + b;}); vector<ll> ans(Q); while (!q.empty()) { auto p = q.top(); q.pop(); if (p.first.second == 0) { T.update(p.second, p.first.first); T2.update(p.second, 1); } else { int l = m[p.first.first].back().first, r = m[p.first.first].back().second; m[p.first.first].pop_back(); ans[p.second] = T.query(l, r) - T2.query(l, r) * p.first.first; } } rep(i, Q) cout << ans[i] << endl; return 0; }