Submission #548414

# Submission time Handle Problem Language Result Execution time Memory
548414 2022-04-13T09:59:32 Z topovik Distributing Candies (IOI21_candies) C++17
11 / 100
348 ms 36472 KB
#include <bits/stdc++.h>
#include "candies.h"
#define pb push_back
#define f first
#define s second

using namespace std;

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

const ll oo = 1e9 + 7;

const ll N = 1e6;
const ll M = 21;
const ll M1 = 1e6;

ll a[N];

struct tree
{
    tree *L = NULL;
    tree *R = NULL;

    ll l, r;
    ll sum = 0;
    ll mdl;

    tree(ll _l, ll _r) : l(_l), r(_r)
    {
        if (l == r) return;
        mdl = (l + r) >> 1;
        L = new tree(l, mdl);
        R = new tree(mdl + 1, r);
    }
    void upd(ll _l, ll _r, ll vl)
    {
        if (_l > _r) return;
        if (_l == l && _r == r)
        {
            sum += vl;
            return;
        }
        L -> upd(_l, min(mdl, _r), vl);
        R -> upd(max(mdl + 1, _l), _r, vl);
    }

    ll val(ll x)
    {
        if (l == r) return sum;
        L -> sum += sum;
        R -> sum += sum;
        sum = 0;
        if (x <= mdl) return L -> val(x);
        else return R -> val(x);
    }
};

std::vector<int> distribute_candies (std::vector<int> c, std::vector<int> l,
                                     std::vector<int> r, std::vector<int> v) {
   int n = c.size();
   int q = l.size();
    if (n <= 2000 && q <= 2000)
    {
        for (int i = 0; i < q; i++)
        {
            for (int j = l[i]; j <= r[i]; j++) a[j] += v[i];
            for (int j = 0; j < n; j++)
            {
                if (a[j] < 0) a[j] = 0;
                if (a[j] > c[j]) a[j] = c[j];
            }
        }
        vector <int> ans(n);
        for (int i = 0; i < n; i++) ans[i] = a[i];
        return ans;
    }
    else
    {
        tree *root = new tree(0, n - 1);
        for (int i = 0; i < q; i++) root -> upd(l[i], r[i], v[i]);
        vector <int> ans(n);
        for (int i = 0; i < n; i++) ans[i] = min((ll)c[i], root -> val(i));
        return ans;
    }
}

//int main()
//{
//    ios_base::sync_with_stdio(0);
//    iostream::sync_with_stdio(0);
//    cin.tie(0);
//    cout.tie(0);
//    int n;
//    cin >> n;
//    vector <int> c(n);
//    for (int i = 0; i < n; i++) cin >> c[i];
//    int q;
//    cin >> q;
//    vector <int> l(q), r(q), v(q);
//    for (int i = 0; i < q; i++) cin >> l[i] >> r[i] >> v[i];
//    vector <int> ans = distribute_candies(c, l, r, v);
//    for (int i = 0; i < n; i++) cout << ans[i] << " ";
//}
/*
5 3
2 1 1 2 2 3
2 3 3 4 4 5
4 1 1 2 2 3 3 4 4 5
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 13 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 332 ms 32368 KB Output is correct
2 Correct 348 ms 36472 KB Output is correct
3 Correct 341 ms 36332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 102 ms 5344 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 60 ms 5216 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 13 ms 384 KB Output is correct
6 Correct 332 ms 32368 KB Output is correct
7 Correct 348 ms 36472 KB Output is correct
8 Correct 341 ms 36332 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Incorrect 102 ms 5344 KB Output isn't correct
11 Halted 0 ms 0 KB -