結果

問題 No.877 Range ReLU Query
ユーザー snow39snow39
提出日時 2019-09-06 22:17:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 392 ms / 2,000 ms
コード長 2,550 bytes
コンパイル時間 1,603 ms
コンパイル使用メモリ 110,364 KB
実行使用メモリ 16,012 KB
最終ジャッジ日時 2024-04-25 22:01:59
合計ジャッジ時間 6,289 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 3 ms
6,816 KB
testcase_02 AC 3 ms
6,944 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,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 4 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 392 ms
15,096 KB
testcase_12 AC 335 ms
14,856 KB
testcase_13 AC 267 ms
11,076 KB
testcase_14 AC 280 ms
11,280 KB
testcase_15 AC 390 ms
15,908 KB
testcase_16 AC 371 ms
15,508 KB
testcase_17 AC 378 ms
15,688 KB
testcase_18 AC 370 ms
15,668 KB
testcase_19 AC 341 ms
16,012 KB
testcase_20 AC 366 ms
15,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

#include <functional>
#include <climits>

//SegmentTree<int> seg(N,[](int a,int b){return min(a,b);},INT_MAX);
template< typename T>
class SegmentTree{
public:

  using F = function<T(T,T)>;

  int n;
  vector<T> tree;
  const F operation;
  const T def;

  SegmentTree(int size,F _operation,T _def):operation(_operation),def(_def){
    n=1;
    while(n<size) n*=2;
    tree.resize(2*n-1,def);
  }

  void initialize(vector<T> v){
    int vSize=v.size();
    n=1;
    while(n<vSize) n*=2;
    tree.resize(2*n-1,def);

    for(int i=0;i<vSize;i++) tree[i+n-1]=v[i];
    for(int i=n-2;i>=0;i--) tree[i]=operation(tree[2*i+1],tree[2*i+2]);
  }

  void update(int index,T value){
    index+=n-1;

    tree[index]=value;
    while(index>0){
      index=(index-1)/2;
      tree[index]=operation(tree[2*index+1],tree[2*index+2]);
    }
  }

  T query(int a,int b,int k=0,int l=0,int r=-1){//[a,b),getMin(a,b,0,0,-1)
    if(r<0) r=n;

    if(r<=a||b<=l) return def;
    else if(a<=l&&r<=b) return tree[k];
    else{
      T lval=query(a,b,2*k+1,l,(l+r)/2);
      T rval=query(a,b,2*k+2,(l+r)/2,r);
      return operation(lval,rval);
    }
  }
};

int N,Q;
vector<ll> A;
vector<ll> L,R,X;

ll sum[100010];

int main(){
  cin>>N>>Q;
  A.resize(N);
  X.resize(Q);
  R.resize(Q);
  L.resize(Q);
  rep(i,N) cin>>A[i];
  rep(i,Q){
    int t;cin>>t;
    cin>>L[i]>>R[i]>>X[i];
    L[i]--;R[i]--;
  }

  for(int i=0;i<N;i++) sum[i+1]=sum[i]+A[i];
  
  vector<pair<ll,int>> v;
  for(int i=0;i<N;i++) v.push_back(mkp(A[i],i));
  SegmentTree<pair<ll,int>> seg(N,[](pair<ll,int> a,pair<ll,int> b){return mkp(a.first+b.first,a.second+b.second);},mkp(0,0));
  //seg.initialize(v);

  vector<pair<ll,int>> qry;
  for(int i=0;i<Q;i++) qry.push_back(mkp(X[i],i));
  sort(qry.begin(),qry.end());
  sort(v.begin(),v.end());

  int now=0;
  vector<ll> ans(Q,0);
  for(int q=0;q<Q;q++){
    int tar=qry[q].second;
    while(now<N&&v[now].first<=X[tar]){
      int p=v[now].second;
      seg.update(p,mkp(A[p],1));
      now++;
    }
    
    auto f=seg.query(L[tar],R[tar]+1);
    ll res=sum[R[tar]+1]-sum[L[tar]]-X[tar]*(R[tar]-L[tar]+1)+(X[tar]*f.second-f.first);
    ans[tar]=res;
  }

  for(int i=0;i<Q;i++) cout<<ans[i]<<endl;

  return 0;
}
0