結果

問題 No.1234 典型RMQ
ユーザー snow39snow39
提出日時 2020-09-18 21:39:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 2,908 bytes
コンパイル時間 1,414 ms
コンパイル使用メモリ 102,824 KB
実行使用メモリ 8,192 KB
最終ジャッジ日時 2024-04-26 11:02:55
合計ジャッジ時間 6,831 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 208 ms
7,936 KB
testcase_07 AC 174 ms
5,376 KB
testcase_08 AC 223 ms
8,064 KB
testcase_09 AC 195 ms
5,632 KB
testcase_10 AC 208 ms
7,936 KB
testcase_11 AC 202 ms
7,936 KB
testcase_12 AC 190 ms
5,760 KB
testcase_13 AC 174 ms
5,376 KB
testcase_14 AC 192 ms
5,632 KB
testcase_15 AC 188 ms
5,376 KB
testcase_16 AC 209 ms
7,936 KB
testcase_17 AC 194 ms
5,632 KB
testcase_18 AC 166 ms
5,376 KB
testcase_19 AC 217 ms
8,192 KB
testcase_20 AC 139 ms
8,064 KB
testcase_21 AC 206 ms
7,808 KB
testcase_22 AC 161 ms
7,936 KB
testcase_23 AC 160 ms
8,192 KB
testcase_24 AC 159 ms
8,064 KB
testcase_25 AC 164 ms
7,936 KB
testcase_26 AC 160 ms
8,064 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 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)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

#include <functional>

template< typename T, typename S >
struct LazySegmentTree{
    int n;
    vector<T> data;
    vector<S> lazy;
    T te;
    S se;

    inline void merge_functions(S& lazy,S& val){
        lazy+=val;
        //lazy=val;
    }
    inline void operate(T& data,S& val,int len){
        //data+=val*len;
        //data=val*len;
        data+=val;
        //data=val;
    }
    inline T merge_values(T& x,T& y){
        //return x+y;
        return min(x,y);
        //return max(x,y);
    }

    LazySegmentTree(){}
    LazySegmentTree(int sz,T te,S se):te(te),se(se){
        n=1;
        while(n<sz) n*=2;
        data.resize(2*n-1,te);
        lazy.resize(2*n-1,se);
    }

    void build(const vector<T> &A){
        for(int k=0;k<int(A.size());k++) data[k+n-1]=A[k];
        for(int k=n-2;k>=0;k--) data[k]=merge_values(data[2*k+1],data[2*k+2]);
    }

    void eval(int k,int l,int r){
        if(lazy[k]==se) return;
        operate(data[k],lazy[k],r-l);
        if(r-l>1){
            merge_functions(lazy[2*k+1],lazy[k]);
            merge_functions(lazy[2*k+2],lazy[k]);
        }
        lazy[k]=se;
    }

    void update(int a,int b,S val,int k=0,int l=0,int r=-1){
        if(r<0) r=n;
        eval(k,l,r);
        if(b<=l||r<=a) return;
        if(a<=l&&r<=b){
            merge_functions(lazy[k],val);
            eval(k,l,r);
        }else{
            update(a,b,val,2*k+1,l,(l+r)/2);
            update(a,b,val,2*k+2,(l+r)/2,r);
            data[k]=merge_values(data[2*k+1],data[2*k+2]);
        }
    }

    T query(int a,int b,int k=0,int l=0,int r=-1){
        if(r<0) r=n;
        eval(k,l,r);
        if(b<=l||r<=a) return te;
        if(a<=l&&r<=b) return data[k];

        T vl=query(a,b,2*k+1,l,(l+r)/2);
        T vr=query(a,b,2*k+2,(l+r)/2,r);
        return merge_values(vl,vr);
    }

    T get(int p){
        return query(p,p+1);
    }
};

/*
  LazySegmentTree<ll,ll> seg(N,te,se);
  vector<ll> init(N,0);
  seg.build(init);
*/

const ll INF=1e18;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin>>N;
  vector<ll> A(N);
  rep(i,N) cin>>A[i];

  LazySegmentTree<ll,ll> seg(N,INF,0);
  seg.build(A);

  int Q;
  cin>>Q;
  rep(q,Q){
      int K,L,R;
      ll C;
      cin>>K>>L>>R>>C;

      L--;R--;
      if(K==1){
          seg.update(L,R+1,C);
      }else{
          ll ans=seg.query(L,R+1);
          cout<<ans<<endl;
      }
  }

  return 0;
}
0