hdu 4893 线段树lazy wa代码 先放这里 我还会回来的!!

 

 

  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<queue>
  6 #include<math.h>
  7 #include<algorithm>
  8 #include<vector>
  9 #define maxx 100010
 10 using namespace std;
 11 #define ss __int64
 12 
 13 struct node {int l,r;ss sum; bool flag;};
 14 node tree[maxx*4];
 15 ss f[100];
 16 
 17 void getfib()
 18 {
 19     f[0]=f[1]=1;
 20     for(int i=2;i<=90;i++)
 21         {
 22             f[i]=f[i-1]+f[i-2];
 23             //printf("%I64d\n",f[i]);
 24         }
 25 }
 26 
 27 int solve(ss x)  //jiang  x  bian wei  zui jin d fib
 28 {
 29     int l=0,r=90,best=0;
 30     while(l<r)
 31     {
 32         int mid=(l+r)/2;
 33         if(f[mid]<=x)
 34         {
 35             l=mid+1;
 36             best=max(best,mid);
 37         }
 38         else
 39             r=mid-1;
 40     }
 41     return best;
 42 }
 43 
 44 void build(int left,int right,int k)
 45 {
 46     int mid;
 47     tree[k].l=left;
 48     tree[k].r=right;
 49     tree[k].sum=0;
 50     tree[k].flag=false;
 51     if(left==right)
 52         return ;
 53     mid=(left+right)/2;
 54     build(left,mid,k*2);
 55     build(mid+1,right,k*2+1);
 56 }
 57 
 58 void down(int k)
 59 {
 60     if(tree[k].flag==false)
 61        return ;
 62     tree[k].flag=false;
 63     if(tree[k].l==tree[k].r)
 64     {
 65 
 66         int temp,a,b;
 67         temp=solve(tree[k].sum);
 68         printf("1----%d\n",tree[k].sum);
 69         a=abs(tree[k].sum-f[temp]);
 70         b=abs(tree[k].sum-f[temp+1]);
 71         if(a<b)
 72         {
 73             tree[k].sum=f[temp];
 74         }
 75         else if(a==b)
 76         {
 77             tree[k].sum=min(f[temp],f[temp+1]);
 78         }
 79         else
 80             tree[k].sum=f[temp+1];
 81         printf("2------%d\n",tree[k].sum);
 82         return ;
 83 
 84     }
 85     down(k*2);
 86     down(k*2+1);
 87     tree[k].sum=tree[k*2].sum+tree[k*2+1].sum;
 88 }
 89 
 90 int sum(int left,int right,int k)
 91 {
 92     if(tree[k].l==left&&tree[k].r==right)
 93     {
 94         if(tree[k].flag)
 95             down(k);
 96         return tree[k].sum;
 97     }
 98     int mid;
 99     mid=(tree[k].l+tree[k].r)/2;
100     if(right<=mid)
101         return sum(left,right,k*2);
102     else if(left>=mid+1)
103         return sum(left,right,k*2+1);
104     else
105         return sum(left,mid,k*2)+sum(mid+1,right,k*2+1);
106 }
107 
108 void add(int x,int zhi,int k)
109 {
110     if(tree[k].l==x&&x==tree[k].r)
111     {
112         if(tree[k].flag)
113             down(k);
114         tree[k].sum+=zhi;
115         return ;
116     }
117     int mid;
118     mid=(tree[k].l+tree[k].r)/2;
119     if(x<=mid)
120         add(x,zhi,k*2);
121     else
122         add(x,zhi,k*2+1);
123     tree[k].sum=tree[k*2].sum+tree[k*2+1].sum;
124 }
125 
126 void biaoji(int left,int right,int k)
127 {
128     if(tree[k].l==left&&tree[k].r==right)
129     {
130         printf("biao  %d %d\n",tree[k].l,tree[k].r);
131         tree[k].flag=true;
132         return ;
133     }
134     int mid;
135     mid=(tree[k].l+tree[k].r)/2;
136     if(right<=mid)
137         biaoji(left,right,k*2);
138     else if(left>=mid+1)
139         biaoji(left,right,k*2+1);
140     else
141     {
142         biaoji(left,mid,k*2);
143         biaoji(mid+1,right,k*2+1);
144     }
145 }
146 
147 int main()
148 {
149     int i,j,n,m,a,b,c;
150     getfib();
151     while(~scanf("%d%d",&n,&m))
152     {
153         build(1,n,1);
154         while(m--)
155         {
156             scanf("%d%d%d",&a,&b,&c);
157             if(a==1)
158                 add(b,c,1);
159             if(a==2)
160                 printf("%d\n",sum(b,c,1));
161             if(a==3)
162                 biaoji(b,c,1);
163         }
164     }
165     return 0;
166 }

 

posted @ 2014-07-30 16:19  galaxy77  阅读(201)  评论(0编辑  收藏  举报