标签:har show down typedef upd += bsp uil using
1 #include<cstdio> 2 const int maxn=100000+10; 3 typedef long long ll; 4 ll a[maxn],tree[4*maxn],lazy[4*maxn]; 5 void Build(int rt,int l,int r){ 6 if(l==r) { 7 tree[rt]=a[l]; 8 return; 9 } 10 int mid=(l+r)>>1; 11 Build(rt<<1,l,mid); 12 Build(rt<<1|1,mid+1,r); 13 tree[rt]=tree[rt<<1]+tree[rt<<1|1]; 14 } 15 void update(int rt,int l,int r,ll w){ 16 tree[rt]+=(r-l+1)*w; 17 lazy[rt]+=w; 18 } 19 void pushdown(int rt,int l,int r){ 20 int mid=(l+r)>>1; 21 update(rt<<1,l,mid,lazy[rt]); 22 update(rt<<1|1,mid+1,r,lazy[rt]); 23 lazy[rt]=0; 24 } 25 ll query(int rt,int l,int r,int x){ 26 if(l==r) return tree[rt]; 27 int mid=(l+r)>>1; 28 pushdown(rt,l,r); 29 if(x<=mid) return query(rt<<1,l,mid,x); 30 else return query(rt<<1|1,mid+1,r,x); 31 } 32 void modify(int rt,int l,int r,int s,int t,ll w){ 33 if(s<=l&&t>=r){ 34 update(rt,l,r,w); 35 return ; 36 } 37 pushdown(rt,l,r); 38 int mid=(l+r)>>1; 39 if(s<=mid) modify(rt<<1,l,mid,s,t,w); 40 if(t>mid) modify(rt<<1|1,mid+1,r,s,t,w); 41 tree[rt]=tree[rt<<1]+tree[rt<<1|1]; 42 } 43 int main(){ 44 int n; 45 scanf("%d",&n); 46 for(int i=1;i<=n;i++) scanf("%lld",&a[i]); 47 Build(1,1,n); 48 int m; 49 scanf("%d",&m); 50 for(int i=1;i<=m;i++){ 51 char s[8]; 52 scanf("%s",s); 53 if(s[0]==‘Q‘){ 54 int x; 55 scanf("%d",&x); 56 ll a=query(1,1,n,x); 57 printf("%lld\n",a); 58 } 59 else { 60 int l,r; 61 ll w; 62 scanf("%d%d%lld",&l,&r,&w); 63 modify(1,1,n,l,r,w); 64 } 65 } 66 return 0; 67 }
1 #include<cstdio> 2 #define lson rt<<1 3 #define rson rt<<1|1 4 const int maxn=1e6+10; 5 int a[maxn]={},tree[4*maxn]={}; 6 void Build(int rt,int l,int r){ 7 if(l==r){ 8 tree[rt]=a[l]; 9 return ; 10 } 11 int mid=(l+r)>>1; 12 Build(lson,l,mid); 13 Build(rson,mid+1,r); 14 tree[rt]=tree[lson]+tree[rson]; 15 } 16 int query(int rt,int l,int r,int s,int t){ 17 if(s<=l&&t>=r) return tree[rt]; 18 int mid=(l+r)>>1; 19 if(t<=mid) return query(lson,l,mid,s,t); 20 else if(s>mid) return query(rson,mid+1,r,s,t); 21 else return query(lson,l,mid,s,t)+query(rson,mid+1,r,s,t); 22 } 23 void modify(int rt,int l,int r,int x,int y){ 24 if(l==r){ 25 tree[rt]+=y; 26 return; 27 } 28 int mid=(l+r)>>1; 29 if(x<=mid) modify(lson,l,mid,x,y); 30 else modify(rson,mid+1,r,x,y); 31 tree[rt]=tree[lson]+tree[rson]; 32 } 33 int main(){ 34 int n,m; 35 scanf("%d",&n); 36 if(n!=0){ 37 for(int i=1;i<=n;i++){ 38 scanf("%d",&a[i]); 39 } 40 Build(1,1,n); 41 scanf("%d",&m); 42 char s[5]; 43 int k,d,tot=0; 44 for(int i=1;i<=m;i++){ 45 scanf("%s%d%d",s,&k,&d); 46 if(s[0]==‘S‘){ 47 printf("%d\n",query(1,1,n,k,d)); 48 } 49 else modify(1,1,n,k,d); 50 } 51 } 52 else { 53 scanf("%d",&m); 54 char s[5]; 55 int a,b; 56 for(int i=1;i<=m;i++){ 57 scanf("%s%d%d",s,&a,&b); 58 if(s[0]==‘S‘) printf("0\n"); 59 } 60 } 61 return 0; 62 }
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 const int maxn=1e6+10; 6 int a[maxn],tree[maxn*4]; 7 void Build(int rt,int l,int r){ 8 if(l==r){ 9 tree[rt]=a[l]; 10 return; 11 } 12 int mid=(l+r)>>1; 13 Build(rt<<1,l,mid); 14 Build(rt<<1|1,mid+1,r); 15 tree[rt]=max(tree[rt<<1],tree[rt<<1|1]); 16 } 17 int query(int rt,int l,int r,int k,int d){ 18 if(k<=l&&d>=r) return tree[rt]; 19 int mid=(l+r)>>1; 20 if(d<=mid) return query(rt<<1,l,mid,k,d); 21 else if(k>mid) return query(rt<<1|1,mid+1,r,k,d); 22 else return max(query(rt<<1,l,mid,k,d),query(rt<<1|1,mid+1,r,k,d)); 23 } 24 int main(){ 25 int n; 26 scanf("%d",&n); 27 for(int i=0;i<=n;i++) scanf("%d",&a[i]); 28 Build(1,1,n); 29 int q; 30 scanf("%d",&q); 31 int a,b; 32 for(int i=1;i<=q;i++){ 33 scanf("%d%d",&a,&b); 34 int j=query(1,1,n,a,b); 35 printf("%d\n",j); 36 } 37 return 0;
标签:har show down typedef upd += bsp uil using
原文地址:https://www.cnblogs.com/HZOIDJ123/p/13220301.html