HDU 4893 線段樹裸題

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   java   os   strong   

Wow! Such Sequence!Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2512    Accepted Submission(s): 751


Problem DescriptionRecently, 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. 
InputInput 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. 
OutputFor each Type 2 ("query sum") operation, output one line containing an integer represent the answer of this query. 
Sample Input
1 12 1 15 41 1 71 3 173 2 42 1 5
 
Sample Output
022
 
AuthorFudan University 
Source2014 Multi-University Training Contest 3


題意不說了,很簡單的線段樹題目了,就是打標記改點求段,修改段時由於極限次數不多,直接暴力更新到點,

好久沒寫線段樹了,錯了好多次,寫的好傻逼。

代碼:

/* ***********************************************Author :rabbitCreated Time :2014/8/4 14:58:15File Name :11.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8#define pi acos(-1.0)typedef long long ll;ll fib[100];struct node{    ll l,r;    ll sum,flag;}a[800300];ll m,n;void pushup(ll t){    if(a[t].l==a[t].r)return;    a[t].sum=a[2*t].sum+a[2*t+1].sum;    a[t].flag=a[2*t].flag&a[2*t+1].flag;}void build(ll t,ll l,ll r){//    cout<<"hhh "<<l<<" "<<r<<endl;    a[t].l=l;    a[t].r=r;    a[t].sum=a[t].flag=0;    if(l==r)return;    ll mid=(l+r)/2;    build(2*t,l,mid);    build(2*t+1,mid+1,r);    pushup(t);}void update1(ll t,ll p,ll val){    if(a[t].l==a[t].r){        a[t].sum+=val;        a[t].flag=0;        return;    }    ll mid=(a[t].l+a[t].r)/2;    if(p<=mid)update1(2*t,p,val);    else update1(2*t+1,p,val);    pushup(t);}ll Find(ll x){    if(x <= 1)return 1;    int l = 1, r = 80, id = 80;    while(l <= r){    int mid = l+r>>1;        if(fib[mid] > x)    id = mid, r = mid-1;        else    l = mid+1;    }    if(x-fib[id-1] <= fib[id]-x)    return fib[id-1];    return fib[id];}void update2(ll t,ll l,ll r){    if(a[t].flag)return;    if(a[t].l==a[t].r){        a[t].sum=Find(a[t].sum);        a[t].flag=1;        //cout<<"ddd "<<l<<" "<<r<<" "<<a[t].sum<<endl;        return;    }    ll mid=(a[t].l+a[t].r)/2;    if(l<=mid)update2(2*t,l,r);    if(r>mid)update2(2*t+1,l,r);    pushup(t);}ll getsum(ll t,ll l,ll r){    if(a[t].l>=l&&a[t].r<=r)return a[t].sum;    ll mid=(a[t].l+a[t].r)/2;    ll ans=0;    if(l<=mid)ans+=getsum(2*t,l,r);    if(r> mid) ans+=getsum(2*t+1,l,r);    return ans;}int main(){    // freopen("data.in","r",stdin);    // freopen("data.out","w",stdout);     fib[0]=1;fib[1]=1;     for(ll i=2;i<=90;i++)fib[i]=fib[i-1]+fib[i-2];     while(~scanf("%I64d%I64d",&n,&m)){        // cout<<"ddd "<<endl;         build(1,1,n);         //cout<<"ppp "<<endl;         while(m--){             ll l,r,op;             scanf("%I64d%I64d%I64d",&op,&l,&r);             if(op==1){                 update1(1,l,r);            //     cout<<"han 1"<<endl;             }              if(op==2){                 printf("%I64d\n",getsum(1,l,r));                // cout<<"han 2"<<endl;             }              if(op==3){                 update2(1,l,r);            //     cout<<"han 3"<<endl;             }         }     }     return 0;}/*5 10 2 1 5 3 1 5 2 1 5 1 1 10 2 1 5 3 1 5 2 1 5  4 5 1 1 3 2 1 2 3 2 3 1 2 1 2 1 4 */ 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.