Paste: 3

Author: 3
Mode: python
Date: Mon, 1 Nov 2021 14:12:19
Plain Text |
def createf(pat,siz,fs,ms2):
    x=pat.find("/")
    if x<0:
        if len(pat)==0:
            return 0,0
        x=len(pat)
    file=pat[0:x]
    # print(file)
    pat=pat[x+1:]
    if len(pat)==0:
        if file in fs["son"].keys():
            tmp=fs["son"][file]
            if tmp["t"]==0:
                return 0,0
            if fs["ls1"]>0 and fs["s1"]+siz-tmp["s1"]>fs["ls1"]:
                return 0,0
            if siz-tmp["s1"]>ms2:
                return 0,0
            adds=siz-tmp["s1"]
            tmp["s1"]=siz
            fs["s1"]+=adds
            fs["s2"]+=adds
            return 1,adds
        else:
            if fs["ls1"]>0 and fs["s1"]+siz>fs["ls1"]:
                return 0,0
            if siz>ms2:
                return 0,0
            fs["son"][file]={"t": 1, "ls1": 0, "ls2": 0, "s1": siz, "s2": 0}
            fs["s1"]+=siz
            fs["s2"]+=siz
            return 1,siz
    else:
        if file in fs["son"].keys():
            tmp=fs["son"][file]
            if tmp["t"]==1:
                return 0,0
            if tmp["ls2"]>0:
                ms2=min(ms2,tmp["ls2"]-tmp["s2"])
            res,adds=createf(pat,siz,fs["son"][file],ms2)
            if res:
                fs["s2"]+=adds
            return res,adds
        else:
            fs["son"][file]={"t": 0, "ls1": 0, "ls2": 0, "s1": 0, "s2": 0,"son":{}}
            res,adds=createf(pat,siz,fs["son"][file],ms2)
            if res:
                fs["s2"]+=adds
            return res,adds


def removef(pat,fs):
    x=pat.find("/")
    if x<0:
        if len(pat)==0:
            return 0,0
        x=len(pat)
    file=pat[0:x]
    pat=pat[x+1:]

    if len(pat)==0:
        if file in fs["son"].keys():
            tmp=fs["son"].pop(file)
            if tmp["t"]==0:
                subs=tmp["s2"]
            else:
                subs=tmp["s1"]
            fs["s1"]-=subs
            fs["s2"]-=subs

            return subs
        else:
            return 0
    else:
        if file in fs["son"].keys():
            tmp=fs["son"][file]
            if tmp["t"]==1:
                return 0
            subs=removef(pat,tmp)
            fs["s2"]-=subs
            return subs
        else:
            return 0

def changef(pat,ls1,ls2,fs):
    x=pat.find("/")
    if x<0:
        if len(pat)==0:
            return 0,0
        x=len(pat)
    file=pat[0:x]
    pat=pat[x+1:]

    if len(pat)==0:
        if file in fs["son"].keys():
            tmp=fs["son"][file]
            if tmp["t"]==1:
                return 0
            if ls1:
                if ls1<tmp["s1"]:
                    return 0
                tmp["ls1"]=ls1
            if ls2:
                if ls2<tmp["s2"]:
                    return 0
                tmp["ls2"]=ls2
            return 1
        else:
            return 0
    else:
        if file in fs["son"].keys():
            return changef(pat,ls1,ls2,fs["son"][file])
        else:
            return 0

def chroot(fs,ls1,ls2):
    tmp = fs
    if ls1:
        if ls1 < tmp["s1"]:
            return 0
        tmp["ls1"] = ls1
    if ls2:
        if ls2 < tmp["s2"]:
            return 0
        tmp["ls2"] = ls2
    return 1


if __name__ == "__main__":
    inf = 1e15
    fs = {"t": 0, "ls1": 0, "ls2": 0, "s1": 0, "s2": 0, "son": {}}
    n = int(input())
    for T in range(n):
        ags = input().split(" ")
        if ags[0] == "C":
            # print(fs)
            res,_=createf(ags[1][1:], int(ags[2]), fs, fs["ls2"]-fs["s2"] if fs["ls2"] else inf)
            if res:
                print("Y")
            else:
                print("N")
        elif ags[0]== "R":
            res=removef(ags[1][1:],fs)
            print("Y")
            # print(fs)
        elif ags[0]=="Q":
            if ags[1]=="/":
                res=chroot(fs,int(ags[2]),int(ags[3]))
            else:
                res=changef(ags[1][1:],int(ags[2]),int(ags[3]),fs)
            if res:
                print("Y")
            else:
                print("N")
            # print(fs)

New Annotation

Summary:
Author:
Mode:
Body: