結果

問題 No.631 Noelちゃんと電車旅行
ユーザー HaarHaar
提出日時 2020-06-19 19:31:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 248 ms / 2,000 ms
コード長 2,412 bytes
コンパイル時間 2,362 ms
コンパイル使用メモリ 203,080 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 08:03:03
合計ジャッジ時間 7,193 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 196 ms
6,812 KB
testcase_01 AC 244 ms
6,940 KB
testcase_02 AC 241 ms
6,940 KB
testcase_03 AC 247 ms
6,940 KB
testcase_04 AC 243 ms
6,940 KB
testcase_05 AC 248 ms
6,940 KB
testcase_06 AC 99 ms
6,944 KB
testcase_07 AC 46 ms
6,940 KB
testcase_08 AC 179 ms
6,940 KB
testcase_09 AC 219 ms
6,944 KB
testcase_10 AC 146 ms
6,940 KB
testcase_11 AC 140 ms
6,940 KB
testcase_12 AC 163 ms
6,944 KB
testcase_13 AC 169 ms
6,944 KB
testcase_14 AC 83 ms
6,940 KB
testcase_15 AC 139 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,940 KB
testcase_20 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>


#ifdef DEBUG
#include <Mylib/Debug/debug.cpp>
#else
#define dump(...) ((void)0)
#endif



template <typename T, typename Compare>
class StarrySkyTree{
  int depth, size, hsize;
  std::vector<T> data;

  Compare compare = Compare();

  T f(T a, T b) const {
    return compare(a, b) ? a : b;
  }

  void bottom_up(int i){
    if(i > size) return;

    while(i >= 1){
      if(i < hsize){
        const auto d = f(data[i << 1 | 0], data[i << 1 | 1]);
        
        data[i << 1 | 0] -= d;
        data[i << 1 | 1] -= d;
        data[i] += d;
      }

      i >>= 1;
    }
  }
  
  std::optional<T> get(int i, int l, int r, int s, int t, T val) const {
    if(r <= s or t <= l) return std::nullopt;
    if(s <= l and r <= t) return val + data[i];

    auto a = get(i << 1 | 0, l, (l + r) / 2, s, t, val + data[i]);
    auto b = get(i << 1 | 1, (l + r) / 2, r, s, t, val + data[i]);

    if(not a) return b;
    if(not b) return a;
    return f(*a, *b);
  }

public:
  StarrySkyTree(int n):
    depth(n > 1 ? 32-__builtin_clz(n-1) + 1 : 1),
    size(1 << depth),
    hsize(size / 2),
    data(size, 0)
  {}

  void update(int l, int r, T val){
    int L = l + hsize;
    int R = r + hsize;

    while(L < R){
      if(R & 1) --R, data[R] += val;
      if(L & 1) data[L] += val, ++L;
      L >>= 1;
      R >>= 1;
    }

    bottom_up(l + hsize);
    bottom_up(r + hsize);
  }

  T get(int l, int r) const {
    return *get(1, 0, hsize, l, r, 0);
  }

  template <typename U>
  void init_with_vector(std::vector<U> &a){
    for(int i = 0; i < (int)a.size(); ++i){
      data[hsize + i] = a[i];
    }

    for(int i = hsize - 1; i >= 1; --i){
      data[i] = f(data[i << 1 | 0], data[i << 1 | 1]);
    }

    for(int i = size - 1; i > 1; --i){
      data[i] -= data[i >> 1];
    }
  }
};

int main(){
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  
  int N;
  while(std::cin >> N){
    auto seg = StarrySkyTree<int64_t, std::greater<>>(N-1);
        
    std::vector<int64_t> T(N-1);
    for(auto &x : T) std::cin >> x;
    
    for(int i = 0; i < N-1; ++i){
      T[i] += 3 * (N-1-i);
    }

    seg.init_with_vector(T);
    
    int M; std::cin >> M;

    for(int i = 0; i < M; ++i){
      int L, R, D; std::cin >> L >> R >> D;
      --L, --R;

      seg.update(L, R+1, D);
      
      auto ans = seg.get(0, N-1);
      std::cout << ans << std::endl;
    }
  }

  return 0;
}
0