可以使用暴力法进行模拟但是不能全过,听讲解是要用线段树,唉不会的知识太多了,等学了回来补坑
n,m = map(int,input().split())
a = list(map(int,input().split()))
a.insert(0,0)
for i in range(m):
temp = list(map(int,input().split()))
if len(temp) == 3:
b,c,d = temp
print(sum(a[c:d+1]))
else:
b,c,d,e = temp
for j in range(c,d+1):
a[j] += e
## 超时,要用线段树
## 等学完线段树回来补坑
还是暴力,没学线段树的苦,等着,我会回来的
n,m,x = map(int,input().split())
a = list(map(int,input().split()))
for i in range(m):
b,c = map(int,input().split())
flag = 0
for j in range(b-1,c):
if flag == 1:
break
for k in range(b-1,c):
if j == k:
continue
if a[j]^a[k] == x:
print("yes")
flag = 1
break
if flag == 0:
print("no")
## 要用线段树
## 我会回来的
用两个点就可以表示一个矩阵,左上点为(a,b),右下的点为(c,d)。然后双层遍历,第一、二层遍历选左上的坐标,第二、三层遍历子矩阵的长宽。然后统计子矩阵的和直接交给函数去做
n,m,k = map(int,input().split())
t = [[0] for i in range(n+1)]
for i in range(1,n+1):
t[i].extend(list(map(int,input().split())))
def msum(a,b,c,d):
res = 0
for i in range(a,c+1):
res += sum(t[i][b:d+1])
return res
ans = 0
for i in range(1,n+1):
for j in range(1,m+1):
for length in range(0,m-j+1):
for width in range(0,n-i+1):
if msum(i,j,i+width,j+length) <= k:
ans += 1
print(ans)
这里原本的思路肯定是暴力,但是过不了太多,还好之前遇到过了七段码,学了并查集。直接秒杀,哇咔咔,哇咔咔。
详细说明见我之前的另一篇文章
n = int(input())
a = list(map(int,input().split()))
fa = [i for i in range(0,1000001)]
def find(x):
if fa[x] == x:
return x
else:
fa[x] = find(fa[x])
return fa[x]
def union(a,b):
a_fa = find(a)
b_fa = find(b)
fa[a_fa] = b_fa
for i in range(n):
if find(a[i]) != a[i]:
a[i] = find(a[i])
union(a[i],a[i]+1)
for i in range(n):
print(a[i],end = " ")
目前只会暴力法按照公式用数组模拟即可,题目也能过,但是总觉得有点怪怪的。可能测试案例太仁慈了。
n,m,k = map(int,input().split())
a = []
b = []
##c = [[0]*k for _ in range(n)]
for i in range(n):
a.append(list(map(int,input().split())))
for i in range(m):
b.append(list(map(int,input().split())))
for i in range(n):
for j in range(k):
## c[i][j] = sum([a[i][t]*b[t][j] for t in range(m)])
print(int(sum([a[i][t]*b[t][j] for t in range(m)])),end = " ")
print("")
小记: