結果

問題 No.877 Range ReLU Query
ユーザー 👑 jupirojupiro
提出日時 2020-02-05 22:16:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 398 ms / 2,000 ms
コード長 2,725 bytes
コンパイル時間 1,265 ms
コンパイル使用メモリ 122,856 KB
実行使用メモリ 64,440 KB
最終ジャッジ日時 2024-04-25 22:18:51
合計ジャッジ時間 5,860 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 360 ms
63,924 KB
testcase_12 AC 319 ms
64,016 KB
testcase_13 AC 230 ms
33,024 KB
testcase_14 AC 243 ms
33,108 KB
testcase_15 AC 380 ms
64,344 KB
testcase_16 AC 386 ms
64,380 KB
testcase_17 AC 398 ms
64,368 KB
testcase_18 AC 391 ms
64,324 KB
testcase_19 AC 170 ms
64,440 KB
testcase_20 AC 315 ms
64,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0