結果

問題 No.877 Range ReLU Query
ユーザー beetbeet
提出日時 2019-09-03 17:20:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 1,986 bytes
コンパイル時間 2,585 ms
コンパイル使用メモリ 207,440 KB
実行使用メモリ 11,424 KB
最終ジャッジ日時 2024-04-25 21:57:08
合計ジャッジ時間 5,506 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 3 ms
6,816 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 155 ms
10,972 KB
testcase_12 AC 135 ms
10,616 KB
testcase_13 AC 105 ms
7,960 KB
testcase_14 AC 109 ms
8,072 KB
testcase_15 AC 167 ms
11,328 KB
testcase_16 AC 159 ms
10,932 KB
testcase_17 AC 159 ms
10,980 KB
testcase_18 AC 157 ms
11,080 KB
testcase_19 AC 120 ms
11,424 KB
testcase_20 AC 149 ms
11,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template <typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  int n;
  F f;
  T ti;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(F f,T ti):f(f),ti(ti){}
  void init(int n_){
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }
  void build(const vector<T> &v){
    int n_=v.size();
    init(n_);
    for(int i=0;i<n_;i++) dat[n+i]=v[i];
    for(int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }
  void set_val(int k,T x){
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
  }
  T query(int a,int b){
    T vl=ti,vr=ti;
    for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,dat[l++]);
      if(r&1) vr=f(dat[--r],vr);
    }
    return f(vl,vr);
  }
};


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;

//INSERT ABOVE HERE
signed main(){
  int n,q;
  cin>>n>>q;
  vector<int> as(n);
  for(int i=0;i<n;i++) cin>>as[i];

  vector<int> ls(q),rs(q),xs(q);
  for(int i=0;i<q;i++){
    int t;
    cin>>t;
    assert(t==1);
    cin>>ls[i]>>rs[i]>>xs[i];
    ls[i]--;
  }

  vector<int> ord(n+q);
  iota(ord.begin(),ord.end(),0);
  auto comp=[&](int a,int b){
              int u=(a<n?as[a]:xs[a-n]);
              int v=(b<n?as[b]:xs[b-n]);
              return u>v;
            };
  sort(ord.begin(),ord.end(),comp);

  using ll = long long;
  vector<ll> ans(q);
  auto f=[](ll a,ll b){return a+b;};
  SegmentTree<ll> num(f,0),sum(f,0);
  num.build(vector<ll>(n,0));
  sum.build(vector<ll>(n,0));
  for(int x:ord){
    if(x<n){
      num.set_val(x,1);
      sum.set_val(x,as[x]);
    }else{
      int k=x-n;
      ans[k]=sum.query(ls[k],rs[k])-num.query(ls[k],rs[k])*xs[k];
    }
  }
  for(ll x:ans) cout<<x<<"\n";
  cout<<flush;
  return 0;
}
0