Submission #623280

# Submission time Handle Problem Language Result Execution time Memory
623280 2022-08-05T11:55:24 Z Hanksburger Distributing Candies (IOI21_candies) C++17
100 / 100
369 ms 38992 KB
#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
long long seg[800005], mx[800005], mn[800005];
vector<long long> vec[200005];
vector<int> ans;
void update(long long i, long long l, long long r, long long x, long long y)
{
    if (l==r)
    {
        seg[i]=mx[i]=mn[i]=y;
        return;
    }
    long long mid=(l+r)/2;
    if (x<=mid)
        update(i*2, l, mid, x, y);
    else
        update(i*2+1, mid+1, r, x, y);
    seg[i]=seg[i*2]+seg[i*2+1];
    mx[i]=max(mx[i*2], seg[i*2]+mx[i*2+1]);
    mn[i]=min(mn[i*2], seg[i*2]+mn[i*2+1]);
}
long long query(long long i, long long l, long long r, long long x)
{
//    cout << "query " << i << ' ' << l << ' ' << r << ' ' << x << '\n';
    if (l==r)
        return max(0LL, min(x, seg[i]));
    long long mid=(l+r)/2, res;
    if (mx[i*2+1]-mn[i*2+1]>=x)
        return query(i*2+1, mid+1, r, x);
    res=query(i*2, l, mid, x);
    if (res+mx[i*2+1]>=x)
        return x-(mx[i*2+1]-seg[i*2+1]);
    if (res+mn[i*2+1]<=0)
        return seg[i*2+1]-mn[i*2+1];
    return res+seg[i*2+1];
}
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v)
{
    long long n=c.size(), q=v.size();
    for (long long i=0; i<q; i++)
    {
        vec[l[i]].push_back(i);
        vec[r[i]+1].push_back(i);
    }
    for (long long i=0; i<n; i++)
    {
        for (long long j:vec[i])
        {
            if (l[j]==i)
            {
                update(1, 0, q-1, j, v[j]);
//                cout << "update " << j << ' ' << v[j] << '\n';
            }
            else
            {
                update(1, 0, q-1, j, 0);
//                cout << "update " << j << ' ' << 0 << '\n';
            }
        }
        ans.push_back(query(1, 0, q-1, c[i]));
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 4 ms 5204 KB Output is correct
5 Correct 5 ms 5280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 369 ms 32280 KB Output is correct
2 Correct 332 ms 36452 KB Output is correct
3 Correct 368 ms 36192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 208 ms 29500 KB Output is correct
3 Correct 60 ms 11800 KB Output is correct
4 Correct 343 ms 38148 KB Output is correct
5 Correct 360 ms 38608 KB Output is correct
6 Correct 353 ms 38992 KB Output is correct
7 Correct 310 ms 38272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 91 ms 25924 KB Output is correct
4 Correct 53 ms 8644 KB Output is correct
5 Correct 141 ms 29132 KB Output is correct
6 Correct 155 ms 29704 KB Output is correct
7 Correct 142 ms 29780 KB Output is correct
8 Correct 140 ms 31356 KB Output is correct
9 Correct 155 ms 32168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 4 ms 5204 KB Output is correct
5 Correct 5 ms 5280 KB Output is correct
6 Correct 369 ms 32280 KB Output is correct
7 Correct 332 ms 36452 KB Output is correct
8 Correct 368 ms 36192 KB Output is correct
9 Correct 4 ms 4948 KB Output is correct
10 Correct 208 ms 29500 KB Output is correct
11 Correct 60 ms 11800 KB Output is correct
12 Correct 343 ms 38148 KB Output is correct
13 Correct 360 ms 38608 KB Output is correct
14 Correct 353 ms 38992 KB Output is correct
15 Correct 310 ms 38272 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 91 ms 25924 KB Output is correct
19 Correct 53 ms 8644 KB Output is correct
20 Correct 141 ms 29132 KB Output is correct
21 Correct 155 ms 29704 KB Output is correct
22 Correct 142 ms 29780 KB Output is correct
23 Correct 140 ms 31356 KB Output is correct
24 Correct 155 ms 32168 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 66 ms 9696 KB Output is correct
27 Correct 212 ms 29132 KB Output is correct
28 Correct 353 ms 36964 KB Output is correct
29 Correct 333 ms 37296 KB Output is correct
30 Correct 339 ms 37384 KB Output is correct
31 Correct 358 ms 37604 KB Output is correct
32 Correct 339 ms 37960 KB Output is correct