hdu 1556 Color the ball

來源:互聯網
上載者:User

標籤:style   blog   color   os   strong   io   

基礎  樹狀數組

每輸入一組數,就對染色次數進行修改;

樹狀數組中的每個節點都代表了一段線段區間,每次更新的時候,根據樹狀數組的特性可以把b以前包含的所有區間都找出來,然後把b以前的區間全部加一次染色次數。然後,再把a以前的區間全部減一次染色次數,這樣就修改了樹狀數組中的[a,b]的區間染色次數,查詢每一個點總的染色次數的時候,就可以直接向上統計每個父節點的值,就是包含這個點的所有區間被染色次數,這就是樹狀數組中向下查詢,向上統計的典型應用

<span style="font-size:14px;">#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int s[100005],a;int low(int i){    return i&(-i);}void show(int q,int w){    while(q>0)    {        s[q]+=w;        q-=low(q);    }}int yy(int num)//向上統計每個區間被染色的次數{    int sum=0;    while(num<=a)    {        sum+=s[num];        num+=low(num) ;    }    return sum;}int main(){    int b,c;    while(scanf("%d",&a)&&a)    {        memset(s,0,sizeof(s));        for(int i=1;i<=a;i++)        {            scanf("%d %d",&b,&c);            show(b-1,-1);            show(c,1);        }        for(int i=1;i<a;i++)        {            printf("%d ",yy(i));        }        printf("%d\n",yy(a));    }    return 0;}</span>




聯繫我們

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