学考复习……(?)
真的是复习吗
难道不是啥也没学过吗
不定期更新
感觉打一些比较熟的东西更容易上手一点
码的怎么现在学啥语言上来先学怎么封装
并查集
洛谷板子
n,m=map(int,input().split())
class dsu:
def __init__(self,n):
self.fa=[i for i in range(n+1)]
def find(self,id):
if self.fa[id]==id:
return id
else:
self.fa[id]=self.find(self.fa[id])
return self.fa[id]
def join(self,x,y):
fx=self.find(x)
fy=self.find(y)
if fx!=fy:
self.fa[fx]=fy
def samefa(self,x,y):
fx=self.find(x)
fy=self.find(y)
return fx==fy
d=dsu(n)
for i in range(m):
op,x,y=map(int,input().split())
if op==1:
d.join(x,y)
else:
print("Y") if d.samefa(x,y) else print("N")
线段树
洛谷板子
你说得对,但是 Python 线段树常数太大是怎么回事呢,Python 线段树常数太大,其实就是 Python 线段树过不了洛谷模板题,对此小编也感到非常奇怪,但是事实就是这样
n,m=map(int,input().split())
a=[int(i) for i in input().split()]
def midof(l,r):
return ((l)+(r))//2
class segtree:
def __init__(self,n):
self.sum=[0 for i in range(n+1)]
self.lazy=[0 for i in range(n+1)]
def build(self,id,l,r):
if l==r:
self.sum[id]=a[l-1]
return
mid=midof(l,r)
self.build(id*2,l,mid)
self.build(id*2+1,mid+1,r)
self.sum[id]=self.sum[id*2]+self.sum[id*2+1]
def pushdown(self,id,l,r):
if self.lazy[id]!=0:
mid=midof(l,r)
self.sum[id*2]+=(mid-l+1)*self.lazy[id]
self.lazy[id*2]+=self.lazy[id]
self.sum[id*2+1]+=(r-(mid+1)+1)*self.lazy[id]
self.lazy[id*2+1]+=self.lazy[id]
self.lazy[id]=0
def change(self,id,l,r,L,R,val):
if L<=l and r<=R:
self.sum[id]+=(r-l+1)*val
self.lazy[id]+=val
return
mid=midof(l,r)
self.pushdown(id,l,r)
if R<=mid:
self.change(id*2,l,mid,L,R,val)
elif L>=mid+1:
self.change(id*2+1,mid+1,r,L,R,val)
else:
self.change(id*2,l,mid,L,mid,val)
self.change(id*2+1,mid+1,r,mid+1,R,val)
self.sum[id]=self.sum[id*2]+self.sum[id*2+1]
def ask(self,id,l,r,L,R):
if L<=l and r<=R:
return self.sum[id]
self.pushdown(id,l,r)
mid=midof(l,r)
if R<=mid:
return self.ask(id*2,l,mid,L,R)
elif L>=mid+1:
return self.ask(id*2+1,mid+1,r,L,R)
return self.ask(id*2,l,mid,L,mid)+self.ask(id*2+1,mid+1,r,mid+1,R)
t=segtree(n*4)
t.build(1,1,n)
for i in range(m):
op=[int(i) for i in input().split()]
if op[0]==1:
t.change(1,1,n,op[1],op[2],op[3])
else:
print(t.ask(1,1,n,op[1],op[2]))
标签:lazy,复习,sum,mid,id,学考,self,def
From: https://www.cnblogs.com/HaneDaCafe/p/18528857