Submission #625511

# Submission time Handle Problem Language Result Execution time Memory
625511 2022-08-10T14:06:44 Z MohamedFaresNebili Distributing Candies (IOI21_candies) C++17
0 / 100
1874 ms 34840 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx2")
/// #pragma GCC optimize("unroll-loops")
 
            using namespace std;
 
            using ll = long long;
            using ld = long double;
 
            #define ff first
            #define ss second
            #define pb push_back
            #define all(x) (x).begin(), (x).end()
            #define lb lower_bound
 
            const int MOD = 998244353;
            const int nx[4] = {-1, 0, 1, 0}, ny[4] = {0, 1, 0, -1};
 
            ll mn[800005], mx[800005], lazy[800005];
 
            void prop(ll v, ll l, ll r) {
                if(l == r) return;
                mn[v * 2] += lazy[v];
                mn[v * 2 + 1] += lazy[v];
 
                mx[v * 2] += lazy[v];
                mx[v * 2 + 1] += lazy[v];
 
                lazy[v] = 0;
            }
 
            void update(ll v, ll l, ll r, ll lo, ll hi, ll val) {
                prop(v, l, r);
                if(l > hi || r < lo) return;
                if(l >= lo && r <= hi) {
                    mn[v] += val; mx[v] += val;
                    lazy[v] += val; return;
                }
                update(v * 2, l, (l + r) / 2, lo, hi, val);
                update(v * 2 + 1, (l + r) / 2 + 1, r, lo, hi, val);
                mn[v] = min(mn[v * 2], mn[v * 2 + 1]);
                mx[v] = max(mx[v * 2], mx[v * 2 + 1]);
            }
 
            ll query(ll v, ll l, ll r, ll lo, ll hi, ll op) {
                prop(v, l, r);
                if(l > hi || r < lo) return (op == 0 ? 1e9 + 7 : 0);
                if(l >= lo && r <= hi) return (op == 0 ? mn[v] : mx[v]);
                ll A = query(v * 2, l, (l + r) / 2, lo, hi, op);
                ll B = query(v * 2 + 1, (l + r) / 2 + 1, r, lo, hi, op);
                return (op == 0 ? min(A, B) : max(A, B));
            }
 
            vector<int> distribute_candies(vector<int> c, vector<int> l,
                                           vector<int> r, vector<int> v) {
                ll N = c.size(), Q = l.size();
                vector<int> res(N); vector<pair<ll, ll>> qs[N + 1];
                for(ll i = 0; i < Q; i++) {
                    qs[l[i]].push_back({i, v[i]});
                    qs[r[i] + 1].push_back({i, -v[i]});
                }
                for(ll i = 0; i < N; i++) {
                    for(auto u : qs[i]) {
                        update(1, 0, Q - 1, u.ff, Q - 1, u.ss);
                        update(1, 0, Q - 1, u.ff, Q - 1, u.ss);
                    }
 
                    ll lo = 0, hi = Q - 1, ans = -1;
                    while(lo <= hi) {
                        ll md = (lo + hi) / 2;
                        ll A = query(1, 0, Q - 1, md, Q - 1, 1);
                        ll B = query(1, 0, Q - 1, md, Q - 1, 0);
                        if(A - B >= c[i]) lo = md + 1, ans = md;
                        else hi = md - 1;
                    }
                    if(ans == -1) {
                        res[i] = mx[1] - mn[1];
                        continue;
                    }
                    ll A = query(1, 0, Q - 1, ans, Q - 1, 1);
                    ll B = query(1, 0, Q - 1, ans, Q - 1, 0);
                    ll C = query(1, 0, Q - 1, Q - 1, Q - 1, 0);
                    ll D = query(1, 0, Q - 1, ans, ans, 0);
                    if(D == A) res[i] = C - B;
                    else res[i] = c[i] + C - A;
                }
                return res;
            }
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1874 ms 34840 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -