Wow! Such Sequence! HDU多校聯合賽第三場1007

來源:互聯網
上載者:User

標籤:des   style   blog   java   color   os   strong   io   

Wow! Such Sequence!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger from St. Beeze College. No, not cactuses. It‘s a mysterious blackbox.

After some research, Doge found that the box is maintaining a sequence an of n numbers internally, initially all numbers are zero, and there are THREE "operations":

1.Add d to the k-th number of the sequence.
2.Query the sum of ai where l ≤ i ≤ r.
3.Change ai to the nearest Fibonacci number, where l ≤ i ≤ r.
4.Play sound "Chee-rio!", a bit useless.

Let F0 = 1,F1 = 1,Fibonacci number Fn is defined as Fn = Fn - 1 + Fn - 2 for n ≥ 2.

Nearest Fibonacci number of number x means the smallest Fn where |Fn - x| is also smallest.

Doge doesn‘t believe the machine could respond each request in less than 10ms. Help Doge figure out the reason. 

 

Input Input contains several test cases, please process till EOF.
For each test case, there will be one line containing two integers n, m.
Next m lines, each line indicates a query:

1 k d - "add"
2 l r - "query sum"
3 l r - "change to nearest Fibonacci"

1 ≤ n ≤ 100000, 1 ≤ m ≤ 100000, |d| < 231, all queries will be valid. 

 

Output For each Type 2 ("query sum") operation, output one line containing an integer represent the answer of this query. 

 

Sample Input1 1 2 1 1 5 4 1 1 7 1 3 17 3 2 4 2 1 5 

 

Sample Output022 線段樹裸題,維護是否改變為fib這個標記Codes:
  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 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.