結果

問題 No.877 Range ReLU Query
ユーザー gasingasin
提出日時 2019-09-07 16:09:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,335 ms / 2,000 ms
コード長 2,797 bytes
コンパイル時間 1,240 ms
コンパイル使用メモリ 80,460 KB
実行使用メモリ 241,744 KB
最終ジャッジ日時 2024-04-25 22:11:15
合計ジャッジ時間 13,869 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 4 ms
6,940 KB
testcase_02 AC 4 ms
6,940 KB
testcase_03 AC 5 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 6 ms
6,940 KB
testcase_09 AC 3 ms
6,948 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 1,232 ms
191,308 KB
testcase_12 AC 1,141 ms
186,576 KB
testcase_13 AC 853 ms
143,056 KB
testcase_14 AC 853 ms
129,232 KB
testcase_15 AC 1,335 ms
235,856 KB
testcase_16 AC 1,268 ms
229,584 KB
testcase_17 AC 1,283 ms
234,828 KB
testcase_18 AC 1,289 ms
237,136 KB
testcase_19 AC 787 ms
241,616 KB
testcase_20 AC 1,020 ms
241,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string.h>
#define rep(i,n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
double PI = 3.1415926535897932;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

// 区間和、要素更新のsegtree

template<typename T>
struct permanent_segtree {
    struct NODE {
        T val;
        struct NODE *l, *r;
    };

    int n;
    vector<NODE*> roots;

    void init(int n_){
        n = 1;
        while(n < n_) n *= 2;
        vector<NODE*> nodes;
        rep(i,2*n-1) {
            NODE* nn = new NODE();
            nn->val = 0;
            nodes.push_back(nn);
        }
        rep(i,2*n-1) {
            if (i < n-1) {
                nodes[i]->l = nodes[2*i+1];
                nodes[i]->r = nodes[2*i+2];
            }
        }
        roots.push_back(nodes[0]);
    }

    void app(int a, T x, NODE* root, int l, int r) {
        int b = a+1;
        if (r <= a || b <= l) return;
        if (a <= l && r <= b) {
            root->val = x;
            return;
        }
        NODE* nl = new NODE(*(root->l));
        NODE* nr = new NODE(*(root->r));
        root->r = nr;
        root->l = nl;
        app(a, x, nl, l, (l+r)/2);
        app(a, x, nr, (l+r)/2, r);
        root->val = nr->val + nl->val;
    }

    void update(int k, T a){
        NODE* nr = new NODE(*(roots.back()));
        roots.push_back(nr);

        app(k, a, nr, 0, n);
    }

    T sub_query(int a, int b, NODE* root, int l, int r){
        if(r <= a || b <= l) return 0;
        if(a <= l && r <= b) return root->val;

        T vl = sub_query(a,b,root->l,l,(l+r)/2);
        T vr = sub_query(a,b,root->r,(l+r)/2,r);
        return vl + vr;
    }

    T query (int a, int b, int t) {
        return sub_query(a, b, roots[t], 0, n);
    }
};

permanent_segtree<ll> seg_v, seg_c;
int n, q;
pl a[100000];

int main() {
    cin >> n >> q;
    seg_v.init(n);
    seg_c.init(n);
    rep(i,n) {
        cin >> a[i].first;
        a[i].second = i;
    }
    sort(a, a+n, greater<pl>());
    rep(i,n) {
        seg_v.update(a[i].second, a[i].first);
        seg_c.update(a[i].second, 1);
    }
    rep(i,q) {
        ll f, l, r, x;
        cin >> f >> l >> r >> x;
        l--; r--;
        int st = -1, en = n; 
        while (en-st > 1) {
            int mid = (st + en) / 2;
            if (a[mid].first < x) en = mid;
            else st = mid;
        }
        ll cnt = seg_c.query(l, r+1, st+1);
        ll val = seg_v.query(l, r+1, st+1);
        cout << val - cnt*x << endl;
    }
}
0