結果

問題 No.876 Range Compress Query
ユーザー minatominato
提出日時 2020-03-19 02:33:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 4,277 bytes
コンパイル時間 2,568 ms
コンパイル使用メモリ 183,768 KB
実行使用メモリ 11,936 KB
最終ジャッジ日時 2023-08-20 20:34:21
合計ジャッジ時間 5,290 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 101 ms
11,876 KB
testcase_12 AC 85 ms
11,776 KB
testcase_13 AC 84 ms
11,776 KB
testcase_14 AC 99 ms
11,864 KB
testcase_15 AC 73 ms
11,892 KB
testcase_16 AC 101 ms
11,936 KB
testcase_17 AC 101 ms
11,788 KB
testcase_18 AC 110 ms
11,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
#define ln '\n'
const long long MOD = 1000000007LL;
//const long long MOD = 998244353LL;
typedef long long ll;
typedef unsigned long long ull; 
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true;} return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true;} return false; }
///////////////////////////////////////////////////////////////////////////////////////////////////

template<typename T, typename U, typename F, typename G, typename H>
struct LazySegmentTree {
    T unitynode;
    U unitylazy;
    F f;
    G g;
    H h;
    int N;
    int height;
    vector<T> node;
    vector<U> lazy;

    LazySegmentTree(F f, G g, H h, T unitynode, U unitylazy) : f(f), g(g), h(h), unitynode(unitynode), unitylazy(unitylazy) {}

    void init(int sz) {
        N = 1;
        height = 0;
        while (N < sz) {
            N *= 2;
            ++height;
        }
        node.assign(2*N,unitynode);
        lazy.assign(2*N,unitylazy);
    }

    void set(int k, const T &val) {node[k+N] = val;}

    void build() {
        for (int i = N-1; i > 0; --i) {
            node[i] = f(node[i<<1|0],node[i<<1|1]);
        }
    }

    inline T reflect(int k) {
        return lazy[k] == unitylazy ? node[k] : g(node[k],lazy[k]);
    }

    void eval(int k) {
        if (lazy[k] == unitylazy) return;
        if (k < N) {
            lazy[k<<1|0] = h(lazy[k<<1|0],lazy[k]);
            lazy[k<<1|1] = h(lazy[k<<1|1],lazy[k]);
        }
        node[k] = reflect(k);
        lazy[k] = unitylazy;
    }

    inline void recalc(int k) {
        while (k >>= 1) {
            node[k] = f(reflect(k<<1|0),reflect(k<<1|1));
        }
    }

    // [l,r) (0-indexed)
    void update(int l, int r, U val) {
        if (l >= r) return;
        l += N; r += N-1;
        for (int i = height; i > 0; --i) {
            eval(l>>i);
            eval(r>>i);
        }
        int a = l; int b = r++;
        while (l < r) {
            if (l & 1) lazy[l] = h(lazy[l],val), ++l;
            if (r & 1) --r, lazy[r] = h(lazy[r],val);
            l >>= 1; r >>= 1;
        }
        recalc(a);
        recalc(b);
    }

    T get(int l, int r) {
        if (l >= r) return unitynode;
        l += N; r += N-1;
        for (int i = height; i > 0; --i) {
            eval(l>>i);
            eval(r>>i);
        }
        ++r;
        T vl = unitynode, vr = unitynode;
        while (l < r) {
            if (l & 1) vl = f(vl,reflect(l++));
            if (r & 1) vr = f(reflect(--r),vr);
            l >>= 1; r >>= 1;
        }
        return f(vl,vr);
    }

    T operator[](int x) {return reflect(x+N);}
};

/*
example
auto f=[](ll a, ll b) {return min(a,b);};
auto g=[](ll a, ll b) {return b;};
auto h=[](ll a, ll b) {return b;};
ll id1 = 1e18; ll id2 = -1;
LazySegmentTree<ll, ll, decltype(f), decltype(g), decltype(h)> seg(f,g,h,id1,id2);
seg.init(N);
for (int i = 0; i < N; ++i) seg.set(i,(1LL<<31)-1);
seg.build();
*/

struct Node {
	ll head,tail,cnt;
	Node () = default;
	Node (ll head, ll tail, ll cnt) : head(head), tail(tail), cnt(cnt) {}
	bool operator== (const Node &p) const { return (head == p.head && tail == p.tail && cnt == p.cnt);}
};
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
	int N,Q; cin >> N >> Q;
	vector<ll> A(N);
	rep(i,N) cin >> A[i];

	const Node id1 = Node(-1e18,-1e18,1);
	const ll id2 = 0;
	auto f=[&] (Node a, Node b) {
		if (a == id1) return b;
		if (b == id1) return a;
		ll c = a.cnt + b.cnt;
		if (a.tail == b.head) c--;
		return Node(a.head, b.tail, c);
	};
	auto g=[] (Node a, ll val) {
		a.head += val;
		a.tail += val;
		return a;
	};
	auto h=[] (ll a, ll b) {return a+b;};
	LazySegmentTree<Node, ll, decltype(f), decltype(g), decltype(h)> seg(f,g,h,id1,id2);
	seg.init(N);
	rep(i,N) {
		seg.set(i, Node(A[i],A[i],1));
	}
	seg.build();

	while (Q--) {
		int com; cin >> com;
		if (com==1) {
			int l,r,x; cin >> l >> r >> x;
			--l;
			seg.update(l,r,x);
		} else {
			int l,r; cin >> l >> r;
			--l;
			cout << seg.get(l,r).cnt << ln;
		}
	}
}
0