This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define db double
#define ll __int128
#define int long long
#define pb push_back
#define fs first
#define sd second
#define Mod long(1e9 + 7)
#define all(x) x.begin(), x.end()
#define unvisited long(-1)
#define Eps double(1e-9)
#define _for(i, n) for(int i = 0; i < (n); i++)
#define dbg(x) cout << #x ": " << x << endl;
const int Max = 1e6 + 7, Inf = 1e15 + 7;
void print(bool x) { cout << (x ? "YES" : "NO") << endl; }
string tostring (__int128 x)
{
string ans = "";
while(x > 0)
{
ans += (x % 10 + '0');
x /= 10;
}
reverse(all(ans));
return ans;
}
int ans = 0, n, q, s, t;
int f(int a, int b)
{
if(a < b)
return (a - b) * s;
else
return (a - b) * t;
}
struct SegmentTreeLazy
{
struct node
{
int value, lazy;
node(int v = 0)
{
value = v;
lazy = -Inf;
}
};
vector <node> tree; int l;
void update(int v, int s, int e, int k)
{
if(tree[v].lazy == -Inf) tree[v].lazy = 0;
tree[v].lazy += k;
tree[v].value += (e - s + 1) * k;
}
void push(int v, int s, int e)
{
if (tree[v].lazy != -Inf)
{
int middle = (s + e) / 2;
update(2 * v, s, middle, tree[v].lazy);
update(2 * v + 1, middle + 1, e, tree[v].lazy);
tree[v].lazy = -Inf;
}
}
void increase(int node, int v, int x, int y, int s, int e)
{
if (x > e || y < s) return;
if (s >= x && e <= y)
{
update(node, s, e, v);
return;
}
push(node, s, e);
int middle = (s + e) / 2;
increase(node * 2, v, x, y, s, middle);
increase(node * 2 + 1, v, x, y, middle + 1, e);
tree[node].value = tree[node * 2].value
+ tree[node * 2 + 1].value;
}
void increase(int x, int y, int v) { increase(1, v, x, y, 1, l); }
int sum(int node, int x, int y, int s, int e)
{
if (x > e || y < s) return 0;
if (s >= x && e <= y)
return tree[node].value;
push(node, s, e);
int middle = (s + e) / 2;
return sum(node * 2, x, y, s, middle) +
sum(node * 2 + 1, x, y, middle + 1, e);
}
int query(int x) { return sum(1, x, x, 1, l); }
SegmentTreeLazy(int n)
{
for (l = 1; l < n; l = (l << 1));
tree.resize(2 * l);
}
};
void solve()
{
cin >> n >> q >> s >> t;
SegmentTreeLazy St(n+2);
vector <int> v(n+1);
for(int i = 0; i <= n; i++)
{
cin >> v[i];
St.increase(i+1, i+1, v[i]);
if(i != 0) ans += f(v[i-1], v[i]);
}
//cerr << ans << endl;
while (q--)
{
int l, r, x, a, b, c, d; cin >> l >> r >> x; l++; r++;
a = St.query(l-1), b = St.query(l), c = St.query(r), d = St.query(r+1);
ans -= f(a, b); if(r != n+1) ans -= f(c, d);
St.increase(l, r, x);
a = St.query(l-1), b = St.query(l), c = St.query(r), d = St.query(r+1);
ans += f(a, b); if(r != n+1) ans += f(c, d);
cout << ans << endl;
/*cout << endl;
for(int i = 1; i <= n+1; i++)
cout << St.query(i) << " ";
break; */
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int Q = 1; //cin >> Q;
while (Q--)
{
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... |