Submission #38042

#TimeUsernameProblemLanguageResultExecution timeMemory
38042MatheusLealVFoehn Phenomena (JOI17_foehn_phenomena)C++14
100 / 100
276 ms5140 KiB
#include <bits/stdc++.h>
#define inf 200000000000000000LL
#define N 200050
using namespace std;
typedef long long ll;

int n, q;

ll A[N], bit[N], ans, S, T;

void upd(int l, int r, int x)
{
	if(1 <= l && l <= n + 1) for(int i = l; i < N; i += (i&-i)) bit[i] += x;

	if(1 <= r + 1&& r + 1 <= n + 1) for(int i = r + 1; i < N; i += (i&-i)) bit[i] -= x;
}

ll query(int x)
{
	ll aux = 0;

	if(x <= 0 || x > n + 1) return inf;

	for(int i = x; i > 0; i -= (i&-i)) aux += bit[i];

	return aux;
}

int32_t main()
{
	scanf("%d %d %lld %lld", &n, &q, &S, &T);

	for(int i = 1; i <= n + 1; i++) scanf("%lld", &A[i]), upd(i, i, A[i]);

	for(int i = 1; i <= n; i++) ans += (A[i] <= A[i + 1] ? (- A[i + 1] + A[i])*S : (A[i] - A[i + 1])*T);

	for(int i = 1; i <= q; i++)
	{
		int li, ri;

		ll xi;

		scanf("%d %d %lld", &li, &ri, &xi);

		li ++, ri ++;

		ll Hl = query(li);

		ll Hr = query(ri);

		ll H_depois = query(ri + 1);

		ll H_antes = query(li - 1);

		upd(li, ri, xi);

		ll dl = 0, dr = 0;

		if(H_antes != inf && H_antes <= Hl) dl -= abs(Hl - H_antes)*S ;

		else if(H_antes  != inf) dl += abs(Hl - H_antes)*T;

		if(Hr <= H_depois && H_depois != inf) dr -= abs(H_depois - Hr)*S ;

		else if(H_depois != inf) dr += abs(H_depois - Hr)*T;

		ans = ans - dl - dr;

		Hl += xi, Hr += xi;

		dl = 0, dr = 0;

		if(H_antes <= Hl&& H_antes != inf) dl -= abs(Hl - H_antes)*S ;

		else if(H_antes != inf) dl += abs(Hl - H_antes)*T;

		if(Hr <= H_depois && H_depois != inf) dr -= abs(H_depois - Hr)*S ;

		else if(H_depois != inf) dr += abs(H_depois - Hr)*T;

		ans += dl + dr;

		printf("%lld\n", ans);
	}
}

Compilation message (stderr)

foehn_phenomena.cpp: In function 'int32_t main()':
foehn_phenomena.cpp:31:42: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %lld %lld", &n, &q, &S, &T);
                                          ^
foehn_phenomena.cpp:33:71: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i = 1; i <= n + 1; i++) scanf("%lld", &A[i]), upd(i, i, A[i]);
                                                                       ^
foehn_phenomena.cpp:43:37: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %lld", &li, &ri, &xi);
                                     ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...