C++-蓝桥杯-数组操作[暴力+线段树(区间修改+区间查询)=40分算法]
不知道区间更改怎么搞...
1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 typedef long long ll; 5 const int MAXN=1e5+10; 6 struct node{ll sum,add;}t[MAXN<<2]; 7 int L,R,L1,R1,L2,R2;ll d; 8 9 #define ls rt<<1 10 #define rs rt<<1|1 11 12 void push_up(int rt){t[rt].sum=t[ls].sum+t[rs].sum;} 13 void push_down(int rt,int l,int r,int mid){ 14 if(t[rt].add){ 15 t[ls].add+=t[rt].add; 16 t[rs].add+=t[rt].add; 17 t[ls].sum+=(mid-l+1)*t[rt].add; 18 t[rs].sum+=(r-mid)*t[rt].add; 19 t[rt].add=0; 20 } 21 } 22 23 void build(int rt,int l,int r){ 24 if(l==r){scanf("%lld",&t[rt].sum);return;} 25 int mid=(l+r)>>1; 26 build(ls,l, mid); 27 build(rs,mid+1,r); 28 push_up(rt); 29 } 30 31 void update(int rt,int l,int r){ 32 if(L<=l&&r<=R){t[rt].sum+=(r-l+1)*d,t[rt].add+=d;return;} 33 int mid=(l+r)>>1; 34 push_down(rt,l,r,mid); 35 if(L<=mid)update(ls,l,mid); 36 if(R>mid)update(rs,mid+1,r); 37 push_up(rt); 38 } 39 40 ll query(int rt, int l, int r){ 41 if(L<=l&&r<=R)return t[rt].sum; 42 int mid=(l+r)>>1; 43 push_down(rt,l,r,mid); 44 ll ans=0; 45 if(L<=mid)ans+=query(ls,l,mid); 46 if(R>mid)ans+=query(rs,mid+1,r); 47 return ans; 48 } 49 50 ll A[MAXN],B[MAXN]; 51 52 int main(){ 53 int k,n,m; 54 scanf("%d%d%d",&k,&n,&m); 55 if(k<=2){ 56 for(int i=1;i<=n;i++)scanf("%lld",&A[i]); 57 for(int q;m--;){ 58 scanf("%d",&q); 59 if(q==1){ 60 scanf("%d%d%lld",&L,&R,&d); 61 for(int i=L;i<=R;i++)A[i]+=d; 62 } 63 else if(q==2){ 64 scanf("%d%d%d%d",&L1,&R1,&L2,&R2); 65 for(int i=0;i<=R2-L2;i++)B[i]=A[L2+i]; 66 for(int i=0;i<=R1-L1;i++)A[L1+i]=B[i]; 67 } 68 else{ 69 ll sum=0; 70 scanf("%d%d",&L,&R); 71 for(int i=L;i<=R;i++)sum+=A[i]; 72 printf("%lld\n",sum); 73 } 74 } 75 } 76 else{ 77 build(1,1,n); 78 for(char s[3];m--;){ 79 scanf("%s",s); 80 if(s[0]=='1')scanf("%d%d%d",&L,&R,&d),update(1,1,n); 81 else scanf("%d%d",&L,&R),printf("%lld\n",query(1,1,n)); 82 } 83 } 84 return 0; 85 }
~~Jason_liu O(∩_∩)O