이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int sz = 2e5 + 5;
vector<int> qr[sz];
long long a[sz], d[sz];
int n, q, s, t;
// ------------ FUNCTIONS -----------
struct BIT{
vector<int> f;
int n = 2e5 + 5;
BIT(){
f.assign(n, 0);
}
void update(int i, int val){
while(i < n){
f[i] += val;
i += (i & -i);
}
}
int get(int i){
int ans = 0;
while(i > 0){
ans += f[i];
i -= (i & -i);
}
return ans;
}
};
int cost(int x, int y){
return (y > x ? -s*(y - x) : t*(x - y));
}
// ------------ SUBTASKS ------------
namespace SUB1{
bool ok(){
return (n <= 2000 && q <= 2000);
}
void solve(){
for(int i=1; i<=q; ++i){
int l = qr[i][0], r = qr[i][1], x = qr[i][2];
int ans = 0;
for(int j=1; j<=n; ++j){
if(j >= l && j <= r){
a[j] += x;
}
if(a[j-1] < a[j]){
ans -= (a[j] - a[j-1])*s;
}
else{
ans += (a[j-1] - a[j])*t;
}
}
cout<<ans<<endl;
}
}
}
namespace SUB23{
void solve(){
BIT bit;
int ans = 0;
for(int i=1; i<=n; ++i){
bit.update(i, a[i]);
bit.update(i+1, -a[i]);
ans += cost(a[i-1], a[i]);
}
for(int i=1; i<=q; ++i){
int l = qr[i][0], r = qr[i][1], x = qr[i][2];
ans -= cost(bit.get(l-1), bit.get(l));
bit.update(l, x);
ans += cost(bit.get(l-1), bit.get(l));
if(r + 1 <= n){
ans -= cost(bit.get(r), bit.get(r+1));
bit.update(r+1, -x);
ans += cost(bit.get(r), bit.get(r+1));
}
cout<<ans<<endl;
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("main.inp","r",stdin);
//freopen("main.out","w",stdout);
cin>>n>>q>>s>>t;
for(int i=0; i<=n; ++i){
cin>>a[i];
}
for(int i=1; i<=q; ++i){
int l, r, x;
cin>>l>>r>>x;
qr[i] = {l, r, x};
}
if(SUB1::ok()){
SUB1::solve();
}
SUB23::solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |