HDU 4217 樹狀數組+二分尋找

來源:互聯網
上載者:User

比賽中做出這題來讓我開心了好久啊~哈哈~溫習了一下樹狀數組。

開始驚奇和雄爺爺和我說樹狀數組沒有這個性質,我不信。後來碼畜來之後,才發現,確實樹狀數組確實沒有這麼個性質。

於是開始用vector做= =TLE.... 還好想出了二分這個好辦法... 1234ms AC了... 嘿嘿...

其實我還是挺膜拜那些4500++的code...

#include<iostream>#include<string>using namespace std;int N,K;int c[399999];int lowbit( int x ){ return x&(-x);}void modify( int num,int v ){  while( num<=N ){  c[num]+=v;  num+=lowbit(num); }}int getnum( int n ){ int x=n; int rec=0; while(x){    rec+=c[x];    x-=lowbit(x);  }  return rec;}int del( int n ){ int l=1,r=N; int mid; while( (mid=(r+l)/2)&&l<r ) {    if( getnum(mid)>=n )       r=mid;    else       l=mid+1;  }  modify(mid,-1);return mid;}int main(){ int T; scanf( "%d",&T ); int ca=1; while( T-- ) {    __int64 ans=0;    memset( c,0,sizeof(c) );    scanf( "%d %d",&N,&K );    for( int i=1;i<=N;i++ ){   modify( i,1 );   }   for( int i=1;i<=K;i++ )   {   int num;   scanf( "%d",&num );   ans+=del(num);      }      printf( "Case %d: %I64d\n",ca++,ans );  } return 0;}

聯繫我們

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