Because you can only bring one, buy and sell the same price, so as long as the right side of the larger than the left, it is bought from this and then brought to the next sale on the line (I want to sell again elsewhere, the big deal and buy back again)
So give Max (w[i]-w[i-1],0) a prefix and it's okay.
1#include <bits/stdc++.h>2 #definePA pair<int,int>3 #defineCLR (a,x) memset (A,x,sizeof (a))4 using namespacestd;5typedefLong Longll;6 Const intmaxn=1e6+Ten;7 8 Inline LL Rd () {9ll x=0;CharC=getchar ();intneg=1;Ten while(c<'0'|| C>'9'){if(c=='-') neg=-1; c=GetChar ();} One while(c>='0'&&c<='9') x=x*Ten+c-'0', c=GetChar (); A returnx*neg; - } - the intn,m; - ll S[MAXN]; - - intMain () { + //freopen (". In", "R", stdin); - inti,j,k; +n=Rd (); A for(i=2, J=rd (); i<=n;i++){ atk=Rd (); -s[i]=s[i-1]+max (K-j,0); -j=K; -}m=Rd (); - for(i=1; i<=m;i++){ - intL=rd (), r=Rd (); inprintf"%lld\n", s[r]-s[l]); - } to return 0; +}
suoi38 sell XY sequence (greedy + prefix and)