結果

問題 No.876 Range Compress Query
ユーザー 👑 jupirojupiro
提出日時 2020-02-05 21:48:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 2,229 bytes
コンパイル時間 1,014 ms
コンパイル使用メモリ 116,564 KB
実行使用メモリ 5,676 KB
最終ジャッジ日時 2023-10-24 10:12:20
合計ジャッジ時間 2,734 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 56 ms
5,148 KB
testcase_12 AC 47 ms
4,884 KB
testcase_13 AC 48 ms
5,148 KB
testcase_14 AC 55 ms
5,148 KB
testcase_15 AC 41 ms
5,412 KB
testcase_16 AC 56 ms
5,412 KB
testcase_17 AC 56 ms
5,676 KB
testcase_18 AC 59 ms
5,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

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; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

class BIT {
private:
	ll N;
	vector<ll> node;
public:
	BIT(ll M) {
		node = vector<ll>(M + 1, 0);
		N = M;
	}

	ll sum(ll i) {
		ll s = 0;
		while (i > 0) {
			s += node[i];
			i -= i & -i;
		}
		return s;
	}

	ll sum(ll i, ll j) {
		ll l = sum(i - 1);
		ll r = sum(j);
		return r - l;
	}

	void add(ll i, ll x) {
		while (i <= N) {
			node[i] += x;
			i += i & -i;
		}
	}

	void add0(ll i, ll x) {
		add(i + 1, x);
	}
	//[i,j)
	ll sum0(ll i, ll j) {
		return sum(i + 1, j);
	}
};

int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	ll n, q; scanf("%lld %lld", &n, &q);
	vector<ll> a(n); for (ll i = 0; i < n; i++) scanf("%lld", &a[i]);
	BIT bit(n + 1);
    vector<ll> sa(n); 
	for (ll i = 0; i < n - 1; i++) {
		sa[i] = a[i + 1] - a[i];
		if (sa[i] != 0) bit.add0(i, 1);
	}

    while (q--) {
        ll t; scanf("%lld", &t);
        if (t == 1) {
            ll l, r, x; scanf("%lld %lld %lld", &l, &r, &x);
            l--; r--;
			if (l > 0) {
				sa[l - 1] += x;
				if (sa[l - 1] == x) bit.add0(l - 1, 1);
				if (sa[l - 1] == 0) bit.add0(l - 1, -1);
			}
			if (r < n - 1) {
				sa[r] -= x;
				if (sa[r] == -x) bit.add0(r, 1);
				if (sa[r] == 0) bit.add0(r, -1);
			}
        }
        else {
			ll l, r; scanf("%lld %lld", &l, &r);
			l--; r--;
			ll res = 1;
			if (l  < r) res += bit.sum0(l, r);
			printf("%lld\n", res);
        }
    }

	return 0;
}
0