Submission #470396

# Submission time Handle Problem Language Result Execution time Memory
470396 2021-09-03T16:20:40 Z wiwiho Distributing Candies (IOI21_candies) C++17
100 / 100
507 ms 40392 KB
#include "candies.h"

#include <bits/stdc++.h>
#include <bits/extc++.h>

#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;

const ll MOD = 1000000007;
const ll MAX = 2147483647;
const ll LLMAX = 1LL << 60;

template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}

ll ifloor(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a < 0) return (a - b + 1) / b;
    else return a / b;
}

ll iceil(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a > 0) return (a + b - 1) / b;
    else return a / b;
}

struct Node{
    ll mn = 0, mx = 0, tag = 0;
};

vector<Node> st;

void addtag(int id, ll v){
    st[id].mn += v;
    st[id].mx += v;
    st[id].tag += v;
}

void push(int id){
    addtag(2 * id + 1, st[id].tag);
    addtag(2 * id + 2, st[id].tag);
    st[id].tag = 0;
}

void modify(int l, int r, ll v, int L, int R, int id){
    if(l == L && r == R){
        addtag(id, v);
        return;
    }
    push(id);
    int M = (L + R) / 2;
    if(r <= M) modify(l, r, v, L, M, 2 * id + 1);
    else if(l > M) modify(l, r, v, M + 1, R, 2 * id + 2);
    else{
        modify(l, M, v, L, M, 2 * id + 1);
        modify(M + 1, r, v, M + 1, R, 2 * id + 2);
    }
    st[id].mn = min(st[2 * id + 1].mn, st[2 * id + 2].mn);
    st[id].mx = max(st[2 * id + 1].mx, st[2 * id + 2].mx);
}

bool type;
ll val;
int query(ll c, int L, int R, int id, ll nmn = LLMAX, ll nmx = -LLMAX){
    if(L == R){
        nmn = min(nmn, st[id].mn);
        nmx = max(nmx, st[id].mx);
//        cerr << "query " << L << " " << R << " " << st[id].mn << " " << nmn << " " << nmx << "\n";
        type = st[id].mn == nmn;
        val = type ? nmx : nmn;
        return L;
    }
    push(id);
    int M = (L + R) / 2;
    ll tmn = min(nmn, st[2 * id + 2].mn);
    ll tmx = max(nmx, st[2 * id + 2].mx);
    if(tmx - tmn >= c) return query(c, M + 1, R, 2 * id + 2, nmn, nmx);
    else return query(c, L, M, 2 * id + 1, tmn, tmx);
}

ll queryp(ll x, int L, int R, int id){
    if(L == R) return st[id].mn;
    push(id);
    int M = (L + R) / 2;
    if(x <= M) return queryp(x, L, M, 2 * id + 1);
    else return queryp(x, M + 1, R, 2 * id + 2);
}

void print(int L, int R, int id){
    if(L == R){
//        cerr << st[id].mn << " ";
        return;
    }
    push(id);
    int M = (L + R) / 2;
    print(L, M, 2 * id + 1);
    print(M + 1, R, 2 * id + 2);
}

vector<int> distribute_candies(vector<int> C, vector<int> L, vector<int> R, vector<int> V){
    int q = L.size();
    int n = C.size();

    st.resize(4 * (q + 1));

    vector<pair<pii, ll>> qry;

    for(int i = 0; i < q; i++){
        qry.eb(mp(mp(L[i], i + 1), V[i]));
        if(R[i] < n - 1) qry.eb(mp(mp(R[i] + 1, i + 1), -V[i]));
    }
    lsort(qry);

    vector<int> ans(n);
    int qp = 0;
    ll sum = 0;
    for(int i = 0; i < n; i++){
        while(qp < qry.size() && qry[qp].F.F == i){
            modify(qry[qp].F.S, q, qry[qp].S, 0, q, 0);
            sum += qry[qp].S;
            qp++;
        }

        ans[i] = min((ll)C[i], sum);

//        cerr << "test " << i << " " << st[0].mx << " " << st[0].mn << "\n";
//        print(0, q, 0);
//        cerr << "\n";

        if(st[0].mx - st[0].mn < C[i]){
            type = false;
            val = st[0].mn;
//            assert(val == 0);
        }
        else{
            int t = query(C[i], 0, q, 0);
//            assert(type && val == sum);
//            cerr << t << "\n";
        }
//        cerr << type << " " << val << " " << sum << "\n";

        if(type) ans[i] = sum - (val - C[i]);
        else ans[i] = sum - val;
    }

    return ans;
}

Compilation message

candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:157:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  157 |         while(qp < qry.size() && qry[qp].F.F == i){
      |               ~~~^~~~~~~~~~~~
candies.cpp:175:17: warning: unused variable 't' [-Wunused-variable]
  175 |             int t = query(C[i], 0, q, 0);
      |                 ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 3 ms 588 KB Output is correct
4 Correct 3 ms 588 KB Output is correct
5 Correct 3 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 480 ms 33604 KB Output is correct
2 Correct 474 ms 33624 KB Output is correct
3 Correct 484 ms 33608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 345 ms 32144 KB Output is correct
3 Correct 77 ms 6280 KB Output is correct
4 Correct 467 ms 39556 KB Output is correct
5 Correct 456 ms 39984 KB Output is correct
6 Correct 507 ms 40392 KB Output is correct
7 Correct 462 ms 39732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 157 ms 27972 KB Output is correct
4 Correct 74 ms 4240 KB Output is correct
5 Correct 283 ms 33076 KB Output is correct
6 Correct 259 ms 33692 KB Output is correct
7 Correct 254 ms 34332 KB Output is correct
8 Correct 232 ms 32976 KB Output is correct
9 Correct 270 ms 34456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 3 ms 588 KB Output is correct
4 Correct 3 ms 588 KB Output is correct
5 Correct 3 ms 716 KB Output is correct
6 Correct 480 ms 33604 KB Output is correct
7 Correct 474 ms 33624 KB Output is correct
8 Correct 484 ms 33608 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 345 ms 32144 KB Output is correct
11 Correct 77 ms 6280 KB Output is correct
12 Correct 467 ms 39556 KB Output is correct
13 Correct 456 ms 39984 KB Output is correct
14 Correct 507 ms 40392 KB Output is correct
15 Correct 462 ms 39732 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 157 ms 27972 KB Output is correct
19 Correct 74 ms 4240 KB Output is correct
20 Correct 283 ms 33076 KB Output is correct
21 Correct 259 ms 33692 KB Output is correct
22 Correct 254 ms 34332 KB Output is correct
23 Correct 232 ms 32976 KB Output is correct
24 Correct 270 ms 34456 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 72 ms 4132 KB Output is correct
27 Correct 346 ms 34740 KB Output is correct
28 Correct 425 ms 38248 KB Output is correct
29 Correct 446 ms 38576 KB Output is correct
30 Correct 474 ms 38844 KB Output is correct
31 Correct 453 ms 39092 KB Output is correct
32 Correct 447 ms 39220 KB Output is correct