結果

問題 No.1441 MErGe
ユーザー SHIJOUSHIJOU
提出日時 2021-04-02 19:25:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 413 ms / 1,000 ms
コード長 7,448 bytes
コンパイル時間 4,011 ms
コンパイル使用メモリ 216,624 KB
実行使用メモリ 12,688 KB
最終ジャッジ日時 2023-08-25 03:35:42
合計ジャッジ時間 13,743 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 8 ms
4,376 KB
testcase_06 AC 8 ms
4,376 KB
testcase_07 AC 9 ms
4,376 KB
testcase_08 AC 82 ms
5,432 KB
testcase_09 AC 82 ms
5,356 KB
testcase_10 AC 146 ms
5,120 KB
testcase_11 AC 133 ms
4,384 KB
testcase_12 AC 147 ms
5,588 KB
testcase_13 AC 331 ms
12,244 KB
testcase_14 AC 336 ms
12,084 KB
testcase_15 AC 357 ms
11,896 KB
testcase_16 AC 324 ms
12,144 KB
testcase_17 AC 314 ms
12,160 KB
testcase_18 AC 249 ms
11,316 KB
testcase_19 AC 279 ms
11,860 KB
testcase_20 AC 223 ms
11,292 KB
testcase_21 AC 199 ms
8,144 KB
testcase_22 AC 302 ms
7,692 KB
testcase_23 AC 413 ms
12,168 KB
testcase_24 AC 406 ms
12,688 KB
testcase_25 AC 401 ms
12,140 KB
testcase_26 AC 408 ms
12,172 KB
testcase_27 AC 407 ms
12,188 KB
testcase_28 AC 370 ms
12,240 KB
testcase_29 AC 372 ms
12,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

