답안 #1068214

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1068214 2024-08-21T08:36:42 Z 정희우(#11129) Weirdtree (RMI21_weirdtree) C++17
13 / 100
136 ms 72532 KB
#include "weirdtree.h"
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;
using lint = long long;
using vint = vector<int>;
using pii = pair<int,int>;

const int MAX_N=300010;

struct Seg
{
    lint sum,fv,sv,c;
    void setv(int h)
    {
        sum=fv=h;
        sv=-1;c=1;
    }
};

int n,q;
Seg seg[MAX_N<<2];

void merge_(Seg &p,const Seg &l,const Seg &r)
{
    p.sum=l.sum+r.sum;
    p.fv=max(l.fv,r.fv);
    p.sv=max(l.sv,r.sv);
    if(p.fv!=l.fv)p.sv=max(p.sv,l.fv);
    if(p.fv!=r.fv)p.sv=max(p.sv,r.fv);
    p.c=l.c*(p.fv==l.fv)+r.c*(p.fv==r.fv);
}

void update_down(const Seg &p,Seg &c)
{
    if(c.fv<=p.fv)return;
    c.sum-=(c.fv-p.fv)*c.c;
    c.fv=p.fv;
}

void update_lazy(int i)
{
    update_down(seg[i],seg[i<<1]);
    update_down(seg[i],seg[i<<1|1]);
}

void update_one(int i,int s,int e,int x,int h)
{
    if(s>x || e<=x)return;
    if(s==x && x+1==e)
    {
        seg[i].setv(h);
        return;
    }
    update_lazy(i);
    update_one(i<<1,s,(s+e)>>1,x,h);
    update_one(i<<1|1,(s+e)>>1,e,x,h);
    merge_(seg[i],seg[i<<1],seg[i<<1|1]);
}

void update_second(int i,int s,int e)
{
    if(seg[i].fv>seg[i].sv)return;
    update_lazy(i);
    update_second(i<<1,s,(s+e)>>1);
    update_second(i<<1|1,(s+e)>>1,e);
    merge_(seg[i],seg[i<<1],seg[i<<1|1]);
}

void update_cut(int i,int s,int e,int l,int r,int h,int dh)
{
    if(s>=r || e<=l || seg[i].fv<h)return;
    if(l<=s && e<=r)
    {
        seg[i].sum-=seg[i].c*dh;
        seg[i].fv-=dh;
        update_second(i,s,e);
        return;
    }
    update_lazy(i);
    update_cut(i<<1,s,(s+e)>>1,l,r,h,dh);
    update_cut(i<<1|1,(s+e)>>1,e,l,r,h,dh);
    merge_(seg[i],seg[i<<1],seg[i<<1|1]);
}

int update_left(int i,int s,int e,int l,int r,int h,int c)
{
    if(s>=r || e<=l || seg[i].fv<h || c==0)return 0;
    if(l<=s && e<=r && seg[i].c<=c)
    {
        seg[i].sum-=seg[i].c;
        seg[i].fv--;
        update_second(i,s,e);
        return seg[i].c;
    }
    update_lazy(i);
    int u=update_left(i<<1,s,(s+e)>>1,l,r,h,c);
    u+=update_left(i<<1|1,(s+e)>>1,e,l,r,h,c-u);
    merge_(seg[i],seg[i<<1],seg[i<<1|1]);
    return u;
}

Seg find_(int i,int s,int e,int l,int r)
{
    if(s>=r || e<=l)return {0,-1,-1,0};
    if(l<=s && e<=r)return seg[i];
    update_lazy(i);
    Seg lv=find_(i<<1,s,(s+e)>>1,l,r);
    Seg rv=find_(i<<1|1,(s+e)>>1,e,l,r);
    Seg p;
    merge_(p,lv,rv);
    return p;
}

void initialise(int N, int Q, int h[])
{
	n=N,q=Q;
    for(int i=0;i<n;i++)
        update_one(1,0,n,i,h[i+1]);
}

void cut(int l, int r, int k)
{
	while(1)
    {
        Seg v=find_(1,0,n,l-1,r);
		v.sv=max(v.sv,0LL);
        if(v.fv==0)break;
        if((v.fv-v.sv)*v.c<=k)
        {
            k-=(v.fv-v.sv)*v.c;
            update_cut(1,0,n,l-1,r,v.fv,v.fv-v.sv);
        }
        else
        {
            update_cut(1,0,n,l-1,r,v.fv,k/v.c);
            update_left(1,0,n,l-1,r,v.fv-k/v.c,k%v.c);
            break;
        }
    }
}

void magic(int i, int x)
{
	update_one(1,0,n,i-1,x);
}

lint inspect(int l, int r)
{
	Seg v=find_(1,0,n,l-1,r);
	return v.sum;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 77 ms 11368 KB Output is correct
4 Incorrect 83 ms 11344 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Runtime error 115 ms 72532 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 136 ms 42752 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 77 ms 11368 KB Output is correct
4 Incorrect 83 ms 11344 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 77 ms 11368 KB Output is correct
4 Incorrect 83 ms 11344 KB Output isn't correct
5 Halted 0 ms 0 KB -