結果

問題 No.877 Range ReLU Query
ユーザー gasingasin
提出日時 2019-09-07 14:16:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 384 ms / 2,000 ms
コード長 2,187 bytes
コンパイル時間 917 ms
コンパイル使用メモリ 77,348 KB
実行使用メモリ 11,224 KB
最終ジャッジ日時 2024-04-25 22:10:39
合計ジャッジ時間 5,957 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 4 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 374 ms
10,836 KB
testcase_12 AC 330 ms
10,708 KB
testcase_13 AC 262 ms
8,632 KB
testcase_14 AC 277 ms
7,936 KB
testcase_15 AC 384 ms
11,220 KB
testcase_16 AC 359 ms
11,092 KB
testcase_17 AC 365 ms
11,096 KB
testcase_18 AC 359 ms
11,064 KB
testcase_19 AC 323 ms
11,224 KB
testcase_20 AC 348 ms
11,220 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 segtree {
    int n;
    vector<T> dat;

    void init(int n_){
        n = 1;
        while(n < n_) n *= 2;
        dat.assign(2*n-1, 0);
    }

    void update(int k, T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = dat[k*2+1] + dat[k*2+2];
        }
    }

    //(a,b,0,0,seg.n)で呼ぶ
    T query(int a, int b, int k, int l, int r){
        if(r <= a || b <= l) return 0;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a,b,k*2+1,l,(l+r)/2);
            T vr = query(a,b,k*2+2,(l+r)/2,r);
            return vl + vr;
        }
    }
};

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

struct Q {
    int l, r, x, i;

    bool operator>(const Q &r) const {
        return x > r.x;
    }
} qu[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,q) {
        ll f, l, r, x;
        cin >> f >> l >> r >> x;
        l--; r--;
        qu[i].l = l; qu[i].r = r; qu[i].x = x; qu[i].i = i;
    }
    sort(qu, qu+q, greater<Q>());
    int pos = 0;
    rep(i,q) {
        while (pos < n && qu[i].x <= a[pos].first) {
            seg_c.update(a[pos].second, 1);
            seg_v.update(a[pos].second, a[pos].first);
            pos++;
        }
        ll cnt = seg_c.query(qu[i].l, qu[i].r+1, 0, 0, seg_c.n);
        ll val = seg_v.query(qu[i].l, qu[i].r+1, 0, 0, seg_v.n);
        ans[qu[i].i] = val - cnt*qu[i].x;
    }
    rep(i,q) {
        cout << ans[i] << endl;
    }
}
0