Submission #443483

# Submission time Handle Problem Language Result Execution time Memory
443483 2021-07-10T15:08:14 Z leinad2 Distributing Candies (IOI21_candies) C++17
100 / 100
2923 ms 38436 KB
#include "candies.h"
#include<bits/stdc++.h>
using namespace std;
long long mx[800010], mn[800010], lazy[800010], inf=9000000000000000000;
vector<pair<int, int> >upd[200010];
void busy(int id)
{
    lazy[id*2]+=lazy[id];
    lazy[id*2+1]+=lazy[id];
    mx[id*2]+=lazy[id];
    mx[id*2+1]+=lazy[id];
    mn[id*2]+=lazy[id];
    mn[id*2+1]+=lazy[id];
    lazy[id]=0;
}
void update(int id, int s, int e, int l, int r, int v)
{
    if(e<l||r<s)return;
    if(l<=s&&e<=r)
    {
        mx[id]+=v;
        mn[id]+=v;
        lazy[id]+=v;
        return;
    }
    int m=s+e>>1;busy(id);
    update(id*2, s, m, l, r, v);update(id*2+1, m+1, e, l, r, v);
    mx[id]=max(mx[id*2], mx[id*2+1]);
    mn[id]=min(mn[id*2], mn[id*2+1]);
}
long long get1(int id, int s, int e, int l, int r)
{
    if(e<l||r<s)return -inf;
    if(l<=s&&e<=r)return mx[id];
    int m=s+e>>1;busy(id);
    return max(get1(id*2, s, m, l, r), get1(id*2+1, m+1, e, l, r));
}
long long get2(int id, int s, int e, int l, int r)
{
    if(e<l||r<s)return inf;
    if(l<=s&&e<=r)return mn[id];
    int m=s+e>>1;busy(id);
    return min(get2(id*2, s, m, l, r), get2(id*2+1, m+1, e, l, r));
}
vector<int>distribute_candies(vector<int>C, vector<int>l, vector<int>r, vector<int>v)
{
    int n=C.size(), q=l.size(), i, j, k, a, b;
    vector<long long>c;for(i=0;i<n;i++)c.push_back(C[i]);
    for(i=0;i<q;i++)
    {
        upd[l[i]].push_back({v[i], i});
        upd[r[i]+1].push_back({-v[i], i});
    }
    vector<int>ans;ans.resize(n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<upd[i].size();j++)
        {
            a=upd[i][j].first;b=upd[i][j].second;
            update(1, 1, q+1, b+2, q+1, a);
        }
        long long x=get1(1, 1, q+1, 1, q+1)-get2(1, 1, q+1, 1, q+1);
        if(x<=c[i])ans[i]=get1(1, 1, q+1, q+1, q+1)-get2(1, 1, q+1, 1, q+1);
        else
        {
            int a=1, b=q+1;
            while(a<b)
            {
                int m=a+b+1>>1;
                if(get1(1, 1, q+1, m, q+1)-get2(1, 1, q+1, m, q+1)<=c[i])b=m-1;
                else a=m;
            }
            if(get1(1, 1, q+1, a, a)<get2(1, 1, q+1, a+1, q+1))
            {
                ans[i]=c[i]-get1(1, 1, q+1, a+1, q+1)+get1(1, 1, q+1, q+1, q+1);
            }
            else
            {
                ans[i]=get1(1, 1, q+1, q+1, q+1)-get2(1, 1, q+1, a+1, q+1);
            }
        }
    }
    return ans;
}

Compilation message

candies.cpp: In function 'void update(int, int, int, int, int, int)':
candies.cpp:26:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   26 |     int m=s+e>>1;busy(id);
      |           ~^~
candies.cpp: In function 'long long int get1(int, int, int, int, int)':
candies.cpp:35:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   35 |     int m=s+e>>1;busy(id);
      |           ~^~
candies.cpp: In function 'long long int get2(int, int, int, int, int)':
candies.cpp:42:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   42 |     int m=s+e>>1;busy(id);
      |           ~^~
candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:57:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         for(j=0;j<upd[i].size();j++)
      |                 ~^~~~~~~~~~~~~~
candies.cpp:69:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   69 |                 int m=a+b+1>>1;
      |                       ~~~^~
candies.cpp:47:39: warning: unused variable 'k' [-Wunused-variable]
   47 |     int n=C.size(), q=l.size(), i, j, k, a, b;
      |                                       ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 5 ms 5196 KB Output is correct
4 Correct 5 ms 5196 KB Output is correct
5 Correct 13 ms 5156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2833 ms 32912 KB Output is correct
2 Correct 2640 ms 33076 KB Output is correct
3 Correct 2316 ms 32888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 328 ms 26540 KB Output is correct
3 Correct 806 ms 9104 KB Output is correct
4 Correct 2923 ms 32948 KB Output is correct
5 Correct 2664 ms 32948 KB Output is correct
6 Correct 2598 ms 32948 KB Output is correct
7 Correct 2457 ms 32952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 5 ms 4940 KB Output is correct
3 Correct 161 ms 25336 KB Output is correct
4 Correct 697 ms 9108 KB Output is correct
5 Correct 1738 ms 30168 KB Output is correct
6 Correct 1969 ms 34484 KB Output is correct
7 Correct 2263 ms 34996 KB Output is correct
8 Correct 1577 ms 33656 KB Output is correct
9 Correct 2383 ms 35056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 5 ms 5196 KB Output is correct
4 Correct 5 ms 5196 KB Output is correct
5 Correct 13 ms 5156 KB Output is correct
6 Correct 2833 ms 32912 KB Output is correct
7 Correct 2640 ms 33076 KB Output is correct
8 Correct 2316 ms 32888 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 328 ms 26540 KB Output is correct
11 Correct 806 ms 9104 KB Output is correct
12 Correct 2923 ms 32948 KB Output is correct
13 Correct 2664 ms 32948 KB Output is correct
14 Correct 2598 ms 32948 KB Output is correct
15 Correct 2457 ms 32952 KB Output is correct
16 Correct 4 ms 4940 KB Output is correct
17 Correct 5 ms 4940 KB Output is correct
18 Correct 161 ms 25336 KB Output is correct
19 Correct 697 ms 9108 KB Output is correct
20 Correct 1738 ms 30168 KB Output is correct
21 Correct 1969 ms 34484 KB Output is correct
22 Correct 2263 ms 34996 KB Output is correct
23 Correct 1577 ms 33656 KB Output is correct
24 Correct 2383 ms 35056 KB Output is correct
25 Correct 4 ms 5000 KB Output is correct
26 Correct 459 ms 10296 KB Output is correct
27 Correct 308 ms 29136 KB Output is correct
28 Correct 1775 ms 37412 KB Output is correct
29 Correct 2253 ms 37920 KB Output is correct
30 Correct 2500 ms 38088 KB Output is correct
31 Correct 2672 ms 38372 KB Output is correct
32 Correct 2662 ms 38436 KB Output is correct