結果

問題 No.876 Range Compress Query
ユーザー le_panda_noirle_panda_noir
提出日時 2020-03-27 22:38:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 649 ms / 2,000 ms
コード長 4,983 bytes
コンパイル時間 1,445 ms
コンパイル使用メモリ 129,992 KB
実行使用メモリ 17,796 KB
最終ジャッジ日時 2023-08-30 16:57:53
合計ジャッジ時間 7,453 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 4 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 623 ms
17,592 KB
testcase_12 AC 531 ms
17,796 KB
testcase_13 AC 511 ms
17,592 KB
testcase_14 AC 633 ms
17,616 KB
testcase_15 AC 459 ms
17,668 KB
testcase_16 AC 624 ms
17,640 KB
testcase_17 AC 609 ms
17,628 KB
testcase_18 AC 649 ms
17,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <iomanip>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
template<class T>using vv = vector<vector<T>>;

// #define rep(i,n) for(int i=0;i<(n);++i)
#define all(f,c,...) (([&](decltype((c)) cccc) { return (f)(begin(cccc), end(cccc), ## __VA_ARGS__); })(c))
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)

#define rrep(i,n) for(int i=(n);i>=0;--i)
const int dx[4]={1,0,-1,0}, dy[4]={0,1,0,-1};
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}

// debug
template<class T>ostream& operator<<(ostream& os,const vector<T>& vec){os<<"{";for(int i=0;i<vec.size();++i)os<<(i?", ":"")<<vec[i];os<<"}";return os;}
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>& rhs){os<<"("<<rhs.first<<", "<<rhs.second<<")";return os;}

#ifdef LOCAL
void debug() {cerr << "\n";}
template<class First> void debug(const First& first) {cerr<<first<<"\n";}
template<class First, class... Rest> void debug(const First& first, const Rest&... rest) {cerr<<first<<",";debug(rest...);}
#else
#define debug(...) 42
#endif


#include <functional>
template<class T, class Lazy> struct LazySegTree {
  private:
    int size;
    vector<T> v; vector<Lazy> lazy;
    vector<int> seg_size;
    using F = function<T(T,T)>;
    using G = function<Lazy(Lazy,Lazy)>;
    using H = function<T(T,Lazy,int)>;
    F f; G g; H h;
    T default_value; Lazy default_lazy;
  public:
    LazySegTree(int n, T default_value, Lazy default_lazy, F f, G g, H h)
      : default_value(default_value), default_lazy(default_lazy), f(f), g(g), h(h) {
      size = 1;
      while (size < n) size *= 2;

      v.resize(2 * size - 1, default_value);
      lazy.resize(4 * size - 1, default_lazy);
      seg_size.resize(2 * size - 1, 1);

      rep(i, size - 1) {
        int j = size - 2 - i;
        seg_size[j] = seg_size[2 * j + 1] + seg_size[2 * j + 2];
      }
    }
    void update(int index, T val) {
      update(index, index+1, default_lazy);
      index = index + size - 1;
      v[index] = val;
      while (index > 0) {
        index = (index - 1) / 2;
        v[index] = f(v[2 * index + 1], v[2 * index + 2]);
      }
    }
    void eval(int index) {
      v[index] = h(v[index], lazy[index], seg_size[index]);

      lazy[2 * index + 1] = g(lazy[index], lazy[2 * index + 1]);
      lazy[2 * index + 2] = g(lazy[index], lazy[2 * index + 2]);

      lazy[index] = default_lazy;
    }
    T query(int l, int r) { return query(l, r, 0, size, 0); }
    T query(int l, int r, int cur_l, int cur_r, int index) {
      // [l, r)に対するクエリ
      eval(index);
      if (cur_r <= l || r <= cur_l) return default_value;
      if (l <= cur_l && cur_r <= r) return v[index];

      // 左右を見ていく
      int mid = (cur_l + cur_r) / 2;
      return f(
          query(l, r, cur_l, mid, 2 * index + 1),
          query(l, r, mid, cur_r, 2 * index + 2));
    }
    void update(int l, int r, Lazy x) { update(l, r, x, 0, size, 0); }
    void update(int l, int r, Lazy x, int cur_l, int cur_r, int index) {
      // [l, r)に対して操作する

      eval(index);
      if (cur_r <= l || r <= cur_l) return;
      if (l <= cur_l && cur_r <= r) {
        lazy[index] = x;
        eval(index);
        return;
      }
      // 左右を見ていく
      int mid = (cur_l + cur_r) / 2;
      update(l, r, x, cur_l, mid, 2 * index + 1);
      update(l, r, x, mid, cur_r, 2 * index + 2);
      v[index] = f(v[2 * index + 1], v[2 * index + 2]);
    }
};




int main() {
  int N; cin >> N;
  int Q; cin >> Q;

  // 要素の管理
  LazySegTree<ll, ll> T(N, 0, 0,
      [](ll a, ll b) {return a + b;},
      [](ll a, ll b) {return a + b;},
      [](ll a, ll b, int size) {return a + b * size;});

  // Fの管理
  // A[i] == A[i+1] なら T2.query(i, i+1) == 0
  // T2.query(l, r) == F(l, r+1)
  LazySegTree<ll, ll> T2(N, 0, 0,
      [](ll a, ll b) {return a + b;},
      [](ll a, ll b) {return a + b;},
      [](ll a, ll b, int size) {return a + b * size;});

  vi A(N);
  rep(i, N) {
    cin >> A[i];
    T.update(i, A[i]);
  }
  rep(i, N-1) {
    if (A[i] != A[i+1])
      T2.update(i, 1);
  }
  rep(i, Q) {
    int q; cin >> q;
    if (q == 1) {
      int l, r, x;
      cin >> l >> r >> x;
      --l; // これで[l, r)の区間とみなせるようになる
      // A_l から A_{r-1}まで変わった
      T.update(l, r, x);
      if (l > 0)
        T2.update(l-1, T.query(l-1, l) == T.query(l, l+1) ? 0 : 1);
      T2.update(r-1, T.query(r-1, r) == T.query(r, r+1) ? 0 : 1);
    } else {
      int l, r; cin >> l >> r;
      --l, --r;
      cout << T2.query(l, r) + 1 << endl;
    }
  }
}
0