hdu 4578 Transformation 线段树

没什么说的裸线段树,注意细节就好了!!!

代码如下:

 

  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<algorithm>
  4 #include<iomanip>
  5 #include<cmath>
  6 #include<cstring>
  7 #include<vector>
  8 #define ll __int64
  9 #define pi acos(-1.0)
 10 #define MAX 100003
 11 #define M 10007
 12 #define lson i<<1
 13 #define rson i<<1|1
 14 using namespace std;
 15 struct tree
 16 {
 17     int l,r,sum[3],add,mul;
 18     void init(int _l,int _r){
 19         l=_l;r=_r;mul=1;add=0;
 20     }
 21     void Add(int a){
 22         sum[2]=(sum[2]+a*a%M*a%M*len()%M+3*a*sum[1]%M+3*a*a%M*sum[0]%M)%M;
 23         sum[1]=(sum[1]+a*a%M*len()%M+2*a*sum[0]%M)%M;
 24         sum[0]=(sum[0]+a*len()%M)%M;
 25         add=(add+a)%M;
 26     }
 27     void Mul(int a){
 28         sum[2]=sum[2]*a%M*a%M*a%M;
 29         sum[1]=sum[1]*a%M*a%M;
 30         sum[0]=sum[0]*a%M;
 31         add=add*a%M;
 32         mul=mul*a%M;
 33     }
 34     int len(){
 35         return r-l+1;
 36     }
 37     int mid(){
 38         return (l+r)>>1;
 39     }
 40 }T[MAX*4];
 41 void down(int i)
 42 {
 43     T[lson].Mul(T[i].mul);
 44     T[rson].Mul(T[i].mul);
 45     T[lson].Add(T[i].add);
 46     T[rson].Add(T[i].add);
 47     T[i].add=0;
 48     T[i].mul=1;
 49 }
 50 void updateson(int i)
 51 {
 52     for(int j=0;j<3;j++)
 53         T[i].sum[j]=(T[lson].sum[j]+T[rson].sum[j])%M;
 54 }
 55 void built(int i,int l,int r)
 56 {
 57     T[i].init(l,r);
 58     memset(T[i].sum,0,sizeof(T[i].sum));
 59     if(l==r) return ;
 60     int m=T[i].mid();
 61     built(lson,l,m);
 62     built(rson,m+1,r);
 63 }
 64 void update(int i,int l,int r,int mul,int add)
 65 {
 66     if(T[i].l==l&&T[i].r==r){
 67         T[i].Mul(mul);
 68         T[i].Add(add);
 69         return ;
 70     }
 71     down(i);
 72     int m=T[i].mid();
 73     if(r<=m) update(lson,l,r,mul,add);
 74     else if(l>m) update(rson,l,r,mul,add);
 75     else{
 76         update(lson,l,m,mul,add);
 77         update(rson,m+1,r,mul,add);
 78     }
 79     updateson(i);
 80 }
 81 int query(int i,int l,int r,int p)
 82 {
 83     if(T[i].l==l&&T[i].r==r) return T[i].sum[p-1];
 84     down(i);
 85     int m=T[i].mid();
 86     if(r<=m) return query(lson,l,r,p);
 87     else if(l>m) return query(rson,l,r,p);
 88     else return (query(lson,l,m,p)+query(rson,m+1,r,p))%M;
 89 }
 90 int main(){
 91     int m,n,op,x,y,p;
 92     while(scanf("%d%d",&n,&m)&&(m+n)){
 93         built(1,1,n);
 94         while(m--){
 95             scanf("%d%d%d%d",&op,&x,&y,&p);
 96             if(op==1) update(1,x,y,1,p);
 97             else if(op==2) update(1,x,y,p,0);
 98             else if(op==3) update(1,x,y,0,p);
 99             else printf("%d\n",query(1,x,y,p)%M);
100         }
101     }
102     return 0;
103 }
View Code

 

 

 

posted @ 2013-08-23 22:08  _随心所欲_  阅读(184)  评论(0编辑  收藏  举报