Submission #984024

# Submission time Handle Problem Language Result Execution time Memory
984024 2024-05-16T09:22:14 Z vjudge1 Snowball (JOI21_ho_t2) C++17
100 / 100
112 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 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 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 348 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 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 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 348 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 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 20 ms 6868 KB Output is correct
21 Correct 20 ms 6868 KB Output is correct
22 Correct 19 ms 6868 KB Output is correct
23 Correct 19 ms 6864 KB Output is correct
24 Correct 25 ms 7124 KB Output is correct
25 Correct 112 ms 13768 KB Output is correct
26 Correct 95 ms 13532 KB Output is correct
27 Correct 102 ms 12644 KB Output is correct
28 Correct 92 ms 12236 KB Output is correct
29 Correct 87 ms 9308 KB Output is correct
30 Correct 62 ms 5856 KB Output is correct
31 Correct 44 ms 5204 KB Output is correct
32 Correct 38 ms 5468 KB Output is correct
33 Correct 8 ms 1624 KB Output is correct
34 Correct 77 ms 7892 KB Output is correct
35 Correct 76 ms 7636 KB Output is correct
36 Correct 95 ms 13260 KB Output is correct
37 Correct 91 ms 11460 KB Output is correct
38 Correct 90 ms 12744 KB Output is correct
39 Correct 87 ms 12744 KB Output is correct
40 Correct 59 ms 12744 KB Output is correct
41 Correct 26 ms 9948 KB Output is correct
42 Correct 37 ms 5480 KB Output is correct
43 Correct 61 ms 15068 KB Output is correct
44 Correct 22 ms 9428 KB Output is correct
45 Correct 51 ms 12748 KB Output is correct
46 Correct 65 ms 15196 KB Output is correct
47 Correct 65 ms 11472 KB Output is correct
48 Correct 65 ms 15564 KB Output is correct