标签:des style blog java color os strong io
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
1 #include<set> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<iostream> 6 #include<algorithm> 7 using namespace std; 8 const int N = 100100; 9 #define Ch1 (i<<1) 10 #define Ch2 (Ch1|1) 11 #define For(i,n) for(int i=1;i<=n;i++) 12 #define Rep(i,l,r) for(int i=l;i<=r;i++) 13 14 struct tnode{ 15 int l,r,mid; 16 long long mark,sum,sumf; 17 }T[N<<2]; 18 long long F[110]; 19 int n,m,op; 20 long long l,r; 21 22 long long ABS(long long x){ 23 if(x<0) return -x; 24 return x; 25 } 26 27 long long MAX(long long A,long long B){ 28 if(A>B) return A; 29 else return B; 30 } 31 32 long long find(long long x){ 33 long long Min = 0 , Max = 0; 34 Rep(i,0,103) 35 if(F[i]<=x) Min = MAX(F[i],Min); 36 else if(F[i]>x){ 37 Max = F[i]; 38 break; 39 } 40 if(ABS(x-Min)<=ABS(x-Max)) return Min; 41 else return Max; 42 } 43 44 void Pushdown(int i){ 45 if(T[i].mark){ 46 T[Ch1].sum = T[Ch1].sumf;T[Ch1].mark = 1; 47 T[Ch2].sum = T[Ch2].sumf;T[Ch2].mark = 1; 48 T[i].mark = 0; 49 } 50 } 51 52 void Build(int l,int r,int i){ 53 T[i].l = l; T[i].r = r; T[i].mid = (l+r)>>1; 54 T[i].mark = 0; T[i].sum = T[i].sumf = 0; 55 if(l==r){ 56 T[i].sumf = 1; 57 return; 58 } 59 Build(l,T[i].mid,Ch1);Build(T[i].mid+1,r,Ch2); 60 T[i].sumf = T[Ch1].sumf + T[Ch2].sumf; 61 } 62 63 void Modify(int i,int x,long long delta){ 64 if(T[i].l==T[i].r){ 65 T[i].sum+=delta; 66 T[i].sumf = find(T[i].sum); 67 return; 68 } 69 Pushdown(i); 70 if(x<=T[i].mid) Modify(Ch1,x,delta); 71 else Modify(Ch2,x,delta); 72 T[i].sum = T[Ch1].sum + T[Ch2].sum; 73 T[i].sumf = T[Ch1].sumf + T[Ch2].sumf; 74 } 75 76 void Modifyf(int i,int l,int r){ 77 if(l<=T[i].l&&T[i].r<=r){ 78 T[i].mark = 1; 79 T[i].sum = T[i].sumf; 80 return; 81 } 82 Pushdown(i); 83 if(r<=T[i].mid) Modifyf(Ch1,l,r);else 84 if(l>T[i].mid) Modifyf(Ch2,l,r);else 85 Modifyf(Ch1,l,T[i].mid) , Modifyf(Ch2,T[i].mid+1,r); 86 T[i].sum = T[Ch1].sum + T[Ch2].sum; 87 T[i].sumf = T[Ch1].sumf + T[Ch2].sumf; 88 } 89 90 long long query(int l,int r,int i){ 91 if(l<=T[i].l&&T[i].r<=r) return T[i].sum; 92 Pushdown(i); 93 if(r<=T[i].mid) return query(l,r,Ch1);else 94 if(l>T[i].mid) return query(l,r,Ch2);else 95 return query(l,T[i].mid,Ch1) + query(T[i].mid+1,r,Ch2); 96 97 } 98 99 void init(){ 100 while(scanf("%d%d",&n,&m)!=EOF){ 101 Build(1,n,1); 102 For(i,m){ 103 scanf("%d%I64d%I64d",&op,&l,&r); 104 if(op==1) Modify(1,l,r); 105 if(op==2) printf("%I64d\n",query(l,r,1)); 106 if(op==3) Modifyf(1,l,r); 107 } 108 } 109 } 110 111 int main(){ 112 F[0] = 1; F[1] = 1; 113 Rep(i,2,103) F[i] = F[i-1] + F[i-2]; 114 init(); 115 return 0; 116 }
Wow! Such Sequence! HDU多校联合赛第三场1007,布布扣,bubuko.com
Wow! Such Sequence! HDU多校联合赛第三场1007
标签:des style blog java color os strong io
原文地址:http://www.cnblogs.com/kjerome/p/3875889.html