結果

問題 No.631 Noelちゃんと電車旅行
ユーザー hashiryohashiryo
提出日時 2018-10-11 13:20:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 420 ms / 2,000 ms
コード長 4,745 bytes
コンパイル時間 889 ms
コンパイル使用メモリ 93,048 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-04-10 07:54:57
合計ジャッジ時間 7,686 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 302 ms
6,812 KB
testcase_01 AC 415 ms
7,296 KB
testcase_02 AC 412 ms
7,296 KB
testcase_03 AC 420 ms
7,296 KB
testcase_04 AC 414 ms
7,168 KB
testcase_05 AC 419 ms
7,168 KB
testcase_06 AC 164 ms
6,944 KB
testcase_07 AC 88 ms
6,944 KB
testcase_08 AC 309 ms
7,296 KB
testcase_09 AC 364 ms
6,940 KB
testcase_10 AC 256 ms
7,296 KB
testcase_11 AC 225 ms
6,940 KB
testcase_12 AC 286 ms
7,296 KB
testcase_13 AC 270 ms
6,940 KB
testcase_14 AC 124 ms
6,940 KB
testcase_15 AC 229 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <stdio.h>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <limits.h>
#include <math.h>
#include <functional>
#include <bitset>

#define repeat(i,n) for (long long i = 0; (i) < (n); ++ (i))
#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i] << '\n'
#define debugArrayP(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i].first<< " " << x[i].second << '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pii;
typedef vector<int> vint;
typedef vector<ll> vll;
const ll INF = INT_MAX;
const ll MOD = 1e9+7;

template<typename T>
struct delay_segtree {
private:
    int N;
    vector<T> node, lazy;
    //例外値 ex)INF,0
    //-------書くとこ---------------------
    T init_value = 0;
    T default_value = -INF;
    T default_lazy = 0;
    static inline T merge(const T& u, const T& v) {
        return max(u,v);
    }
    static inline void lazy_transmit(T& u, const T& v) {
        u += v;
    }
    static inline void node_transmit(T& u,const T& v,int l,int r){
        u += v;
    }
    //------------------------------------
    // k 番目のノードについて遅延評価を行う
    void eval(int k, int l, int r) {
        // 遅延配列が空でない場合、自ノード及び子ノードへの
        // 値の伝播が起こる
        if (lazy[k] != default_lazy) {
            node_transmit(node[k], lazy[k],l,r);
            // 最下段かどうかのチェックをしよう
            if (r - l > 1) {
                lazy_transmit(lazy[2 * k + 1], lazy[k]);
                lazy_transmit(lazy[2 * k + 2], lazy[k]);
            }
            // 伝播が終わったので、自ノードの遅延配列を空にする
            lazy[k] = default_lazy;
        }
    }
public:
    delay_segtree(int n) {
        for(N=1;N<n;N*=2);
        node.resize(2 * N, init_value);
        lazy.resize(2 * N, default_lazy);
    }
    delay_segtree(vector<int> v) {
        int sz = v.size();
        for(N=1;N<sz;N*=2);
        node.resize(2 * N, init_value);
        lazy.resize(2 * N, default_lazy);
        for (int i = 0; i < sz; i++)
            node[i + N - 1] = v[i];
        for (int i = N - 2; i >= 0; i--)
            node[i] = merge(node[2 * i + 1], node[2 * i + 2]);
    }
    void update(int a, int b, T x, int k = 0, int l = 0, int r = -1) {
        if (r < 0)
            r = N;
        // k 番目のノードに対して遅延評価を行う
        eval(k, l, r);
        // 範囲外なら何もしない
        if (b <= l || r <= a)
            return;
        // 完全に被覆しているならば、遅延配列に値を入れた後に評価
        if (a <= l && r <= b) {
            lazy_transmit(lazy[k], x);
            eval(k, l, r);
        }
        // そうでないならば、子ノードの値を再帰的に計算して、
        // 計算済みの値をもらってくる
        else {
            update(a, b, x, 2 * k + 1, l, (l + r) / 2);
            update(a, b, x, 2 * k + 2, (l + r) / 2, r);
            node[k] = merge(node[2 * k + 1], node[2 * k + 2]);
        }
    }
    // update k th element
    void update(int k, T val) {
        k += N - 1; // leaf
        node[k] = val;
        while (k > 0) {
            k = (k - 1) / 2;
            node[k] = merge(node[k * 2 + 1], node[k * 2 + 2]);
        }
    }
    void add(int k, T val) {
        k += N - 1; // leaf
        node[k] += val;
        while (k > 0) {
            k = (k - 1) / 2;
            node[k] = merge(node[k * 2 + 1], node[k * 2 + 2]);
        }
    }
    // [a, b)
    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 (r <= a or b <= l)
            return default_value;
        if (a <= l and r <= b)
            return node[k];
        int m = (l + r) / 2;
        T vl = query(a, b, k * 2 + 1, l, m);
        T vr = query(a, b, k * 2 + 2, m, r);
        return merge(vl, vr);
    }

    T operator[](const int &k) {
        //遅延評価をまずしておく
        query(k, k + 1);
        return node[k + N - 1];
    }
};


int main(){
  int N;cin>>N;
  delay_segtree<ll> dif(N);
  int d=0;
  repeat(i,N-1){
    int t;cin>>t;
    dif.update(i,d);
    dif.add(i,t);
    d-=3;
  }
  dif.update(N-1,d);
  int M;cin>>M;
  repeat(i,M){
    int L,R,D;cin>>L>>R>>D;L--;
    dif.update(L,R,D);
    ll mx=dif.query(0,N);
    if(mx>0)dif.update(0,N,-mx);
    cout << -dif[N-1] << endl;
  }
  return 0;
}
0