#undef _GLIBCXX_DEBUG
string to_string(const string &s) {return '"' + s + '"';}
string to_string(const char *s) {return to_string(string(s));}
string to_string(bool b) {return b?"true":"false";}
string to_string(vector<bool> v) {
  string res = "{";
  for(int i = 0; i < int(v.size()); i++) {
    if(i) res += ", ";
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template<size_t N>
string to_string(bitset<N> v) {
  string res;
  for(size_t i = 0; i < N; i++) res += char('0' + v[i]);
  return res;
}
template<class A, class B>
string to_string(pair<A, B>);
template<class A, class B, class C>
string to_string(tuple<A, B, C>);
template<class A, class B, class C, class D>
string to_string(tuple<A, B, C, D>);
template<class A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for(const auto &x:v) {
    if(!first) res += ", ";
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template<class A, class B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template<class A, class B, class C>
string to_string(tuple<A, B, C> t) {
  return "(" + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ")";
}
template<class A, class B, class C, class D>
string to_string(tuple<A, B, C, D> t) {
  return "(" + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ", " + to_string(get<3>(t)) + ")";
}
void debug_out() {cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << ' ' << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 822
#endif

template <class S, class F>
struct SegTreeL {
  using FX = function<S(S, S)>;
  using FA = function<S(S, F)>;
  using FF = function<F(F, F)>;
  int n, _n, log;
  FX fx;
  FA fa;
  FF ff;
  const S es;
  const F ef;
  vector<S> dat;
  vector<F> laz;
  // es_はSの零元 ef_はFとして使わない値かつff_(val, ef_)がうまくいく数
  SegTreeL(int n_, FX fx_, FA fa_, FF ff_, S es_, F ef_)
    : fx(fx_), fa(fa_), ff(ff_), es(es_), ef(ef_), n(1), _n(n_), log(0) {
      while(n_ > n) n <<= 1, log++;
      dat.assign(n*2-1, es);
      laz.assign(n*2-1, ef);
      build();
  }
  SegTreeL(vector<S> &v, FX fx_, FA fa_, FF ff_, S es_, F ef_)
    : fx(fx_), fa(fa_), ff(ff_), es(es_), ef(ef_), n(1), _n(int(v.size())), log(0) {
      while(_n > n) n <<= 1, log++;
      dat.assign(n*2-1, es);
      laz.assign(n*2-1, ef);
      copy(v.begin(), v.end(), dat.begin()+n-1);
      build();
  }
  inline int chld(int k) {return k*2+1;}
  inline int chrd(int k) {return k*2+2;}
  void set(int i, S s) {dat[i+n-1] = s;}
  void build() {
    for(int k = n-2; k >= 0; k--) dat[k] = fx(dat[chld(k)], dat[chrd(k)]);
  }
  void eval(int k) {
    if(laz[k] == ef) return;
    if(k < n-1) {
      laz[chld(k)] = ff(laz[chld(k)], laz[k]);
      laz[chrd(k)] = ff(laz[chrd(k)], laz[k]);
    }
    dat[k] = fa(dat[k], laz[k]);
    laz[k] = ef;
  }
  void eval_thrice(int k) {
    eval(k);
    if(k >= n-1) return;
    eval(chld(k));
    eval(chrd(k));
  }
  void update(int a, int b, F x, int k, int l, int r) {
    eval(k);
    if(a <= l and r <= b) {
      laz[k] = ff(laz[k], x);
      eval(k);
    }
    else if(a < r and l < b) {
      update(a, b, x, chld(k), l, (l+r)>>1);
      update(a, b, x, chrd(k), (l+r)>>1, r);
      dat[k] = fx(dat[chld(k)], dat[chrd(k)]);
    }
  }
  void update(int a, int b, F x) {update(a, b, x, 0, 0, n);}
  void update2(int id, S x) {
    eval2(id, 0, 0, n);
    int now = id+n-1;
    dat[now] = x;
    while(now) {
      eval(now&1?now+1:now-1);
      now = (now-1)/2;
      dat[now] = fx(dat[chld(now)], dat[chrd(now)]);
    }
  }
  void eval2(int id, int k, int l, int r) {
    if(l == r-1) return;
    eval(k);
    if((l+r)/2 > id) eval2(id, chld(k), l, (l+r)/2);
    else eval2(id, chrd(k), (l+r)/2, r);
    if(l == r-2) {
      eval(chld(k));
      eval(chrd(k));
    }
  }
  S query(int a, int b, int k, int l, int r) {
    eval(k);
    if(r <= a or b <= l) return es;
    else if(a <= l and r <= b) return dat[k];
    else {
      S vl = query(a, b, chld(k), l, (l+r)>>1);
      S vr = query(a, b, chrd(k), (l+r)>>1, r);
      return fx(vl, vr);
    }
  }
  S query(int a, int b) {return query(a, b, 0, 0, n);}
  template<class G>
  int max_right(int l, G g) {
    assert(g(es));
    if(l == _n) return _n;
    l += n;
    for(int i = log; i >= 0; i--) eval_thrice((l>>i)-1);
    S now = es;
    do{
      while(~l&1) l >>= 1;
      if(!g(fx(now, dat[l-1]))) {
        while(l < n) {
          eval_thrice(l-1);
          l <<= 1;
          if(g(fx(now, dat[l-1]))) {
            now = fx(now, dat[l++ - 1]);
          }
        }
        return l-n;
      }
      now = fx(now, dat[l++ - 1]);
    } while((l & -l) != l);
    return _n;
  }
  template<class G>
  int min_left(int r, G g) {
    assert(g(es));
    if(r == 0) return 0;
    r += n;
    for(int i = log; i >= 0; i--) eval_thrice(((r-1)>>i)-1);
    S now = es;
    do{
      r--;
      while(r > 1 and (r&1)) r >>= 1;
      if(!g(fx(dat[r-1], now))) {
        while(r < n) {
          eval_thrice(r-1);
          r = chld(r);
          if(g(fx(dat[r-1], now))) {
            now = fx(dat[--r], now);
          }
        }
        return r+1-n;
      }
      now = fx(dat[r-1], now);
    } while((r & -r) != r);
    return 0;
  }
  //debug
  inline const S operator[](const int i) {return query(i, i+1);}
  void print() {for(int i = 0; i < n; i++) cerr << (*this)[i] << (i == n-1?'\n':' ');}
};

//head

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  vector<ll> pref(n+1);
  rep(i, n) {
    int a;
    cin >> a;
    pref[i+1] = pref[i]+a;
  }
  vector<P> Z(n, {1, 1});
  SegTreeL<P, int> seg(Z, [](P i, P j)->P{return {i.first+j.first, i.second+j.second};}, [](P i, int j)->P{return {i.second*j, i.second};}, [](int i, int j){return j;}, P(0, 0), -1);
  auto get = [&](int i) {
    if(i < 0) return i;
    return seg.max_right(0, [i](P x){return x.first <= i;});
  };
  while(q--) {
    int t, l, r;
    cin >> t >> l >> r;
    //debug(t, l, r);
    l--;
    if(t == 1) {
      l = get(l);
      r = get(r-1);
      seg.update(l, r, 0);
      //rep(i, n) debug(i, get(i));
    }
    else {
      cout << pref[get(r-1)+1]-pref[get(l-1)+1] << '\n';
    }
  }
}
0