#include <bits/stdc++.h>
#define ar array
#define fi first
#define se second
using namespace std;
typedef long double ld;
typedef long long ll;
const int inf=2e9+9;
const ll linf=4e18+18;
const int N=2e5;
struct fenw {
vector<ll> f1, f2;
int n;
fenw(int n=0): n(n) {
f1.assign(n+1,0LL), f2=f1;
}
void upd(vector<ll>&f, int p, ll x) { ++p;
for (; p<=n; p+=(p&-p)) f[p]+=x;
}
void rng_upd(int l, int r, ll x) {
upd(f1,l,x), upd(f1,r,-x);
upd(f2,l,x*l), upd(f2,r,-x*r);
}
ll sum(int p, ll ans=0) {
int p1=p;
for (; p; p-=(p&-p)) ans+=f1[p]*p1-f2[p];
return ans;
}
ll get(int p) {return sum(p+1)-sum(p);}
};
struct segtr {
struct node {
ll sum;
pair<int,bool> l, r;
node() {}
};
vector<node> t;
fenw fw;
int n;
segtr(ll *x, int n): n(n) {
fw={n};
for (int i=0; i<n; ++i) fw.rng_upd(i,i+1,x[i]);
t.resize(n*4);
}
void calc(int v, int l, int r) {
int m=(l+r)/2;
t[v].sum=t[2*v+1].sum+t[2*v+2].sum;
t[v].l=t[2*v+1].l, t[v].r=t[2*v+2].r;
ll am1=fw.get(m-1), am=fw.get(m);
if (am1<am&&(t[2*v+1].r.se||t[2*v+1].r.fi==m-1)&&
(t[2*v+2].l.se||t[2*v+2].l.fi==m)) {
t[v].sum+=am-am1;
if (t[2*v+1].l.fi==m-1) t[v].l={t[2*v+2].l.fi,1};
if (t[2*v+2].r.fi==m) t[v].r={t[2*v+1].r.fi,1};
}
else if (am1>am&&(!t[2*v+1].r.se||t[2*v+1].r.fi==m-1)&&
(!t[2*v+2].l.se||t[2*v+2].l.fi==m)) {
t[v].sum+=am1-am;
if (t[2*v+1].l.fi==m-1) t[v].l={t[2*v+2].l.fi,0};
if (t[2*v+2].r.fi==m) t[v].r={t[2*v+1].r.fi,0};
}
}
void build(int v, int l, int r) {
if (l+1==r) {
t[v].sum=0;
t[v].l=t[v].r={l,1};
}
else {
int m=(l+r)/2;
build(2*v+1,l,m);
build(2*v+2,m,r);
calc(v,l,r);
}
} void build() {build(0,0,n);}
void upd(int v, int tl, int tr, int l, int r) {
if (l>=r||(tl==l&&tr==r)) return;
int tm=(tl+tr)/2;
upd(2*v+1,tl,tm,l,min(r,tm));
upd(2*v+2,tm,tr,max(l,tm),r);
calc(v,tl,tr);
} void upd(int l, int r, ll x) {
fw.rng_upd(l,r,x);
upd(0,0,n,l,r);
}
};
int n, q;
ll a[N];
void solve() {
int ss[1000][1000][1000];
for (int i=0; i<1000; ++i) {
for (int j=0; j<1000; ++j) {
for (int k=0; k<1000; ++k) {
ss[i][j][k]=(1<<20);
}
}
}
cin >> n >> q;
for (int i=0; i<n; ++i) cin >> a[i];
segtr tr(a,n); tr.build();
while(q--) {
int l, r, x; cin >> l >> r >> x; --l;
tr.upd(l,r,x);
cout << tr.t[0].sum << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifdef disastah
cout << "Output\n\n";
#endif
/*#ifndef disastah
freopen("marathon.in","r",stdin);
freopen("marathon.out","w",stdout);
#endif*/
int tt=1;
// cin >> tt;
while (tt--) {
solve();
cout << "\n";
}
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:92:6: warning: variable 'ss' set but not used [-Wunused-but-set-variable]
92 | int ss[1000][1000][1000];
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |