結果

問題 No.876 Range Compress Query
ユーザー ゆきのんゆきのん
提出日時 2020-03-12 04:13:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 276 ms / 2,000 ms
コード長 2,920 bytes
コンパイル時間 1,404 ms
コンパイル使用メモリ 173,644 KB
実行使用メモリ 13,400 KB
最終ジャッジ日時 2024-04-27 22:21:30
合計ジャッジ時間 4,966 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
11,720 KB
testcase_01 AC 5 ms
11,848 KB
testcase_02 AC 3 ms
11,724 KB
testcase_03 AC 5 ms
11,592 KB
testcase_04 AC 3 ms
11,724 KB
testcase_05 AC 3 ms
11,724 KB
testcase_06 AC 4 ms
11,724 KB
testcase_07 AC 4 ms
11,600 KB
testcase_08 AC 4 ms
11,732 KB
testcase_09 AC 3 ms
11,728 KB
testcase_10 AC 4 ms
11,724 KB
testcase_11 AC 273 ms
12,964 KB
testcase_12 AC 239 ms
13,000 KB
testcase_13 AC 232 ms
13,204 KB
testcase_14 AC 269 ms
12,956 KB
testcase_15 AC 193 ms
13,400 KB
testcase_16 AC 257 ms
13,232 KB
testcase_17 AC 262 ms
13,384 KB
testcase_18 AC 276 ms
13,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<LL,LL> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const LL mod=1000000007;
const LL LINF=1LL<<60;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

static const int MAX_SIZE = 1 << 20; 

LL segMax[2 * MAX_SIZE - 1], segAdd[2 * MAX_SIZE - 1];

//区間[a, b)に値xを加算する.
void add(int a, int b, LL x, int k = 0, int l = 0, int r = MAX_SIZE)
{
    if (r <= a || b <= l) return;

    if (a <= l && r <= b){
        segAdd[k] += x;
        return;
    }

    add(a, b, x, k * 2 + 1, l, (l + r) / 2);
    add(a, b, x, k * 2 + 2, (l + r) / 2, r);

    segMax[k] = max(segMax[k * 2 + 1] + segAdd[k * 2 + 1], segMax[k * 2 + 2] + segAdd[k * 2 + 2]);
}

LL getMax(int a, int b, int k = 0, int l = 0, int r = MAX_SIZE)
{
    if (r <= a || b <= l) return 0;

    if (a <= l && r <= b) return (segMax[k] + segAdd[k]);

    LL left = getMax(a, b, k * 2 + 1, l, (l + r) / 2);
    LL right = getMax(a, b, k * 2 + 2, (l + r) / 2, r);

    return (max(left, right) + segAdd[k]);
}


template< typename T >
struct BinaryIndexedTree{

    //0-indexed
    vector< T > bit;

    BinaryIndexedTree(int sz){
        bit.assign(sz, 0);
    }

    //[0,k)までの総和を返す
    T sum(int k){
        T ret = 0;
        for (--k; k >= 0; k = (k&(k+1))-1) ret += bit[k];
        return ret;
    }
    
    //[l,r)の総和を返す
    T sum(int l, int r){
        return sum(r) - sum(l);
    }

    //kにx加算する
    void add(int k, T x){
        for (; k < bit.size(); k |= k+1) bit[k] += x;
    }
};

int main(){
    int n,q;cin >> n >> q;
    vector<LL> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        add(i,i+1,a[i]);
    }
    BinaryIndexedTree<int> bit(n-1);
    for (int i = 0; i < n-1; i++) {
        if(a[i] != a[i+1]) bit.add(i, 1);
    }
    while(q--){
        int t;cin >> t;
        if(t == 1){
            int l,r;LL x;
            cin >> l >> r >> x;
            l--;
            add(l, r, x);
            if(l){
                if(getMax(l-1,l) != getMax(l,l+1)) bit.add(l-1, 1 - bit.sum(l-1,l));
                else bit.add(l-1,- bit.sum(l-1,l));
            }
            if(r < n){
                if(getMax(r-1,r) != getMax(r,r+1)) bit.add(r-1, 1 - bit.sum(r-1,r));
                else bit.add(r-1,- bit.sum(r-1,r));
            }
        }
        else{
            int l,r;cin >> l >> r;
            l--,r--;
            cout << 1 + bit.sum(l, r) << endl;
        }
    }
    return 0;
}

0