結果

問題 No.876 Range Compress Query
ユーザー mtsdmtsd
提出日時 2019-09-06 21:44:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 266 ms / 2,000 ms
コード長 3,313 bytes
コンパイル時間 748 ms
コンパイル使用メモリ 84,644 KB
実行使用メモリ 12,940 KB
最終ジャッジ日時 2023-09-06 23:21:35
合計ジャッジ時間 3,926 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 260 ms
12,544 KB
testcase_12 AC 219 ms
12,324 KB
testcase_13 AC 217 ms
12,232 KB
testcase_14 AC 257 ms
12,492 KB
testcase_15 AC 181 ms
12,552 KB
testcase_16 AC 248 ms
12,940 KB
testcase_17 AC 252 ms
12,872 KB
testcase_18 AC 266 ms
12,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <deque>
#include <iomanip>
#include <cstdio>
#include <stack>
#include <numeric>
#include <cassert>
using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define int long long
template<typename T> class segtree {
private:
    int n,sz,h;
    vector<T> node, lazy;
    void eval(int k) {
        if(lazy[k]){
            node[k] += lazy[k];
            if(k < n) {
                lazy[k*2] += lazy[k] / 2, lazy[k*2+1] += lazy[k] / 2;
            }
            lazy[k] = 0;
        }
    }

public:
    segtree(vector<T>& v) : sz((int)v.size()), h(0) {
        n = 1;
        while(n < sz) n *= 2, h++;
        node.resize(2*n, 0), lazy.resize(2*n, 0);
        for(int i = 0; i < sz; i++) node[i+n] = v[i];
        for(int i = n-1; i >= 1; i--) node[i] = node[2*i] + node[2*i+1];
    }
    void range(int a, int b, T x) {
        a += n, b += n - 1;
        for(int i = h; i > 0; i--) eval(a >> i), eval(b >> i);
        int ta = a, tb = b++, length = 1;
        while(a < b){
            if(a & 1) lazy[a++] += length * x;
            if(b & 1) lazy[--b] += length * x;
            a >>= 1, b >>= 1, length <<= 1;
        }
        while(ta >>= 1, tb >>= 1, ta){
            if(!lazy[ta]){
                eval(2*ta), eval(2*ta+1), node[ta] = node[2*ta] + node[2*ta+1];
            }
            if(!lazy[tb]){
                eval(2*tb), eval(2*tb+1), node[tb] = node[2*tb] + node[2*tb+1];
            }
        }
    }
    T query(int a, int b) {
        a += n, b += n - 1;
        for(int i = h; i > 0; i--) eval(a >> i), eval(b >> i);
        b++;
        T res1 = 0, res2 = 0;
        while(a < b) {
            if(a & 1) eval(a), res1 += node[a++];
            if(b & 1) eval(--b), res2 += node[b];
            a >>= 1, b >>= 1;
        }
        return res1 + res2;
    }
    void print(){for(int i = 0; i < sz; i++) cout<<query(i,i+1)<< " ";cout<<endl;}
};


signed main(){
    int n,q;
    cin >> n >> q;
    vector<int>a(n),b(n-1);
    rep(i,n)cin >> a[i];
    rep(i,n-1){
        if(a[i]!=a[i+1]){
            b[i] = 1;
        }
    }
    segtree<int>sg(a),sg2(b);
    rep(i,q){
        int c,l,r;
        cin >> c >> l >> r;
        l--;r--;
        if(c==1){
            int x;
            cin >> x;
            if(l!=0){
                int p = sg.query(l-1,l);
                int q = sg.query(l,l+1);
                if(p==q){
                    sg2.range(l-1,l,1);
                }else if(q+x==p){
                    sg2.range(l-1,l,-1);
                }
            }
            if(r!=n-1){
                int p = sg.query(r,r+1);
                int q = sg.query(r+1,r+2);
                if(p==q){
                    sg2.range(r,r+1,1);
                }else if(p+x==q){
                    sg2.range(r,r+1,-1);
                }
            }
            sg.range(l,r+1,x);

        }else{
            cout << sg2.query(l,r)+1 << endl;
        }
        // sg.print();
        // sg2.print();
    }
    return 0;
}
0