Submission #713388

# Submission time Handle Problem Language Result Execution time Memory
713388 2023-03-21T20:32:46 Z Pherokung Distributing Candies (IOI21_candies) C++17
3 / 100
393 ms 34044 KB
#include "candies.h"
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define INF 1000000007
#define F first
#define S second
#define pb push_back
typedef pair<int,pair<pair<int,int>, pair<int,int> > > pa;
int n,q,a[MAXN];
vector<pair<int,int> > vec;
vector<int> add[MAXN],del[MAXN];
struct node{
    int mx,mi,pmx,pmi,val;
    node *l, *r;
    node() : pmx(-1),pmi(-1),mx(-INF),mi(INF),val(0),l(NULL),r(NULL) {}
    node(node *_l,node *_r) : pmx(-1),pmi(-1),mx(-INF),mi(INF),val(0),l(_l),r(_r) {}
} *top;


node *build(int be,int ed){
    if(be == ed) return new node();
    int mid = (be+ed)/2;
    return new node(build(be,mid),build(mid+1,ed));
}

void update(node *pos,int be,int ed,int i,int x){
    if(be > ed || i < be || ed < i) return;
    if(be == ed){
        pos->val += x;
        pos->mx = pos->val, pos->pmx = be;
        pos->mi = pos->val, pos->pmi = be;
        return;
    }
    int mid = (be+ed)/2;
    update(pos->l,be,mid,i,x), update(pos->r,mid+1,ed,i,x);
    pos->val = pos->l->val + pos->r->val;
    pos->mx = max(pos->l->mx, pos->l->val + pos->r->mx);
    pos->pmx = pos->l->mx > (pos->l->val + pos->r->mx) ? pos->l->pmx : pos->r->pmx;
    pos->mi = min(pos->l->mi, pos->l->val + pos->r->mi);
    pos->pmi = pos->l->mi < (pos->l->val + pos->r->mi) ? pos->l->pmi : pos->r->pmi;
}

pa query(node *pos,int be,int ed,int x,int y){
    if(be > ed || ed < x || y < be) return {0,{{-INF,-1},{INF,-1}}};
    if(x <= be && ed <= y){
        return {pos->val,{{pos->mx,pos->pmx},{pos->mi,pos->pmi}}};
    };
    int mid = (be+ed)/2;
    pa L = query(pos->l,be,mid,x,y), R = query(pos->r,mid+1,ed,x,y);
    return {L.F+R.F,{{max(L.S.F.F,L.F + R.S.F.F), (L.S.F.F > L.F + R.S.F.F) ? L.S.F.S : R.S.F.S},{min(L.S.S.F,L.F + R.S.S.F), (L.S.S.F < L.F + R.S.S.F) ? L.S.S.S : R.S.S.S}}};
}

int remain(int c,int all){
    int be = 1, ed = all+2,val = 0;
    while(be <= ed){
        int mid = (be+ed)/2;
        pa p = query(top,1,all+2,mid,all+2);
        int va = p.F, mx = p.S.F.F, px = p.S.F.S, mi = p.S.S.F, pi = p.S.S.S;
        //printf("OOO %d %d %d : %d %d\n",be,mid,ed,mx,mi);
        if(mx-mi > c) be = mid+1; 
        else ed = mid-1;
    }

    //printf("|| %d %d\n",c,ed);
    pa p = query(top,1,all+2,ed,all+2);
    int va = p.F, mx = p.S.F.F, px = p.S.F.S, mi = p.S.S.F, pi = p.S.S.S;
    val = query(top,1,all+2,max(px,pi)+1,all+2).F;
    if(px > pi) val += c;

    //printf(">>>>> %d %d , %d\n",px,pi,val);

    return val;
}


