#include<bits/stdc++.h>
using namespace std;
#include "candies.h"
//#include "grader.cpp"
#define pb push_back
#define X first
#define Y second
#define ll long long
ll mx[800005],mn[800005],lz[800005];
vector<pair<ll,ll> > sweep[200005]; // pair (index of commands, updated value)
vector<int> ans;
void uptlz(ll tree,ll st,ll ed)
{
mx[tree]+=lz[tree],mn[tree]+=lz[tree];
if(st!=ed)
{
lz[2*tree]+=lz[tree],lz[2*tree+1]+=lz[tree];
}
lz[tree]=0;
}
void update(ll tree,ll st,ll ed,ll l,ll r,ll val)
{
ll md=(st+ed)/2;
if(st>=l&&ed<=r)
{
lz[tree]+=val;
}
uptlz(tree,st,ed);
if(st>r||ed<l)return;
if(st>=l&&ed<=r)return;
update(2*tree,st,md,l,r,val);
update(2*tree+1,md+1,ed,l,r,val);
mx[tree]=max(mx[2*tree],mx[2*tree+1]);
mn[tree]=min(mn[2*tree],mn[2*tree+1]);
}
ll query_mx(ll tree,ll st,ll ed,ll l,ll r)
{
ll md=(st+ed)/2;
uptlz(tree,st,ed);
if(st>r||ed<l)return -1e18;
if(st>=l&&ed<=r)return mx[tree];
return max(query_mx(2*tree,st,md,l,r),query_mx(2*tree+1,md+1,ed,l,r));
}
ll query_mn(ll tree,ll st,ll ed,ll l,ll r)
{
ll md=(st+ed)/2;
uptlz(tree,st,ed);
if(st>r||ed<l)return 1e18;
if(st>=l&&ed<=r)return mn[tree];
return min(query_mn(2*tree,st,md,l,r),query_mn(2*tree+1,md+1,ed,l,r));
}
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
ll n = c.size();
ll m=v.size();
for(int i=0;i<m;i++)
{
sweep[l[i]+1].pb({i+1,v[i]});
sweep[r[i]+2].pb({i+1,-v[i]});
}
/*for(int i=1;i<=n;i++)
{
printf("%d\n",i);
for(int j=0;j<sweep[i].size();j++)
{
printf("%d %d\n",sweep[i][j].X,sweep[i][j].Y);
}
printf("\n");
}*/
for(int i=1;i<=n;i++)
{
for(int j=0;j<sweep[i].size();j++)
{
int cmd=sweep[i][j].X,flag=sweep[i][j].Y;
update(1,1,m,cmd,m,flag);
}
/*printf("i=%d\n",i);
for(int j=1;j<=m;j++)
{
printf("%lld ",query(1,1,m,j,j));
}
printf("\n");*/
ll st=0,md,ed=m;
while(ed>=st)
{
md=(st+ed)/2;
ll diff;
pair<ll,ll> rng={query_mx(1,1,m,md,m),query_mn(1,1,m,md,m)};
if(md==0)
{
diff=query_mx(1,1,m,1,m);
}else
{
diff=rng.X-rng.Y;
}
if(diff>c[i-1])
{
st=md+1;
}else
{
ed=md-1;
}
}
ll fn=query_mx(1,1,m,m,m);//final poll
ll l1,r1,l2,r2,base;
if(ed==-1)
{
l1=min((ll)0,query_mn(1,1,m,1,m));
r1=query_mx(1,1,m,1,m);
ans.pb(fn-l1);
}else
{
if(ed==0)
{
l1=0;
r1=query_mx(1,1,m,1,m);
}else
{
l1=query_mn(1,1,m,ed,m);
r1=query_mx(1,1,m,ed,m);
}
l2=query_mn(1,1,m,ed+1,m);
r2=query_mx(1,1,m,ed+1,m);
if(l1==l2)
{
base=l1;
}else
{
base=r1-c[i-1];
}
ans.pb(fn-base);
}
}
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:72:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int j=0;j<sweep[i].size();j++)
| ~^~~~~~~~~~~~~~~~
candies.cpp:105:21: warning: variable 'r2' set but not used [-Wunused-but-set-variable]
105 | ll l1,r1,l2,r2,base;
| ^~
# |
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 |
4 ms |
5312 KB |
Output is correct |
4 |
Correct |
4 ms |
5280 KB |
Output is correct |
5 |
Correct |
11 ms |
5344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1887 ms |
36488 KB |
Output is correct |
2 |
Correct |
1951 ms |
39728 KB |
Output is correct |
3 |
Correct |
1928 ms |
39828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
258 ms |
31964 KB |
Output is correct |
3 |
Correct |
641 ms |
11772 KB |
Output is correct |
4 |
Correct |
1916 ms |
41772 KB |
Output is correct |
5 |
Correct |
1866 ms |
42080 KB |
Output is correct |
6 |
Correct |
1898 ms |
42488 KB |
Output is correct |
7 |
Correct |
1909 ms |
42104 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 |
129 ms |
29508 KB |
Output is correct |
4 |
Correct |
620 ms |
9728 KB |
Output is correct |
5 |
Correct |
1630 ms |
35236 KB |
Output is correct |
6 |
Correct |
1645 ms |
36004 KB |
Output is correct |
7 |
Correct |
1746 ms |
36764 KB |
Output is correct |
8 |
Correct |
1585 ms |
35016 KB |
Output is correct |
9 |
Correct |
1644 ms |
36628 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 |
4 ms |
5312 KB |
Output is correct |
4 |
Correct |
4 ms |
5280 KB |
Output is correct |
5 |
Correct |
11 ms |
5344 KB |
Output is correct |
6 |
Correct |
1887 ms |
36488 KB |
Output is correct |
7 |
Correct |
1951 ms |
39728 KB |
Output is correct |
8 |
Correct |
1928 ms |
39828 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
258 ms |
31964 KB |
Output is correct |
11 |
Correct |
641 ms |
11772 KB |
Output is correct |
12 |
Correct |
1916 ms |
41772 KB |
Output is correct |
13 |
Correct |
1866 ms |
42080 KB |
Output is correct |
14 |
Correct |
1898 ms |
42488 KB |
Output is correct |
15 |
Correct |
1909 ms |
42104 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
129 ms |
29508 KB |
Output is correct |
19 |
Correct |
620 ms |
9728 KB |
Output is correct |
20 |
Correct |
1630 ms |
35236 KB |
Output is correct |
21 |
Correct |
1645 ms |
36004 KB |
Output is correct |
22 |
Correct |
1746 ms |
36764 KB |
Output is correct |
23 |
Correct |
1585 ms |
35016 KB |
Output is correct |
24 |
Correct |
1644 ms |
36628 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
588 ms |
9800 KB |
Output is correct |
27 |
Correct |
226 ms |
33476 KB |
Output is correct |
28 |
Correct |
1784 ms |
40448 KB |
Output is correct |
29 |
Correct |
1826 ms |
40816 KB |
Output is correct |
30 |
Correct |
1850 ms |
41028 KB |
Output is correct |
31 |
Correct |
1839 ms |
41288 KB |
Output is correct |
32 |
Correct |
1898 ms |
41536 KB |
Output is correct |