• python萌新笔记


    1,输入一个以空格隔开的数组

    input()接入一整行的内容

    接受一个以空格隔开的数组时需要input().split()

    split函数是将一个字符串内以规定的(默认是空格)字符隔开成数组。

    2,global问题

    数组如果在函数中调用不需要global

    普通变量需要调用global

    线段树

     1 class Tree(object):
     2     def __init__(self):
     3         self.l=0
     4         self.r=0
     5         self.lazy=0
     6         self.v=0
     7 tr=[Tree() for i in range(100002*4)]
     8 a,Question=[],[]
     9 n,m,ans=0,0,0
    10 
    11 def built(p,l,r):
    12     tr[p].l,tr[p].r=l,r
    13     if l==r :
    14         tr[p].v=a[l]
    15         return 
    16     mid=(l+r)>>1
    17     built(p<<1,l,mid)
    18     built(p<<1|1,mid+1,r)
    19     tr[p].v=tr[p<<1].v+tr[p<<1|1].v    
    20     return
    21 
    22 def spread(p):
    23     tr[p<<1].v+=(tr[p<<1].r-tr[p<<1].l+1)*tr[p].lazy
    24     tr[p<<1|1].v+=(tr[p<<1|1].r-tr[p<<1|1].l+1)*tr[p].lazy
    25     tr[p<<1].lazy+=tr[p].lazy
    26     tr[p<<1|1].lazy+=tr[p].lazy
    27     tr[p].lazy=0
    28     return
    29 
    30 def ask(p,l,r):
    31     global ans
    32     if (tr[p].l>=l) and (tr[p].r<=r) :
    33         ans+=tr[p].v
    34         return 
    35     if tr[p].lazy!=0 :
    36         spread(p)
    37     mid=(tr[p].l+tr[p].r)>>1
    38     if mid>=l :
    39         ask(p<<1,l,r)
    40     if mid+1<=r :
    41         ask(p<<1|1,l,r)
    42        
    43 def change(p,l,r,x):
    44     if (tr[p].l>=l) and (tr[p].r<=r) :
    45         tr[p].lazy+=x
    46         tr[p].v+=(tr[p].r-tr[p].l+1)*x
    47         return 
    48     if tr[p].lazy!=0 :
    49         spread(p)
    50     mid=(tr[p].l+tr[p].r)>>1
    51     if mid>=l :
    52         change(p<<1,l,r,x)
    53     if mid+1<=r :
    54         change(p<<1|1,l,r,x)
    55     tr[p].v=tr[p<<1].v+tr[p<<1|1].v
    56 
    57 #int main
    58 nm=[int(i) for i in input().split()]
    59 n,m=nm[0],nm[1]
    60 a=[int(i) for i in input().split()]
    61 built(1,0,n-1)
    62 for i in range(0,m):
    63     Question=[int(i) for i in input().split()]
    64     w,ll,rr=Question[0],Question[1],Question[2]
    65     if w==1 :
    66         change(1,ll-1,rr-1,Question[3])
    67     elif w==2 :
    68         ans=0
    69         ask(1,ll-1,rr-1)
    70         print(ans)

     3,python多组数据

     1 while 1:
     2     try:
     3         n=int(input())
     4         list=[int(i) for i in input().split()]
     5         x=10000
     6         y=-10000
     7         for i in range(n):
     8             x=min(list[i],x)
     9             y=max(list[i],y)
    10         print(f"{y} {x}")
    11     except :
    12         break
    我已没有下降的余地
  • 相关阅读:
    同步gitlab与github
    配置hosts快速访问GitHub
    Linux下Julia安装
    LATEX图片位置
    IPOPT安装
    sqlplus传入shell变量
    users表空间满导致应用无法连接
    坏块修复 ORA-00701
    Oracle中INITRANS和MAXTRANS参数(转)
    DBMS_ROWID包的使用
  • 原文地址:https://www.cnblogs.com/znsbc-13/p/15257127.html
Copyright © 2020-2023  润新知