結果
| 問題 | No.876 Range Compress Query |
| コンテスト | |
| ユーザー |
onigawara_kypr
|
| 提出日時 | 2019-09-07 06:32:07 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 61 ms / 2,000 ms |
| コード長 | 2,618 bytes |
| コンパイル時間 | 1,004 ms |
| コンパイル使用メモリ | 108,768 KB |
| 実行使用メモリ | 8,192 KB |
| 最終ジャッジ日時 | 2024-06-25 13:26:56 |
| 合計ジャッジ時間 | 3,286 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
ソースコード
#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
using namespace std;
using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(),v.end()
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;}
const ll MOD = 1000000007;
const ll HIGHINF = (ll)1e18;
template < typename T >
struct SegmentTree {
vector<T> data;
long long n;
SegmentTree(vector<T> vec) {
long long sz = vec.size();
n=1;
while(n<sz) n*=2;
data.resize(2*n-1, 0);
for (long long i=0; i<sz; i++) data[i+n-1] = vec[i];
for (long long i=n-2; i>=0; i--) data[i] = data[2*i+1] + data[2*i+2];
}
void update(long long x, T val) {
x += (n-1);
data[x] = val;
while(x > 0) {
x = (x-1) / 2;
data[x] = data[2*x+1] + data[2*x+2];
}
}
T query(long long l, long long r) {
T ret = 0;
l += (n-1), r += (n-1);
while (l < r) {
if ((l & 1LL) == 0LL) ret += data[l];
if ((r & 1LL) == 0LL) ret += data[r-1];
l /= 2;
r = (r-1) / 2;
}
return ret;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n, q; cin >> n >> q;
vll a(n); REP(i, n) cin >> a[i];
vll aa(n-1, 0);
REP(i, n-1) aa[i] = a[i] - a[i+1];
vll aaa(n-1, 0);
REP(i, n-1) aaa[i] = aa[i]==0?0:1;
SegmentTree<ll> seg(aaa);
REP(_, q) {
ll qi; cin >> qi;
if (qi == 1) {
ll l, r, x; cin >> l >> r >> x;
l--;
if (l > 0) {
aa[l-1] -= x;
aaa[l-1] = aa[l-1]==0?0:1;
seg.update(l-1, aaa[l-1]);
}
if (r < n) {
aa[r-1] += x;
aaa[r-1] = aa[r-1]==0?0:1;
seg.update(r-1, aaa[r-1]);
}
} else {
ll l, r; cin >> l >> r;
cout << seg.query(l-1, r-1)+1 << '\n';
}
}
return 0;
}
onigawara_kypr