답안 #939546

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
939546 2024-03-06T13:45:22 Z IS_Rushdi 사탕 분배 (IOI21_candies) C++17
0 / 100
84 ms 54772 KB
#include "candies.h" 
#include<bits/stdc++.h>
using namespace std;
typedef int64_t i64;
#define VII vector<int>


struct seg{
    vector<i64>mx,mn,cmx,cmn,op;
    int sz = 1;
    void build(int x,int lx,int rx){
        cmx[x] = cmn[x] = rx-lx;
        int m = (lx+rx)/2;
        build(x*2+1,lx,m);
        build(x*2+2,m,rx);
    }
    seg(int n){
        while(sz < n) sz*=2;
        op.assign(sz*2,0ll);
        mx.assign(sz*2,0ll);
        mn.assign(sz*2,0ll);
        cmx.assign(sz*2,0ll);
        cmn.assign(sz*2,0ll);
        build(0,0,sz);
    }
    void update(int l,int r,int v,int x=0,int lx=0,int rx=-1){
        if(rx == -1) rx = sz;
        if(rx <= l || lx >= r) return;
        if(rx <= r && lx >= l){
            mn[x] += v;
            mx[x] += v;
            op[x] += v;
            return;
        }
        int m = (lx+rx)/2;
        update(l,r,v,x*2+1,lx,m);
        update(l,r,v,x*2+2,m,rx);
        
        mx[x] = max(mx[x*2+1],mx[x*2+2]) + op[x];
        mn[x] = min(mn[x*2+1],mn[x*2+2]) + op[x];
        
        if(mx[x*2+1] > mx[x*2+2]) cmx[x] = cmx[x*2+1];
        else if(mx[x*2+2] > mx[x*2+1]) cmx[x] = cmx[x*2+2];
        else cmx[x] = cmx[x*2+1] + cmx[x*2+2];
        
        if(mn[x*2+1] < mn[x*2+2]) cmn[x] = cmn[x*2+1];
        else if(mn[x*2+2] < mn[x*2+1]) cmn[x] = cmn[x*2+2];
        else cmn[x] = cmn[x*2+1] + cmn[x*2+2];
    }
    void checkMAX(int v,int x=0,int lx=0,int rx=-1){
        if(rx == -1) rx = sz;
        if(mx[x] > v){
            if(cmx[x] == rx-lx){
                op[x] -= mx[x]-v;
                mx[x] = v;
            }else{
                int m = (lx+rx)/2;
                checkMAX(v,x*2+1,lx,m);
                checkMAX(v,x*2+2,m,rx);
            }
        }
    }
    void checkMIN(int x=0,int lx=0,int rx=-1){
        if(rx == -1) rx = sz;
        if(mn[x] < 0){
            if(cmx[x] == rx-lx){
                op[x] += -mn[x];
                mn[x] = 0;
            }else{
                int m = (lx+rx)/2;
                checkMIN(x*2+1,lx,m);
                checkMIN(x*2+2,m,rx);
            }
        }
    }
    i64 get(int i,int x=0,int lx=0,int rx=-1){
        if(rx == -1) rx = sz;
        if(rx-lx == 1) return op[x];
        
        int m = (lx+rx)/2;
        if(i < m) return op[x] + get(i,x*2+1,lx,m);
        else return op[x] + get(i,x*2+2,m,rx);
    }
};
VII distribute_candies(VII c,VII l ,VII r,VII v) {
    int n = c.size();
    int q = l.size();
    seg st(n);
    for(int i = 0; i < q; i++){
        st.update(l[i],r[i],v[i]);
        st.checkMAX(c[0]);
        st.checkMIN();
    }
    vector<int>ans;
    for(int i = 0; i < n; i++) ans.push_back(st.get(i));
    return ans;
}

// int main(){
    
// }
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 348 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 84 ms 54772 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 348 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -