关于python的常数问题
查看原帖
关于python的常数问题
215142
UchItachi楼主2021/12/6 16:15

萌新刚学python,敲了道树状数组练练手。然后发现莫名地T了,是python自带的常数大吗?
python版

def lowbit (x):
    return (int)( x&(-1*x) )
tr=[0]*500005
tp=[0]*500005
n=0;
q=0;
def add (x,k):
    while int(x)<=int(n):
        tr[x]=int(tr[x])+int(k)
        x=int(x)+lowbit(x)

def query (x):
    ret=0;
    while int(x)>0:
        ret=int(ret)+int(tr[x])
        x=int(x)-lowbit(x)
    return int(ret)
n,q=input().split()
n=int(n)
q=int(q)
tp[1:n+1]=input().split(" ")
for i in range(1,n+1):
    add(i,int(tp[i]))
for i in range(1,q+1):
    opt,x,y=input().split(" ")
    x=int(x)
    opt=int(opt)
    y=int(y)
    if opt==1:
        add(x,y)
    if opt==2:
        print(int(query(y))-int(query(x-1)))
        

c++版:

#include<bits/stdc++.h>
#define MAX 500005
#define ll long long
using namespace std;
int lowbit(int x){
	return x&-x;
}
ll tr[MAX],n,q;
void add(int x,ll c){
	for(;x<=n;x+=lowbit(x)) tr[x]+=c;
}
ll query(int x){
	ll ret=0;
	for(;x;x-=lowbit(x)) ret+=tr[x];
	return ret;
}
int main(){
	scanf("%lld%lld",&n,&q);
	for(int i=1;i<=n;i++){
		ll x;
		scanf("%lld",&x);
		add(i,x); 
	}
	while(q--){
		ll opt,x,y;
		scanf("%lld%lld%lld",&opt,&x,&y);
		if(opt==1) add(x,y);
		else printf("%lld\n",query(y)-query(x-1));
	}
}
2021/12/6 16:15
加载中...