답안 #475894

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
475894 2021-09-24T10:10:09 Z Jarif_Rahman Snowball (JOI21_ho_t2) C++17
100 / 100
2431 ms 26120 KB
    #include <bits/stdc++.h>
    #define pb push_back
    #define f first
    #define sc second
    using namespace std;
    typedef long long int ll;
    typedef string str;
     
    #pragma GCC target ("avx2")
    #pragma GCC optimization ("O3")
    #pragma GCC optimization ("unroll-loops")
     
    const ll inf = 1e18;
    struct segtree{
        struct node{
            ll sm, mx, mn;
            node(){
                sm = 0, mx = -1e18, mn = 1e18;
            }
            node(ll x){
                sm = x, mx = x, mn = x;
            }
            node operator+(node a){
                node rt;
                rt.sm = sm+a.sm;
                rt.mx = max(mx, a.mx);
                rt.mn = min(mn, a.mn);
                return rt;
            }
        };
        int k;
        vector<node> v;
        segtree(int n){
            k = 1;
            while(k < n) k*=2;
            k*=2;
            v.resize(k, node());
        }
        void upd(int in, ll x){
            in+=k/2;
            v[in] = node(x);
            in/=2;
            while(in > 0){
                v[in] = v[2*in]+v[2*in+1];
                in/=2;
            }
        }
        int first_atleast(int nd, int a, int b, ll x){
            if(v[nd].mx < x) return -1;
            if(a == b) return a;
            int md = (a+b)/2;
            if(v[2*nd].mx < x) return first_atleast(2*nd+1, md+1, b, x);
            else return first_atleast(2*nd, a, md, x);
        }
        int first_atleast(ll x){
            return first_atleast(1, 0, k/2-1, x);
        }
        int first_atmost(int nd, int a, int b, ll x){
            if(v[nd].mn > x) return -1;
            if(a == b) return a;
            int md = (a+b)/2;
            if(v[2*nd].mn > x) return first_atmost(2*nd+1, md+1, b, x);
            else return first_atmost(2*nd, a, md, x);
        }
        int first_atmost(ll x){
            return first_atmost(1, 0, k/2-1, x);
        }
    };
    int main(){
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        int n, q; cin >> n >> q;
        vector<ll> v(n);
        for(ll &x: v) cin >> x;
     
        vector<ll> w(q);
        for(ll &x: w) cin >> x;
        vector<ll> sum(q+1);
        sum[0] = 0;
        for(int i = 0; i < q; i++) sum[i+1] = sum[i]+w[i];
        segtree seg(q+1);
        for(int i = 0; i < q+1; i++) seg.upd(i, sum[i]);
     
        vector<ll> ans(n, 0);
     
        for(int i = 0; i < n; i++){
            
            ll a = -inf, b = v[i];
            if(i != 0) a = v[i-1];
            while(a < b){
                ll md = (a+b);
                if(md >= 0) md/=2;
                else md/=2, md--;
                int x = seg.first_atmost(md-v[i]);
                if(x == -1){
                    a = md+1;
                    continue;
                }
                if(i != 0 && seg.first_atleast(md+1-v[i-1]) != -1 && seg.first_atleast(md+1-v[i-1]) < x){
                    a = md+1;
                    continue;
                }
                b = md;
            }
            ans[i]+=v[i]-a;
     
            a = v[i], b = inf;
            if(i != n-1) b = v[i+1];
            while(a < b){
                ll md = (a+b);
                if(md >= 0) md++, md/=2;
                else md/=2;
                int x = seg.first_atleast(md-v[i]);
                if(x == -1){
                    b = md-1;
                    continue;
                }
                if(i != n-1 && seg.first_atmost(md-1-v[i+1]) != -1 && seg.first_atmost(md-1-v[i+1]) < x){
                    b = md-1;
                    continue;
                }
                a = md;
            }
            ans[i]+=a-v[i];
     
        }
     
        for(ll x: ans) cout << x << "\n";
    }

Compilation message

Main.cpp:10: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   10 |     #pragma GCC optimization ("O3")
      | 
Main.cpp:11: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   11 |     #pragma GCC optimization ("unroll-loops")
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 444 KB Output is correct
4 Correct 13 ms 472 KB Output is correct
5 Correct 12 ms 460 KB Output is correct
6 Correct 9 ms 460 KB Output is correct
7 Correct 8 ms 460 KB Output is correct
8 Correct 8 ms 460 KB Output is correct
9 Correct 7 ms 460 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 14 ms 476 KB Output is correct
16 Correct 12 ms 460 KB Output is correct
17 Correct 9 ms 460 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 444 KB Output is correct
4 Correct 13 ms 472 KB Output is correct
5 Correct 12 ms 460 KB Output is correct
6 Correct 9 ms 460 KB Output is correct
7 Correct 8 ms 460 KB Output is correct
8 Correct 8 ms 460 KB Output is correct
9 Correct 7 ms 460 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 14 ms 476 KB Output is correct
16 Correct 12 ms 460 KB Output is correct
17 Correct 9 ms 460 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 46 ms 15708 KB Output is correct
21 Correct 46 ms 15756 KB Output is correct
22 Correct 48 ms 15708 KB Output is correct
23 Correct 85 ms 15812 KB Output is correct
24 Correct 308 ms 16204 KB Output is correct
25 Correct 2431 ms 20424 KB Output is correct
26 Correct 2365 ms 20432 KB Output is correct
27 Correct 2304 ms 20172 KB Output is correct
28 Correct 2246 ms 20164 KB Output is correct
29 Correct 2136 ms 20036 KB Output is correct
30 Correct 1062 ms 19656 KB Output is correct
31 Correct 170 ms 19288 KB Output is correct
32 Correct 82 ms 19164 KB Output is correct
33 Correct 168 ms 2620 KB Output is correct
34 Correct 2054 ms 20488 KB Output is correct
35 Correct 2145 ms 20168 KB Output is correct
36 Correct 2406 ms 20452 KB Output is correct
37 Correct 2209 ms 20200 KB Output is correct
38 Correct 2215 ms 20168 KB Output is correct
39 Correct 2224 ms 20176 KB Output is correct
40 Correct 1400 ms 20292 KB Output is correct
41 Correct 53 ms 15704 KB Output is correct
42 Correct 103 ms 22420 KB Output is correct
43 Correct 689 ms 25680 KB Output is correct
44 Correct 54 ms 18256 KB Output is correct
45 Correct 620 ms 23888 KB Output is correct
46 Correct 807 ms 25944 KB Output is correct
47 Correct 779 ms 26040 KB Output is correct
48 Correct 260 ms 26120 KB Output is correct