結果

問題 No.877 Range ReLU Query
ユーザー どららどらら
提出日時 2019-09-07 13:24:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 344 ms / 2,000 ms
コード長 2,250 bytes
コンパイル時間 2,089 ms
コンパイル使用メモリ 179,608 KB
実行使用メモリ 13,036 KB
最終ジャッジ日時 2024-04-25 22:10:46
合計ジャッジ時間 6,614 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 4 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 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 329 ms
12,508 KB
testcase_12 AC 289 ms
12,428 KB
testcase_13 AC 232 ms
8,608 KB
testcase_14 AC 251 ms
8,764 KB
testcase_15 AC 344 ms
12,820 KB
testcase_16 AC 320 ms
13,036 KB
testcase_17 AC 326 ms
12,720 KB
testcase_18 AC 324 ms
12,712 KB
testcase_19 AC 327 ms
12,808 KB
testcase_20 AC 321 ms
12,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

struct ST {
  ll x, y;
  ST():x(0),y(0){}
  ST(ll x, ll y):x(x),y(y){}
};
ST combine(const ST& lhs, const ST& rhs){
  ST st;
  st.x = lhs.x + rhs.x;
  st.y = lhs.y + rhs.y;
  return st;
}
template<class S>
class SegmentTree {
  int N;
  vector<S> t;
  void build(){
    for(int i=N-1; i>0; i--){
      t[i] = combine(t[i<<1], t[i<<1|1]);
    }
  }
public:
  SegmentTree(int n_){
    N = 1;
    while(N < n_) N *= 2;
    t.resize(2*N);
    for(int i=N; i<2*N; i++){
      t[i] = S();
    }
    build();
  }
  SegmentTree(const vector<S>& v){
    N = 1;
    while(N < v.size()) N *= 2;
    t.resize(2*N);
    for(int i=0; i<v.size(); i++){
      t[i+N] = v[i];
    }
    build();
  }
  void modify(int p, S val){
    for(t[p+=N] = val; p>>=1;){
      t[p] = combine(t[p<<1], t[p<<1|1]);
    }
  }
  S query(int l, int r){
    S resl, resr;
    for(l+=N, r+=N; l<r; l>>=1, r>>=1){
      if(l&1) resl = combine(resl, t[l++]);
      if(r&1) resr = combine(t[--r], resr);
    }
    return combine(resl, resr);
  }
};


struct X {
  int i;
  int l, r;
  ll x;
};
bool operator<(const X& a, const X& b){
  return a.x > b.x;
}

int main(){
  int N, Q;
  cin >> N >> Q;
  priority_queue<pair<ll,int>> que;
  rep(i,N){
    ll a;
    cin >> a;
    que.push(make_pair(a,i));
  }
  vector<X> query;
  rep(i,Q){
    X st;
    cin >> st.i >> st.l >> st.r >> st.x;
    st.l--;
    st.r--;
    st.i = i;
    query.push_back(st);
  }
  sort(ALLOF(query));

  vector<ll> ret(Q);
  SegmentTree<ST> segtree(N+1);
  ll sum = 0;
  ll prev = -1;
  rep(i,query.size()){
    ll x = query[i].x;

    while(true){
      if(que.empty()) break;
      pair<ll,int> p = que.top();
      if(p.first >= x){
        segtree.modify(p.second,ST(p.first,1));
        que.pop();
      }else{
        break;
      }
    }
    ST st = segtree.query(query[i].l, query[i].r+1);
    ret[query[i].i] = st.x - st.y * x;
  }

  rep(i,ret.size()){
    cout << ret[i] << endl;
  }
  
  return 0;
}

0