逆序對 【線段樹解法】


逆序對 【線段樹解法】

求逆序對問題是一個十分經典的算法問題,通常使用歸並排序解決,經gster大神指點,寫出了逆序對線段樹寫法,順便練了練線段樹。

題目傳送門:http://noi.openjudge.cn/ch0204/7622/

代碼:

 1 /* 
 2         Segment_Tree 
 3         author: SHHHS
 4         2016-09-28 12:35:17 
 5 */ 
 6 #include "bits/stdc++.h"
 7 
 8 using namespace std ;
 9 typedef long long QAQ ;
10 const int maxN = 100100 ;
11 struct SegTree { int l , r , sum ;};
12 
13 SegTree tr[ maxN<<2 ] ;
14 int arr[ maxN ] ;
15 void Build_Tree ( int x , int y , int i ) {
16          tr[ i ].l = x ; tr[ i ].r = y ;
17          if ( x==y ) return ;
18          else {
19                   QAQ mid = ( tr[ i ].l + tr[ i ].r ) >> 1 ;
20                  Build_Tree ( x , mid , i<<1 ) ;
21                  Build_Tree ( mid +1 , y , i<<1|1 ) ;
22          }
23 }
24 
25 void Insert_Tree ( int q , int i ) {
26          if ( q==tr[ i ].l && q==tr[ i ].r ) {
27                    tr[ i ].sum ++ ;
28                    return ;
29          }
30          else {
31                    QAQ mid = ( tr[ i ].l + tr[ i ].r ) >> 1 ;
32                    if ( q>mid ) Insert_Tree ( q , i<<1|1 ) ;
33                    else if ( q<=mid ) Insert_Tree ( q , i<<1 ) ;
34          }
35          tr[ i ].sum = tr[ i<<1 ].sum + tr[ i<<1|1 ].sum ;
36 }
37 
38 QAQ Query_Tree (  int q , int w , int i ){
39          if ( q<=tr[i].l && w>=tr[i].r ) {
40                   return tr[i].sum ;
41          }
42          else {
43                   QAQ mid=(tr[i].l+tr[i].r)>>1;
44                   if ( q>mid )return Query_Tree(q,w,i<<1|1);
45                   else if ( w<=mid )return Query_Tree(q,w,i<<1);
46                   else return Query_Tree(q,mid,i<<1) + Query_Tree(mid+1,w,i<<1|1);
47         
48          }
49 }
50 
51 int main ( ) {
52          int N ; 
53          QAQ ans = 0;
54          scanf ("%d",&N);
55          for ( int i=1 ; i<=N ; ++i ) scanf ( "%d" , arr + i ) ;
56          Build_Tree ( 1 , N , 1 ) ;
57          for ( int i=1 ; i<=N ; ++i ) {
58                   int temp = arr [ i ] ;
59                   Insert_Tree ( temp , 1 ) ;
60                   ans += i - Query_Tree ( 1 , temp , 1 ) ;
61          }
62          printf ( "%lld" , ans ) ;
63          return 0 ;
64 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM