結果

問題 No.877 Range ReLU Query
ユーザー gasingasin
提出日時 2019-09-07 16:17:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 921 ms / 2,000 ms
コード長 2,854 bytes
コンパイル時間 984 ms
コンパイル使用メモリ 81,052 KB
実行使用メモリ 182,096 KB
最終ジャッジ日時 2024-04-25 22:10:57
合計ジャッジ時間 10,117 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 5 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 850 ms
144,332 KB
testcase_12 AC 751 ms
140,624 KB
testcase_13 AC 570 ms
108,236 KB
testcase_14 AC 590 ms
97,872 KB
testcase_15 AC 921 ms
177,868 KB
testcase_16 AC 871 ms
173,012 KB
testcase_17 AC 896 ms
177,104 KB
testcase_18 AC 915 ms
178,764 KB
testcase_19 AC 625 ms
182,096 KB
testcase_20 AC 745 ms
182,096 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;

pl operator+(const pl &l, const pl &r) {
    return pl(l.first+r.first, l.second+r.second);
}

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

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

    int n;
    T unit;
    vector<NODE*> roots;

    void init(int n_, T unit_){
        n = 1;
        unit = unit_;
        while(n < n_) n *= 2;
        vector<NODE*> nodes;
        rep(i,2*n-1) {
            NODE* nn = new NODE();
            nn->val = unit;
            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 unit;
        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<pl> seg;
int n, q;
pl a[100000];

int main() {
    cin >> n >> q;
    seg.init(n, pl(0,0));
    rep(i,n) {
        cin >> a[i].first;
        a[i].second = i;
    }
    sort(a, a+n, greater<pl>());
    rep(i,n) {
        seg.update(a[i].second, pl(a[i].first, 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;
        }
        pl val = seg.query(l, r+1, st+1);
        cout << val.first - val.second*x << endl;
    }
}
0