Submission #984391

# Submission time Handle Problem Language Result Execution time Memory
984391 2024-05-16T15:17:22 Z Mr_Husanboy Snowball (JOI21_ho_t2) C++17
100 / 100
109 ms 15564 KB
//#pragma GCC optimize("Ofast,O3")
//#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>
using namespace std;
 
 
 
#ifdef LOCAL
#include "debugger.cpp"
#else
#define debug(...)
#endif
template<class T>
int len(T &a){
    return a.size();
}
 
using ll = long long;
using pii = pair<int,int>;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
string fileio = "";
 
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll infl = 1e18;
const int maxn = 1e5 + 1;
 
 
 
 
 
void solve(){
    ll n, q; cin >> n >> q;
    vector<ll> a(n);
    for(int i = 0; i < n; i ++) cin >> a[i];
    vector<ll> nq;
    nq.push_back(0);
    ll s = 0;
    ll mn = 0, mx = 0;
    for(int i = 1; i <= q; i ++){
        ll x; cin >> x;
        s += x;
        if(s > mx) nq.push_back(s - mx), mx = s;
        if(s < mn) nq.push_back(s - mn), mn = s;
    }
    q = len(nq);
    vector<ll> lef(len(nq)), rig(len(nq)), pref(q);
    for(int i = 1; i < len(nq); i ++){
        lef[i] = lef[i - 1] + min(0ll, nq[i]);
        rig[i] = rig[i - 1] + max(0ll, nq[i]);
        pref[i] = pref[i - 1] + llabs(nq[i]);
    }
    debug(nq);
    for(int i = 0; i < n; i ++){
        ll lb = -infl, rb = infl;
        if(i) lb = a[i - 1] - a[i];
        if(i + 1 < n) rb = a[i + 1] - a[i];
        //debug(lb, rb);
        auto it = upper_bound(all(pref), rb) - pref.begin();
        auto it2 = upper_bound(all(pref), -lb) - pref.begin();
        //debug(it);
        ll ans = rig[it - 1];
        rb -= pref[it - 1];
        if(it != q && nq[it] > 0) ans += rb;
 
        ans += -lef[it2 - 1];
        lb += pref[it2 - 1];
        if(it2 != q && nq[it2] < 0) ans += -lb;
        cout << ans << ' ';
    }
}
 
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    auto start = chrono::high_resolution_clock::now();
    #ifndef LOCAL
    if(fileio.size()){
        freopen((fileio + ".in").c_str(), "r", stdin);
        freopen((fileio + ".out").c_str(), "w", stdout);
    }
    #endif
    int testcases = 1;
    #ifdef Tests
    cin >> testcases;
    #endif
    while(testcases --){
        solve();
        if(testcases) cout << '\n';
        #ifdef LOCAL
        else cout << '\n';
        cout << "_________________________" << endl;
        #endif
    }
    #ifdef LOCAL
    auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
    
    cout << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
    #endif
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:78:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
   78 |     auto start = chrono::high_resolution_clock::now();
      |          ^~~~~
Main.cpp:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |         freopen((fileio + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |         freopen((fileio + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 516 KB Output is correct
2 Correct 1 ms 472 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 516 KB Output is correct
2 Correct 1 ms 472 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 476 KB Output is correct
20 Correct 24 ms 8964 KB Output is correct
21 Correct 23 ms 8728 KB Output is correct
22 Correct 23 ms 8448 KB Output is correct
23 Correct 21 ms 8348 KB Output is correct
24 Correct 28 ms 8624 KB Output is correct
25 Correct 109 ms 13592 KB Output is correct
26 Correct 106 ms 13500 KB Output is correct
27 Correct 109 ms 12728 KB Output is correct
28 Correct 98 ms 12236 KB Output is correct
29 Correct 88 ms 9432 KB Output is correct
30 Correct 65 ms 5856 KB Output is correct
31 Correct 44 ms 5200 KB Output is correct
32 Correct 44 ms 5420 KB Output is correct
33 Correct 9 ms 1512 KB Output is correct
34 Correct 75 ms 7760 KB Output is correct
35 Correct 82 ms 7636 KB Output is correct
36 Correct 101 ms 13220 KB Output is correct
37 Correct 92 ms 12488 KB Output is correct
38 Correct 93 ms 12760 KB Output is correct
39 Correct 92 ms 12868 KB Output is correct
40 Correct 59 ms 12760 KB Output is correct
41 Correct 22 ms 9420 KB Output is correct
42 Correct 37 ms 5460 KB Output is correct
43 Correct 58 ms 15048 KB Output is correct
44 Correct 23 ms 9416 KB Output is correct
45 Correct 62 ms 12744 KB Output is correct
46 Correct 63 ms 15272 KB Output is correct
47 Correct 59 ms 11212 KB Output is correct
48 Correct 62 ms 15564 KB Output is correct