結果

問題 No.877 Range ReLU Query
ユーザー 438kujira438kujira
提出日時 2019-09-26 22:45:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 382 ms / 2,000 ms
コード長 2,984 bytes
コンパイル時間 2,158 ms
コンパイル使用メモリ 180,108 KB
実行使用メモリ 17,792 KB
最終ジャッジ日時 2024-04-25 22:16:15
合計ジャッジ時間 7,085 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 4 ms
6,944 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,940 KB
testcase_08 AC 4 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 362 ms
17,024 KB
testcase_12 AC 324 ms
16,000 KB
testcase_13 AC 261 ms
12,416 KB
testcase_14 AC 282 ms
12,544 KB
testcase_15 AC 378 ms
17,408 KB
testcase_16 AC 353 ms
17,280 KB
testcase_17 AC 360 ms
17,152 KB
testcase_18 AC 382 ms
17,152 KB
testcase_19 AC 323 ms
17,320 KB
testcase_20 AC 368 ms
17,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define LLINF 9223372036854775807
#define MOD ll(1e9+7)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cerr<<#x<<": "<<x<<endl

vector<ll> argsort(const vector<ll> &v) {
      vector<ll> index(v.size());
      iota(index.begin(), index.end(), 0);
      sort(index.begin(), index.end(),[&v](size_t i1, size_t i2) { return v[i1] < v[i2]; });
      return index;
}


class segtree{
public:
    segtree(ll size, vector<ll> &v){
        n = 1;
        while(n < size){n *= 2;}
        data = vector<ll>(2*n-1, 0);
        build(v);
    }
    void build(vector<ll> &v){
        for(int i = 0; i < v.size(); i++){
            data[n-1+i] = v[i];
        }
        for(int i = n-2; i >= 0; i--){
            data[i] = data[2*i+1] + data[2*i+2];
        }
    }
    void update(ll k, ll a){
        k += n-1;
        data[k] = a;
        while(k>0){
            k = (k-1)/2;
            data[k] = data[2*k+1] + data[2*k+2];
        }
    }
    ll getSize(){return n;}
    ll getData(ll x){return data[n-1+x];}
    ll query(ll a, ll b, ll k, ll l, ll r){
        if(r <= a || b <= l){return 0;}
        if(a <= l && r <= b){return data[k];}
        else{
            ll vl = query(a, b, 2*k+1, l, (l+r)/2);
            ll vr = query(a, b, 2*k+2, (l+r)/2, r);
            return vl + vr;
        }
    }
    void printAllTree(){
        ll cnt = 0;
        ll br = 1;
        for(int i = 0; i < data.size(); i++){
            cout << data[i] << " ";
            cnt++;
            if(cnt == br){
                cout << endl;
                cnt = 0;
                br *= 2;
            }
        }
    }
private:
    ll n;
    vector<ll> data;
    ll MAX_NUM = 1LL << 60;
};


int main(){
    ll n, q;
    cin >> n >> q;
    vector<ll> a(n);
    vector<ll> alive(n,0);
    for(int i = 0; i < n; i++){
        cin >> a[i];
        if(a[i] > 0){alive[i] = 1;}
    }
    segtree st(n, a);
    segtree st_alive(n, alive);

    vector<ll> l(q), r(q), x(q);
    ll t;
    for(int i = 0; i < q; i++){
        cin >> t >> l[i] >> r[i] >> x[i];
        l[i]--; r[i]--;
    }
    vector<ll> order_x = argsort(x);
    vector<ll> sorted = x;
    sort(all(sorted));
    vector<vector<ll>> deadline(q+1,vector<ll>());
    for(int i = 0; i < n; i++){
        auto itr = lower_bound(all(sorted),a[i]);
        deadline[itr-sorted.begin()].push_back(i);
    }

    vector<ll> ans(q,0);
    for(int i = 0; i < q; i++){
        for(int j = 0; j < deadline[i].size(); j++){
            st.update(deadline[i][j], 0);
            st_alive.update(deadline[i][j],0);
        }
        ll ret = st.query(l[order_x[i]], r[order_x[i]]+1, 0, 0, st.getSize());
        ll ret_alive = st_alive.query(l[order_x[i]], r[order_x[i]]+1, 0, 0, st.getSize());
        ans[order_x[i]] = ret - ret_alive*sorted[i];
    }
    for(int i = 0; i < q; i++){
        cout << ans[i] << endl;
    }
    return 0;
}
0