제출 #232726

#제출 시각아이디문제언어결과실행 시간메모리
232726syyFoehn Phenomena (JOI17_foehn_phenomena)C++17
100 / 100
177 ms14768 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for(ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
#define f first
#define s second
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define pb push_back
#define all(v) v.begin(), v.end()
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

ll n, q, s, t, arr[200005], inc[200005], l, r, x, ans; //up = -s, down = +t
ll ft[1000005];              // 1-index
ll ls(ll x){
	return (x & (-x));
}
// N means the largest possible index you need for the fenwick!! Refer to flowering for more.
void update(ll l, ll r, ll v){ // Updates from l to r inclusive
	r++;           // Update the difference at r+1 not r
	for(; l <= n; l += ls(l)) ft[l] += v;// Add v to the diff at l
	for(; r <= n; r += ls(r)) ft[r] -= v;// Minus v from diff at r+1
}
ll query(ll p){                     // Returns the element at p
	ll sum = 0;
	for(; p; p -= ls(p)) sum += ft[p];
	return sum; 
}

int main() {
	fastio; cin >> n >> q >> s >> t;
	cin >> arr[0];
	FOR(i, 1, n) {
		cin >> arr[i];
		update(i, i, arr[i]);
		ll pre = ans;
		if (arr[i] > arr[i-1]) ans -= s * (arr[i] - arr[i-1]);
		else ans += t * (arr[i-1] - arr[i]);
		inc[i] = ans - pre;
	}
	while (q--) {
		cin >> l >> r >> x;
		update(l, r, x);
		ll nh = query(l), change = 0;
		if (nh > query(l-1)) change -= s * (nh - query(l-1));
		else change += t * (query(l-1) - nh);
		ans += change - inc[l]; inc[l] = change;
		if (r != n) {
			change = 0;
			nh = query(r);
			if (query(r+1) > nh) change -= s * (query(r+1) - nh);
			else change += t * (nh - query(r+1));
			ans += change - inc[r+1]; inc[r+1] = change;
		}
		cout << ans << "\n";
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...