数据结构模板

线段树

函数名要写上。。。
LL 在运算的时候要防溢出。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN=100005;
long long  sum[MAXN<<2];
long long col[MAXN<<2];
long long read(){
    long long rv=0,fh=1;
    char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-') fh=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        rv=(rv<<1)+(rv<<3)+c-'0';
        c=getchar();
    }
    return rv*fh;
}
void PushUP(int rt){
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(int rt,int m){
    if(col[rt]){
        col[rt<<1]+=col[rt];
        col[rt<<1|1]+=col[rt];
        sum[rt<<1]+=col[rt]*(m-(m>>1));   //
        sum[rt<<1|1]+=col[rt]*(m>>1);
        col[rt]=0;
    }
}
void build(int l,int r,int rt){
    col[rt]=0;
    if(l==r) {sum[rt]=read();/*scanf("%lld",&sum[rt]);*/return ;}
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    PushUP(rt);
}
/*void update(int p,int add,int l,int r,int rt){
    if(l==r){
        sum[rt]+=add;
        return ;
    }
    int m=(l+r)>>1;
    if(p<=m) update(lson);
    else update(rson);
    PushUP(rt);
}
int query(int L,int R,int l,int r,int rt){
    if(L<=l&&r<=R) {
        return sum[rt];
    }
    int m=(l+r)>>1;
    int ret=0;
    if(L<=m) ret+=(L,R,lson);
    if(R>m) ret+=(L,R,rson);
    return ret;
}*/
void update(int L,int R,int add,int l,int r,int rt){
    if(L<=l&&r<=R){
        col[rt]+=add;
        sum[rt]+=(long long)add*(r-l+1);
        return ;
    }
    PushDown(rt,r-l+1);
    int m=(l+r)>>1;
    if(L<=m) update(L,R,add,lson);
    if(m<R) update(L,R,add,rson);
    PushUP(rt);
}
long long query(int L,int R,int l,int r,int rt){
    if(L<=l&&r<=R){
        return sum[rt];
    }
    PushDown(rt , r - l + 1);  
  int m = (l + r) >> 1;  
    long long ret = 0;  
  if(L<=m) ret += query(L , R , lson); 
    if(m<R) ret+=query(L,R,rson);
    return ret;
}
int n,m;
int main(){
    freopen("in.txt","r",stdin);
    n=read();m=read();
    //scanf("%d%d",&n,&m);
    build(1,n,1);
    for(int i=1;i<=m;i++){
        int t;
        scanf("%d",&t);
        int l,r,add;
        if(t==1){
            l=read();r=read();add=read();
            //scanf("%d%d%d",&l,&r,&add);
            update(l,r,add,1,n,1);
        }else {
            l=read();r=read();
            //scanf("%d%d",&l,&r);
            printf("%lld\n",query(l,r,1,n,1));
        }
    }   
    fclose(stdin);
    return 0;
}

区间加,区间乘,取模

ATTENTION:凡是能取模的地方都要取模,防溢出。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
#define ll long long 
using namespace std;
const int MAXN=100005;
ll init() {
	ll rv = 0, fh = 1;
	char c =getchar();
	while(c < '0' || c > '9'){
		if(c == '-') fh = -1;
		c = getchar();
	}
	while(c >= '0' && c <= '9'){
		rv = (rv<<1) + (rv<<3) + c - '0';
		c = getchar();
	}
	return fh * rv;
}
ll n,m,MOD;
struct SGT{
	ll val[MAXN<<2], add[MAXN<<2], mul[MAXN<<2];
	void PushUp(int rt){
		(val[rt] = val[rt<<1] + val[rt<<1|1]) %= MOD;
	}
	void build(int l,int r,int rt){
		mul[rt]=1;
		if(l == r){
			val[rt] = init();
			return ;
		}
		int mid = (l+r) >> 1;
		build(lson);
		build(rson);
		PushUp(rt);
	}
	void PushDown(int rt, int m){
		(val[rt<<1] = val[rt<<1] * mul[rt] + add[rt] * (m - (m>>1))) %= MOD;
		(val[rt<<1|1] = val[rt<<1|1] * mul[rt] + add[rt] * (m>>1)) %= MOD;
		(add[rt<<1] = add[rt<<1] * mul[rt] + add[rt]) %= MOD;
		(add[rt<<1|1] = add[rt<<1|1] * mul[rt] +add[rt]) %= MOD;
		(mul[rt<<1] = mul[rt<<1] * mul[rt]) %= MOD;
		(mul[rt<<1|1] = mul[rt<<1|1] * mul[rt]) %= MOD;
		add[rt]=0;mul[rt]=1;
	}
	void UpdateAdd(int L, int R, int _add, int l, int r, int rt){
		if(L <= l && r <= R){
			(val[rt] += (ll)_add * (r-l+1)) %= MOD;
			(add[rt] += _add) %= MOD;
			return;
		}
		PushDown(rt,r-l+1);
		int mid = (l+r)>>1;
		if(L <= mid) UpdateAdd(L, R, _add, lson);
		if(mid < R) UpdateAdd(L, R, _add, rson);
		PushUp(rt);
	}
	void UpdateMul(int L, int R, int _mul, int l, int r, int rt){
		if(L <=l && r<=R ){
			(val[rt] *= _mul) %= MOD;
			(mul[rt] *= _mul) %= MOD;
			(add[rt] *= _mul) %= MOD;
			return;
		}
		PushDown(rt,r-l+1);
		int mid = (l+r)>>1;
		if(L <= mid) UpdateMul(L, R, _mul, lson);
		if(mid < R) UpdateMul(L, R, _mul, rson);
		PushUp(rt);
	}
	ll Query(int L, int R, int l, int r,int rt){
		if(L <= l && r<=R ){
			return val[rt];
		}
		PushDown(rt, r-l+1);
		int mid = (l+r)>>1;
		ll ans=0;
		if(L<=mid) ans+= Query(L, R, lson);
		if(mid < R) ans+= Query(L, R, rson);
		return ans % MOD; 
		//PushUp(rt);
	}
}sgt;
int main(){
	freopen("in.txt", "r", stdin);
	n = init(); m = init(); MOD = init();
	sgt.build(1, n, 1);
	for(int i = 1 ; i <= m ; i++){
		int t = init();
		if(t == 1){
			int l = init(), r = init(), k = init();
			sgt.UpdateMul(l, r, k, 1, n, 1);
		}else if(t == 2) {
			int l = init(), r =init(), k=init();
			sgt.UpdateAdd(l, r, k, 1, n, 1);
		}else {
			int l = init(), r = init();
			printf("%lld\n",sgt.Query(l, r, 1, n, 1));
		}
	//	cout<<sgt.val[1]<<endl;
	} 
	fclose(stdin);
	return 0;
}
posted @ 2017-11-20 21:19  Mr_Wolfram  阅读(338)  评论(0编辑  收藏  举报