Algorithm-Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub UScuber/Algorithm-Library

:heavy_check_mark: test/aoj/DSL/DSL_2_G.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/DSL_2_G"

#include "../../../template/template.hpp"

#include "../../../SegmentTree/radd-rsum.hpp"

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n,q;
  cin >> n >> q;
  LazySegmentTree<ll> seg(n);
  while(q--){
    int t; cin >> t;
    if(t == 0){
      int l,r,x;
      cin >> l >> r >> x;
      seg.update(l-1, r, x);
    }else{
      int l,r;
      cin >> l >> r;
      cout << seg.query(l-1, r) << "\n";
    }
  }
}
#line 1 "test/aoj/DSL/DSL_2_G.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/DSL_2_G"

#line 1 "template/template.hpp"
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#include <cstring>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
constexpr int mod = 1000000007;
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
#line 4 "test/aoj/DSL/DSL_2_G.test.cpp"

#line 1 "SegmentTree/radd-rsum.hpp"
template <class T>
struct LazySegmentTree {
  LazySegmentTree(const int _n) : n(_n){
    while((1 << log) < n) log++;
    len = 1 << log;
    d.assign(len * 2, 0);
    lazy.assign(len, 0);
    si.assign(len * 2, 1);
    for(int i = len - 1; i >= 1; i--) si[i] = si[i<<1] << 1;
  }
  void set(const int i, const T &x){
    assert(0 <= i && i < n);
    d[i + len] = x;
  }
  T get(int p){
    assert(0 <= p && p < n);
    p += len;
    for(int i = log; i >= 1; i--) push(p >> i);
    return d[p];
  }
  void build(){
    for(int i = len - 1; i >= 1; i--) update(i);
  }
  void update(int l, int r, const T &x){
    assert(0 <= l && l <= r && r <= n);
    l += len; r += len;
    const int l_ctz = __builtin_ctz(l);
    const int r_ctz = __builtin_ctz(r);
    for(int i = log; i > l_ctz; i--) push(l >> i);
    for(int i = log; i > r_ctz; i--) push((r - 1) >> i);
    const int lt = l, rt = r;
    while(l < r){
      if(l & 1) apply(l++, x);
      if(r & 1) apply(--r, x);
      l >>= 1; r >>= 1;
    }
    l = lt; r = rt;
    for(int i = l_ctz + 1; i <= log; i++) update(l >> i);
    for(int i = r_ctz + 1; i <= log; i++) update((r - 1) >> i);
  }
  T query(int l, int r){
    assert(0 <= l && l <= r && r <= n);
    l += len; r += len;
    const int l_ctz = __builtin_ctz(l);
    const int r_ctz = __builtin_ctz(r);
    for(int i = log; i > l_ctz; i--) push(l >> i);
    for(int i = log; i > r_ctz; i--) push((r - 1) >> i);
    T res = 0;
    while(l < r){
      if(l & 1) res += d[l++];
      if(r & 1) res += d[--r];
      l >>= 1; r >>= 1;
    }
    return res;
  }
  private:
  vector<T> d, lazy;
  vector<int> si;
  int n = 1, log = 0, len = 0;
  inline void update(const int k){ d[k] = d[2*k] + d[2*k+1]; }
  inline void apply(const int k, const T &x){
    d[k] += x * si[k];
    if(k < len) lazy[k] += x;
  }
  inline void push(const int k){
    if(!lazy[k]) return;
    apply(2*k, lazy[k]);
    apply(2*k+1, lazy[k]);
    lazy[k] = 0;
  }
};
#line 6 "test/aoj/DSL/DSL_2_G.test.cpp"

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n,q;
  cin >> n >> q;
  LazySegmentTree<ll> seg(n);
  while(q--){
    int t; cin >> t;
    if(t == 0){
      int l,r,x;
      cin >> l >> r >> x;
      seg.update(l-1, r, x);
    }else{
      int l,r;
      cin >> l >> r;
      cout << seg.query(l-1, r) << "\n";
    }
  }
}
Back to top page