#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);; cin.tie(NULL)
void selfMax(long long& a, long long b){
a = max(a, b);
}
void selfMin(long long& a, long long b){
a = min(a, b);
}
template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;};
template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& x) {return in >> x.first >> x.second;}
template<typename T1, typename T2> ostream& operator<<(ostream& out, const pair<T1, T2>& x) {return out << x.first << ' ' << x.second;}
template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {for(auto &x : a) out << x << ' '; return out;};
template<typename T> ostream& operator<<(ostream& out, vector<vector<T> >& a) {for(auto &x : a) out << x << '\n'; return out;};
template<typename T1, typename T2> ostream& operator<<(ostream& out, vector<pair<T1, T2> >& a) {for(auto &x : a) out << x << '\n'; return out;};
/*
Use DSU dsu(N);
*/
struct DSU {
vector<long long> e;
DSU(long long N) { e = vector<long long>(N, -1); }
// get representive component (uses path compression)
long long get(long long x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool same_set(long long a, long long b) { return get(a) == get(b); }
long long size(long long x) { return -e[get(x)]; }
bool unite(long long x, long long y) { // union by size
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y]; e[y] = x;
return true;
}
};
/*
Run with Bit<long long> BIT
*/
template <class T> class BIT {
private:
long long size;
vector<T> bit;
vector<T> arr;
public:
BIT(long long size) : size(size), bit(size + 1), arr(size) {}
void set(long long ind, long long val) { add(ind, val - arr[ind]); }
void add(long long ind, long long val) {
arr[ind] += val;
ind++;
for (; ind <= size; ind += ind & -ind) { bit[ind] += val; }
}
T pref_sum(long long ind) {
ind++;
T total = 0;
for (; ind > 0; ind -= ind & -ind) { total += bit[ind]; }
return total;
}
};
/*
Change Comb for specific Seg tree probs, but run with Seg<long long> Seg;
*/
template<class T> struct Seg {
const T ID = 1e18; T comb(T a, T b) { return min(a,b); }
long long n; vector<T> seg;
void init(long long _n) { n = _n; seg.assign(2*n,ID); }
void pull(long long p) { seg[p] = comb(seg[2*p],seg[2*p+1]); }
void upd(long long p, T val) {
seg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); }
T query(long long l, long long r) {
T ra = ID, rb = ID;
for (l += n, r += n+1; l < r; l /= 2, r /= 2) {
if (l&1) ra = comb(ra,seg[l++]);
if (r&1) rb = comb(seg[--r],rb);
}
return comb(ra,rb);
}
};
int main() {
IOS;
long long n, q;
cin >> n >> q;
vector<long long> v(n + 1);
for(long long i = 1; i < n + 1; i++){
cin >> v[i];
}
vector<long long> queries(q + 1);
for(long long i = 1; i < q + 1; i++){
cin >> queries[i];
}
vector<long long> left(q + 1);
vector<long long> right(q + 1);
long long sum = 0;
left[0] = 0;
right[0] = 0;
for(long long i = 1; i <= q; i++){
sum += queries[i];
right[i] = max(right[i - 1], -sum);
left[i] = max(left[i - 1], sum);
}
vector<long long> answers(1 + n);
answers[1] = right[q];
answers[n] = left[q];
for(long long i = 1; i < n; i++){
long long difference = v[i + 1] - v[i];
long long low = 0;
long long high = q;
while(low <= high){
long long mid = (low + high)/2;
if(left[mid] + right[mid] >= difference){
high = mid - 1;
}
else{
low = mid + 1;
}
}
answers[i] += left[high];
answers[i + 1] += right[high];
long long remaining = difference - left[high] - right[high];
if(high < q){
if(left[high + 1] > left[high]){
answers[i] += remaining;
}
else{
answers[i + 1] += remaining;
}
}
}
for(long long i = 1; i <= n; i++){
cout << answers[i] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
2 ms |
392 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
2 ms |
392 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
21 ms |
7124 KB |
Output is correct |
21 |
Correct |
20 ms |
6872 KB |
Output is correct |
22 |
Correct |
19 ms |
6728 KB |
Output is correct |
23 |
Correct |
19 ms |
6608 KB |
Output is correct |
24 |
Correct |
23 ms |
7120 KB |
Output is correct |
25 |
Correct |
75 ms |
13400 KB |
Output is correct |
26 |
Correct |
82 ms |
13500 KB |
Output is correct |
27 |
Correct |
75 ms |
12952 KB |
Output is correct |
28 |
Correct |
75 ms |
13112 KB |
Output is correct |
29 |
Correct |
73 ms |
12616 KB |
Output is correct |
30 |
Correct |
72 ms |
11984 KB |
Output is correct |
31 |
Correct |
47 ms |
11516 KB |
Output is correct |
32 |
Correct |
42 ms |
11604 KB |
Output is correct |
33 |
Correct |
7 ms |
1620 KB |
Output is correct |
34 |
Correct |
77 ms |
13772 KB |
Output is correct |
35 |
Correct |
74 ms |
13136 KB |
Output is correct |
36 |
Correct |
75 ms |
13400 KB |
Output is correct |
37 |
Correct |
78 ms |
13092 KB |
Output is correct |
38 |
Correct |
74 ms |
13000 KB |
Output is correct |
39 |
Correct |
76 ms |
13268 KB |
Output is correct |
40 |
Correct |
51 ms |
13132 KB |
Output is correct |
41 |
Correct |
22 ms |
7632 KB |
Output is correct |
42 |
Correct |
41 ms |
11568 KB |
Output is correct |
43 |
Correct |
55 ms |
14916 KB |
Output is correct |
44 |
Correct |
20 ms |
7496 KB |
Output is correct |
45 |
Correct |
53 ms |
13160 KB |
Output is correct |
46 |
Correct |
55 ms |
15060 KB |
Output is correct |
47 |
Correct |
56 ms |
15304 KB |
Output is correct |
48 |
Correct |
56 ms |
15332 KB |
Output is correct |