結果
問題 | No.877 Range ReLU Query |
ユーザー |
|
提出日時 | 2020-02-05 22:16:54 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 420 ms / 2,000 ms |
コード長 | 2,725 bytes |
コンパイル時間 | 1,264 ms |
コンパイル使用メモリ | 122,884 KB |
実行使用メモリ | 64,384 KB |
最終ジャッジ日時 | 2024-11-08 10:24:21 |
合計ジャッジ時間 | 6,302 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 20 |
ソースコード
#include <cstdio> #include <iostream> #include <string> #include <sstream> #include <stack> #include <algorithm> #include <cmath> #include <queue> #include <map> #include <set> #include <cstdlib> #include <bitset> #include <tuple> #include <assert.h> #include <deque> #include <bitset> #include <iomanip> #include <limits> #include <chrono> #include <random> #include <array> #include <unordered_map> #include <functional> #include <complex> template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long MAX = 5100000; const long long INF = 1LL << 60; const long long mod = 1000000007LL; //const long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; struct SegmentTree { private: int n; vector<vector<ll>> node; vector<vector<ll>> sum; public: SegmentTree(vector<ll> v) { int sz = v.size(); n = 1; while (sz > n) n *= 2; node.resize(2 * n - 1); sum.resize(2 * n); for (ll i = 0; i < sz; i++) { node[i + n - 1].push_back(v[i]); } for (ll i = sz; i + n - 1 < node.size(); i++) { node[i + n - 1].push_back(0); } for (ll i = n - 2; i >= 0; i--) { node[i].resize(node[i * 2 + 1].size() + node[i * 2 + 2].size()); merge(node[i * 2 + 1].begin(), node[i * 2 + 1].end(), node[i * 2 + 2].begin(), node[i * 2 + 2].end(), node[i].begin()); } for (ll i = 0; i < node.size(); i++) { sum[i].resize(node[i].size() + 1); for (ll j = 0; j < node[i].size();j++) { sum[i][j + 1] = sum[i][j] + node[i][j]; } } } ll query(ll a, ll b, ll v, ll k = 0, ll l = 0, ll r = -1) { if (r < 0) r = n; if (r <= a || b <= l) return 0; if (a <= l && r <= b) { ll idx = lower_bound(node[k].begin(), node[k].end(), v) - node[k].begin(); return sum[k].back() - sum[k][idx] - (node[k].size() - idx) * v; } ll vl = query(a, b, v, 2 * k + 1, l, (l + r) / 2); ll vr = query(a, b, v, 2 * k + 2, (l + r) / 2, r); return vl + vr; } }; int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ ll n, q; scanf("%lld %lld", &n, &q); vector<ll> a(n); for (ll i = 0; i < n; i++) scanf("%lld", &a[i]); SegmentTree seg(a); while (q--) { ll t; scanf("%lld", &t); if (t == 1) { ll l, r, x; scanf("%lld %lld %lld", &l, &r, &x); l--; printf("%lld\n", seg.query(l, r, x)); } } return 0; }