結果
| 問題 |
No.1441 MErGe
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-04-02 19:03:28 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 7,448 bytes |
| コンパイル時間 | 2,187 ms |
| コンパイル使用メモリ | 211,052 KB |
| 最終ジャッジ日時 | 2025-01-20 07:59:13 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 4 WA * 24 |
ソースコード
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}
#undef _GLIBCXX_DEBUG
string to_string(const string &s) {return '"' + s + '"';}
string to_string(const char *s) {return to_string(string(s));}
string to_string(bool b) {return b?"true":"false";}
string to_string(vector<bool> v) {
string res = "{";
for(int i = 0; i < int(v.size()); i++) {
if(i) res += ", ";
res += to_string(v[i]);
}
res += "}";
return res;
}
template<size_t N>
string to_string(bitset<N> v) {
string res;
for(size_t i = 0; i < N; i++) res += char('0' + v[i]);
return res;
}
template<class A, class B>
string to_string(pair<A, B>);
template<class A, class B, class C>
string to_string(tuple<A, B, C>);
template<class A, class B, class C, class D>
string to_string(tuple<A, B, C, D>);
template<class A>
string to_string(A v) {
bool first = true;
string res = "{";
for(const auto &x:v) {
if(!first) res += ", ";
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template<class A, class B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template<class A, class B, class C>
string to_string(tuple<A, B, C> t) {
return "(" + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ")";
}
template<class A, class B, class C, class D>
string to_string(tuple<A, B, C, D> t) {
return "(" + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ", " + to_string(get<3>(t)) + ")";
}
void debug_out() {cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << ' ' << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 822
#endif
template <class S, class F>
struct SegTreeL {
using FX = function<S(S, S)>;
using FA = function<S(S, F)>;
using FF = function<F(F, F)>;
int n, _n, log;
FX fx;
FA fa;
FF ff;
const S es;
const F ef;
vector<S> dat;
vector<F> laz;
// es_はSの零元 ef_はFとして使わない値かつff_(val, ef_)がうまくいく数
SegTreeL(int n_, FX fx_, FA fa_, FF ff_, S es_, F ef_)
: fx(fx_), fa(fa_), ff(ff_), es(es_), ef(ef_), n(1), _n(n_), log(0) {
while(n_ > n) n <<= 1, log++;
dat.assign(n*2-1, es);
laz.assign(n*2-1, ef);
build();
}
SegTreeL(vector<S> &v, FX fx_, FA fa_, FF ff_, S es_, F ef_)
: fx(fx_), fa(fa_), ff(ff_), es(es_), ef(ef_), n(1), _n(int(v.size())), log(0) {
while(_n > n) n <<= 1, log++;
dat.assign(n*2-1, es);
laz.assign(n*2-1, ef);
copy(v.begin(), v.end(), dat.begin()+n-1);
build();
}
inline int chld(int k) {return k*2+1;}
inline int chrd(int k) {return k*2+2;}
void set(int i, S s) {dat[i+n-1] = s;}
void build() {
for(int k = n-2; k >= 0; k--) dat[k] = fx(dat[chld(k)], dat[chrd(k)]);
}
void eval(int k) {
if(laz[k] == ef) return;
if(k < n-1) {
laz[chld(k)] = ff(laz[chld(k)], laz[k]);
laz[chrd(k)] = ff(laz[chrd(k)], laz[k]);
}
dat[k] = fa(dat[k], laz[k]);
laz[k] = ef;
}
void eval_thrice(int k) {
eval(k);
if(k >= n-1) return;
eval(chld(k));
eval(chrd(k));
}
void update(int a, int b, F x, int k, int l, int r) {
eval(k);
if(a <= l and r <= b) {
laz[k] = ff(laz[k], x);
eval(k);
}
else if(a < r and l < b) {
update(a, b, x, chld(k), l, (l+r)>>1);
update(a, b, x, chrd(k), (l+r)>>1, r);
dat[k] = fx(dat[chld(k)], dat[chrd(k)]);
}
}
void update(int a, int b, F x) {update(a, b, x, 0, 0, n);}
void update2(int id, S x) {
eval2(id, 0, 0, n);
int now = id+n-1;
dat[now] = x;
while(now) {
eval(now&1?now+1:now-1);
now = (now-1)/2;
dat[now] = fx(dat[chld(now)], dat[chrd(now)]);
}
}
void eval2(int id, int k, int l, int r) {
if(l == r-1) return;
eval(k);
if((l+r)/2 > id) eval2(id, chld(k), l, (l+r)/2);
else eval2(id, chrd(k), (l+r)/2, r);
if(l == r-2) {
eval(chld(k));
eval(chrd(k));
}
}
S query(int a, int b, int k, int l, int r) {
eval(k);
if(r <= a or b <= l) return es;
else if(a <= l and r <= b) return dat[k];
else {
S vl = query(a, b, chld(k), l, (l+r)>>1);
S vr = query(a, b, chrd(k), (l+r)>>1, r);
return fx(vl, vr);
}
}
S query(int a, int b) {return query(a, b, 0, 0, n);}
template<class G>
int max_right(int l, G g) {
assert(g(es));
if(l == _n) return _n;
l += n;
for(int i = log; i >= 0; i--) eval_thrice((l>>i)-1);
S now = es;
do{
while(~l&1) l >>= 1;
if(!g(fx(now, dat[l-1]))) {
while(l < n) {
eval_thrice(l-1);
l <<= 1;
if(g(fx(now, dat[l-1]))) {
now = fx(now, dat[l++ - 1]);
}
}
return l-n;
}
now = fx(now, dat[l++ - 1]);
} while((l & -l) != l);
return _n;
}
template<class G>
int min_left(int r, G g) {
assert(g(es));
if(r == 0) return 0;
r += n;
for(int i = log; i >= 0; i--) eval_thrice(((r-1)>>i)-1);
S now = es;
do{
r--;
while(r > 1 and (r&1)) r >>= 1;
if(!g(fx(dat[r-1], now))) {
while(r < n) {
eval_thrice(r-1);
r = chld(r);
if(g(fx(dat[r-1], now))) {
now = fx(dat[--r], now);
}
}
return r+1-n;
}
now = fx(dat[r-1], now);
} while((r & -r) != r);
return 0;
}
//debug
inline const S operator[](const int i) {return query(i, i+1);}
void print() {for(int i = 0; i < n; i++) cerr << (*this)[i] << (i == n-1?'\n':' ');}
};
//head
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<ll> pref(n+1);
rep(i, n) {
int a;
cin >> a;
pref[i+1] = pref[i]+a;
}
vector<P> Z(n, {1, 1});
SegTreeL<P, int> seg(Z, [](P i, P j)->P{return {i.first+j.first, i.second+j.second};}, [](P i, int j)->P{return {i.second*j, i.second};}, [](int i, int j){return j;}, P(0, 0), -1);
auto get = [&](int i) {
if(i < 0) return i;
return seg.max_right(0, [i](P x){return x.first <= i;});
};
while(q--) {
int t, l, r;
cin >> t >> l >> r;
//debug(t, l, r);
l--;
if(t == 1) {
l = get(l);
r = get(r);
seg.update(l, r-1, 0);
//rep(i, n) debug(i, get(i));
}
else {
cout << pref[get(r-1)+1]-pref[get(l-1)+1] << '\n';
}
}
}