POJ 2299 樹狀數組+離散化

來源:互聯網
上載者:User

上課前看了下題,上課時構了下思,下課敲了10min... 交了就A了... 好吧.. 這題弱爆了!

題目大意:

求一個數列中的逆序數....

這題可以用座標離散化。然後... 從大的數向樹狀數組更新.. 查的時候求和,把這個數前面的大的數求出來就OK了。

水爆了... 1Y

#include<iostream>#include<cstdio>#include<algorithm>using namespace std;struct node{    int val,pos;}a[555555];int b[555555],c[555555];bool cmp( node a,node b ){ return a.val>b.val; }int lowbit( int x ){ return x&-x; }int n;int sum( int x ){ int rec=0; while( x ) {    rec+=c[x];    x-=lowbit(x);  }  return rec;}void add( int x,int val ){ while( x<=n ) {    c[x]+=val;    x+=lowbit(x);  }}int main(){ while( scanf("%d",&n)!=EOF ) {    memset( c,0,sizeof(c) );    if( n==0 )break;    for( int i=1;i<=n;i++ )    {   scanf( "%d",&a[i].val );   a[i].pos=i;   }   sort( a+1,a+n+1,cmp );   for( int i=1;i<=n;i++ )   b[a[i].pos]=i;   __int64 ans=0;   for( int i=1;i<=n;i++ )   {   ans+=sum(b[i]-1);   add(b[i],1);      }      printf( "%I64d\n",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.