vector<int> distribute_candies(vector<int> c, vector<int> l,
                                    vector<int> r, vector<int> v) {
    int n = c.size(), q = l.size();
    for(int i=1;i<=n;i++) a[i] = c[i-1];
    for(int i=0;i<q;i++){
        l[i]++, r[i]++;
        add[l[i]].pb(i), del[r[i]].pb(i);
    } 

    vector<int> ans;
    top = build(1,q+2);
    update(top,1,q+2,1,1e9);
    update(top,1,q+2,2,-1e9);
    
    for(int i=1;i<=n;i++){
        for(auto id : add[i]){
            update(top,1,q+2,id+3,v[id]);
        }
        // for(int j=1;j<=q+2;j++){
        //     printf("(%d,%d) ",query(top,1,q+2,j,q+2).S.F.F,query(top,1,q+2,j,q+2).S.S.F);
        // }
        printf("\n\n");
        ans.pb(remain(a[i],q));
        for(auto id : del[i]){
            update(top,1,q+2,id+3,-v[id]);
        }
    }
    return ans;
}

Compilation message

candies.cpp: In constructor 'node::node()':
candies.cpp:14:19: warning: 'node::pmi' will be initialized after [-Wreorder]
   14 |     int mx,mi,pmx,pmi,val;
      |                   ^~~
candies.cpp:14:9: warning:   'int node::mx' [-Wreorder]
   14 |     int mx,mi,pmx,pmi,val;
      |         ^~
candies.cpp:16:5: warning:   when initialized here [-Wreorder]
   16 |     node() : pmx(-1),pmi(-1),mx(-INF),mi(INF),val(0),l(NULL),r(NULL) {}
      |     ^~~~
candies.cpp: In constructor 'node::node(node*, node*)':
candies.cpp:14:19: warning: 'node::pmi' will be initialized after [-Wreorder]
   14 |     int mx,mi,pmx,pmi,val;
      |                   ^~~
candies.cpp:14:9: warning:   'int node::mx' [-Wreorder]
   14 |     int mx,mi,pmx,pmi,val;
      |         ^~
candies.cpp:17:5: warning:   when initialized here [-Wreorder]
   17 |     node(node *_l,node *_r) : pmx(-1),pmi(-1),mx(-INF),mi(INF),val(0),l(_l),r(_r) {}
      |     ^~~~
candies.cpp: In function 'int remain(int, int)':
candies.cpp:59:13: warning: unused variable 'va' [-Wunused-variable]
   59 |         int va = p.F, mx = p.S.F.F, px = p.S.F.S, mi = p.S.S.F, pi = p.S.S.S;
      |             ^~
candies.cpp:59:37: warning: unused variable 'px' [-Wunused-variable]
   59 |         int va = p.F, mx = p.S.F.F, px = p.S.F.S, mi = p.S.S.F, pi = p.S.S.S;
      |                                     ^~
candies.cpp:59:65: warning: unused variable 'pi' [-Wunused-variable]
   59 |         int va = p.F, mx = p.S.F.F, px = p.S.F.S, mi = p.S.S.F, pi = p.S.S.S;
      |                                                                 ^~
candies.cpp:67:9: warning: unused variable 'va' [-Wunused-variable]
   67 |     int va = p.F, mx = p.S.F.F, px = p.S.F.S, mi = p.S.S.F, pi = p.S.S.S;
      |         ^~
candies.cpp:67:19: warning: unused variable 'mx' [-Wunused-variable]
   67 |     int va = p.F, mx = p.S.F.F, px = p.S.F.S, mi = p.S.S.F, pi = p.S.S.S;
      |                   ^~
candies.cpp:67:47: warning: unused variable 'mi' [-Wunused-variable]
   67 |     int va = p.F, mx = p.S.F.F, px = p.S.F.S, mi = p.S.S.F, pi = p.S.S.S;
      |                                               ^~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 4 ms 5008 KB Output is correct
3 Correct 5 ms 5204 KB Output is correct
4 Correct 6 ms 5204 KB Output is correct
5 Correct 8 ms 5400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 85 ms 28228 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Incorrect 393 ms 34044 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5004 KB Output is correct
3 Correct 161 ms 32744 KB Output is correct
4 Runtime error 32 ms 15140 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 4 ms 5008 KB Output is correct
3 Correct 5 ms 5204 KB Output is correct
4 Correct 6 ms 5204 KB Output is correct
5 Correct 8 ms 5400 KB Output is correct
6 Runtime error 85 ms 28228 KB Execution killed with signal 11
7 Halted 0 ms 0 KB -