1 solutions

  • 2
    @ 2024-12-6 20:56:21

    题解

    肥肠煎蛋,直接使用区改区查,主墓碑单独计算。

    呆码

    #include<bits/stdc++.h>
    #define int long long
    
    using namespace std;
    int n,a[1000001],b[1000001],trb[1000001],big;
    int trib[1000001];
    int lowbit(int x){return x&-x;}
    int getArea(int l,int r){
    	l--;
    	int temp;
    	int sum1;
    	temp=l,sum1=0;
    	while(temp){
    		sum1+=trb[temp]*(l+1);
    		sum1-=trib[temp];
    		temp-=lowbit(temp);
    	}
    	int sum2;
    	temp=r,sum2=0;
    	while(temp){
    		sum2+=trb[temp]*(r+1);
    		sum2-=trib[temp];
    		temp-=lowbit(temp);
    	}
    	return sum2-sum1;
    }
    void change(int p,int k){
    	int temp=p;
    	while(temp<=n){
    		trb[temp]+=k;
    		trib[temp]+=k*p;
    		temp+=lowbit(temp); 
    	}
    }
    void changeArea(int l,int r,int k){
    	r++;
    	int temp=l;
    	while(temp<=n){
    		trb[temp]+=k;
    		trib[temp]+=k*l;
    		temp+=lowbit(temp);
    	}
    	temp=r;
    	k=-k;
    	while(temp<=n){
    		trb[temp]+=k;
    		trib[temp]+=k*r;
    		temp+=lowbit(temp);
    	}
    }
    signed main(){
    	cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
    	cin>>n;
    	int x;
    	cin>>x;
    	for(int i=1;i<=n;i++){
    		cin>>a[i];
    		change(i,a[i]-a[i-1]);
    	}
    	while(x--){
    		int ctrl=0;
    		int l,r,k;
    		cin>>ctrl;
    		switch(ctrl){
    		case 1:
    			cin>>l>>r>>k;
    			changeArea(l,r,k);
    			break;
    		case 2:
    			cin>>k;
    			big+=k;
    			//主墓碑单独计算
    			break;
    		case 3:
    			cin>>k;
    			big-=k;
    			break;
    		case 4:
    			cin>>l>>r;
    			if(l==1){
    				cout<<getArea(l,r)+big<<endl;
    				//若含有主墓碑,加上主墓碑
    			}
    			else cout<<getArea(l,r)<<endl;
    			break;
    		case 5:
    			cout<<big+getArea(1,1)<<endl;
    			//记得算上更改的值
    			break;
    		}
    	}
    }
    
    • 1

    Information

    ID
    282
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    9
    Tags
    # Submissions
    28
    Accepted
    4
    Uploaded By