輸入方式
'''
def main():
Do somthing
if __name__ == '__main__':
t = int(input())
for i in range(t):
main()
'''
for T in range(0,int(input())): #T組數據
N=int(input())
n,m=map(int,input().split())
s=input()
s=[int(x) for x in input().split()] #一行輸入的數組
for i in range(0,len(s)):
a,b=map(int,input().split())
while True: #未知多組數據
try:
#n,m=map(int,input().split())
#print(n+m,end="\n")
except EOFError: #捕獲到異常
break
一些基本數據結構
python中的棧和隊列可以使用列表來模擬,或者import deque
匿名函數使用lambda關鍵字來定義lambda 參數: 表達式
#使用中括號[]定義一個列表
# l=[23,'wtf',3.14]
list.append(obj)#將obj添加到list末尾,O(1)
list.insert(index,obj)#將obj插入列表index位置,O(n)
list.pop([index=-1])#移除元素並返回該元素
list.sort(key=None,reverse=False)#默認升序排序,O(nlogn)
list.reverse()#反轉列表元素
list.clear()
len(list)#列表元素個數,O(1)
max(list)#返回列表元素最大值,O(n)
del list[2]#刪除list中第三個元素
#用小括號定義一個元組,可以當作不能修改的list
# t=(23,'wtf',3.14)
#用花括號{}定義一個字典
d={key1:value1,key2:value2}#通過key訪問value
print(d[key1])#輸出value1
if key in dict : #key不存在會報錯,要先詢問
do somthing #或者使用
d.get(key)
for key in d: #遍歷字典d
print(key,':',d.get(key))
dMerge=dict(d1,**d2)#將d1和d2合併爲dMerge
#調用set()方法創建集合
s=set([1,2,3])#定義
s.add(4)#添加
s.remove(4)#刪除
math庫
import math
math.e #常量e,2.718281828459045
math.pi #常量pi,3.141592653589793
math.factorial(x) #x的階乘
math.gcd(x,y) #x,y的gcd
math.sqrt(x) #x的平方根
x=math.log(n,a) #以a爲底n的對數x,a^x=n,默認底數爲e
math.log(32,2) #5.0
math.degrees(math.pi/4) #將Π/4轉爲角度
math.radians(45) #將45度轉爲弧度
math.cos(math.pi/4) #參數都爲弧度
一些封裝的模板
並查集
N,m=map(int,input().split())
fa=[int(i) for i in range(N+1)]
siz=[1]*(N+1)
def findfa(x):
if fa[x]!=x:
fa[x]=findfa(fa[x])
return fa[x]
def Merge(x,y):
xx,yy=findfa(x),findfa(y)
if xx == yy:
return False
if siz[xx] > siz[yy]: #按秩合併
fa[yy]=xx
siz[xx]+=siz[yy]
else:
fa[xx]=yy
siz[yy]+=siz[xx]
return True
for i in range(m):
z,x,y=map(int,input().split())
if z==1:
Merge(x,y)
else:
print('Y' if findfa(x)==findfa(y)else 'N')
線段樹區間加+區間和
class SegTreeNode(): #python3中所有類默認都是新式類
def __init__(self): #類似構造函數,類方法必須包含參數self
self.value=0
self.lazytag=0
Data=[0 for i in range(0,100010)]
class SegTree():
def __init__(self):
self.SegTree=[SegTreeNode() for i in range(0,400010)]
def Build_SegTree(self,Root,L,R):
if L==R:
self.SegTree[Root].value=Data[L]
return
mid=(L+R)>>1
self.Build_SegTree(Root<<1,L,mid)
self.Build_SegTree(Root<<1|1,mid+1,R)
self.SegTree[Root].value=self.SegTree[Root<<1].value+self.SegTree[Root<<1|1].value
return
def Push_Down(self,Root,L,R):
if self.SegTree[Root].lazytag==0:
return
Add=self.SegTree[Root].lazytag
self.SegTree[Root].lazytag=0
mid=(L+R)>>1
self.SegTree[Root<<1].value+=(mid-L+1)*Add
self.SegTree[Root<<1|1].value+=(R-mid)*Add
self.SegTree[Root<<1].lazytag+=Add
self.SegTree[Root<<1|1].lazytag+=Add
return
def Update(self,Root,L,R,QL,QR,Add):
if R<QL or QR<L:
return
if QL<=L and R<=QR:
self.SegTree[Root].value+=(R-L+1)*Add
self.SegTree[Root].lazytag+=Add
return
mid=(L+R)>>1
self.Push_Down(Root,L,R)
self.Update(Root<<1,L,mid,QL,QR,Add)
self.Update(Root<<1|1,mid+1,R,QL,QR,Add)
self.SegTree[Root].value=self.SegTree[Root<<1].value+self.SegTree[Root<<1|1].value
return
def Query(self,Root,L,R,QL,QR):
if R<QL or QR<L:
return 0
if QL<=L and R<=QR:
return self.SegTree[Root].value
mid=(L+R)>>1
self.Push_Down(Root,L,R)
return self.Query(Root<<1,L,mid,QL,QR)+self.Query(Root<<1|1,mid+1,R,QL,QR)
Tree=SegTree()
N,M=map(int,input().split())
a=input().split() #初始值
for i in range(1,N+1):
Data[i]=int(a[i-1])
Tree.Build_SegTree(1,1,N)
while M:
opt,L,R=map(int,input().split())
if opt==1:
Tree.Update(1,1,N,L,R,int(a[3]))
else:
print(str(Tree.Query(1,1,N,L,R)))
M-=1