Submission #1183459

#TimeUsernameProblemLanguageResultExecution timeMemory
1183459SmuggingSpunSnowball (JOI21_ho_t2)C++20
100 / 100
72 ms8308 KiB
#include<bits/stdc++.h> #define taskname "B" using namespace std; typedef long long ll; template<class T>void minimize(T& a, T b){ if(a > b){ a = b; } } template<class T>void maximize(T& a, T b){ if(a < b){ a = b; } } const ll INF = 1e18; const int lim = 2e5 + 5; int n, q; namespace sub1{ void solve(){ vector<ll>x(n), min_x(n), max_x(n), ans(n, 0); for(int i = 0; i < n; i++){ cin >> x[i]; min_x[i] = max_x[i] = x[i]; } for(int _ = 0; _ < q; _++){ ll w; cin >> w; if(w > 0){ for(int i = n - 1; i > -1; x[i--] += w){ if(i == n - 1){ ans[i] += max(0LL, w - max_x[i] + x[i]); } else{ ans[i] += max(0LL, min(min_x[i + 1] - max_x[i], w - max_x[i] + x[i])); } } } else{ for(int i = 0; i < n; x[i++] += w){ if(i == 0){ ans[i] += max(0LL, -w - x[i] + min_x[i]); } else{ ans[i] += max(0LL, min(min_x[i] - max_x[i - 1], -w - x[i] + min_x[i])); } } } for(int i = 0; i < n; i++){ minimize(min_x[i], x[i]); maximize(max_x[i], x[i]); } } for(ll& v : ans){ cout << v << "\n"; } } } namespace sub2{ const int lim = 2e5 + 5; ll x[lim], max_sum[lim], min_sum[lim], ans[lim]; void solve(){ x[0] = -(x[n + 1] = INF); for(int i = 1; i <= n; i++){ cin >> x[i]; } ll sum = min_sum[0] = max_sum[0] = 0; for(int i = 1; i <= q; i++){ ll w; cin >> w; min_sum[i] = min(min_sum[i - 1], sum += w); max_sum[i] = max(max_sum[i - 1], sum); } memset(ans, 0, sizeof(ans)); for(int i = 1; i < n + 2; i++){ int low = 1, high = q, p = 0; while(low <= high){ int mid = (low + high) >> 1; if(x[i] + min_sum[mid] >= x[i - 1] + max_sum[mid]){ low = (p = mid) + 1; } else{ high = mid - 1; } } ans[i - 1] += max_sum[p]; ans[i] -= min_sum[p]; if(p < q){ if(max_sum[p + 1] > max_sum[p]){ ans[i - 1] += x[i] + min_sum[p] - x[i - 1] - max_sum[p]; } else{ ans[i] += x[i] + min_sum[p] - x[i - 1] - max_sum[p]; } } } for(int i = 1; i <= n; i++){ cout << ans[i] << "\n"; } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(fopen(taskname".inp", "r")){ freopen(taskname".inp", "r", stdin); } cin >> n >> q; if(max(n, q) <= 2000){ sub1::solve(); } else{ sub2::solve(); } }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...