HDU 4417 類似求逆序數的樹狀數組

來源:互聯網
上載者:User

標籤:des   style   blog   java   color   os   

Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2250    Accepted Submission(s): 1092


Problem DescriptionMario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss’s castle as a line (the length is n), on every integer point i there is a brick on height hi. Now the question is how many bricks in [L, R] Mario can hit if the maximal height he can jump is H. 

 

InputThe first line follows an integer T, the number of test data.
For each test data:
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.
Next line contains n integers, the height of each brick, the range is [0, 1000000000].
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.) 

 

OutputFor each case, output "Case X: " (X is the case number starting from 1) followed by m lines, each line contains an integer. The ith integer is the number of bricks Mario can hit for the ith query. 

 

Sample Input110 100 5 2 7 5 4 3 8 7 7 2 8 63 5 01 3 11 9 40 1 03 5 55 5 14 6 31 5 75 7 3 

 

Sample OutputCase 1:4003120151    題目大意:給定兩個數n, m分別為數組長度,操作次數。給定長度n的數組序列,然後m次操作,每次操作輸入l  r  v   分別代表查詢數組l---r區間段中比v小的數的數目。 先說說逆序數,在數組a中若有i<j&&a[i]>a[j] 則稱之為逆序數對。求的方法是數組從左至右依次插入樹狀數組中,到第i個元素時,第i個元素前面的數和a[i]構成的逆序對數目為sum[MAX]-sum[i],其中sum[MAX]為i前面數組元素>0&&<=MAX數的數目,sum[i]為前面數組元素>0&&<=i的數目,兩者相減不就是i前面數組中大於a[i]的數的數目了嗎,對數組所有元素進行上述操作相加即為該數組逆序對總數。  對於本題,假設某次操作中輸入l、r、v。  有種方法,先找出所有小於v的元素,然後找出這些元素中id在[l,r]中的元素,其數目即為答案,找小於v的元素時很容易吧,但是怎麼找這些元素中id在[l,r]區間裡的元素呢?就用到前面所說的知識了,每找一個小於v的元素,就把其在數組中的位置即id插入樹狀數組中,找完後,在[l,r]中元素的數目即為sum[r]-sum[r-1]。 運用這些知識就能寫代碼了 ,為了代碼時間複雜度降低一些,代碼中運用了一些排序小技巧,詳見代碼:  
 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 #define N 100005 7  8 int c[N]; 9 int ans[N];10 11 struct mem{12     int l, r;13     int val;14     int id;15 }a[N];16 17 struct node{18     int num;19     int id;20 }b[N];21 22 bool cmp1(mem a,mem b){23     return a.val<b.val;24 }25 26 bool cmp2(node a,node b){27     return a.num<b.num;28 }29 30 int lowbit(int x){31     return x&(-x);32 }33 34 void update(int x){35     while(x<N){36         c[x]++;37         x+=lowbit(x);38     }39 }40 41 int get_sum(int x){42     int ans=0;43     while(x>0){44         ans+=c[x];45         x-=lowbit(x);46     }47     return ans;48 }49 50 main()51 {52     int i, j, k, kase=1;53     int t;54     int n, m;55     cin>>t;56     while(t--){57         cin>>n>>m;58         memset(c,0,sizeof(c));59         for(i=1;i<=n;i++){60             scanf("%d",&b[i].num);61             b[i].id=i;62         }63         for(i=1;i<=m;i++){64             scanf("%d %d %d",&a[i].l,&a[i].r,&a[i].val);65             a[i].l++;66             a[i].r++;67             a[i].id=i;68             ans[i]=0;69         }70         sort(a+1,a+m+1,cmp1);71         sort(b+1,b+n+1,cmp2);72         k=1;73         for(i=1;i<=m;i++){74             while(k<=n&&a[i].val>=b[k].num){75                 update(b[k].id);76                 k++;77             }78             ans[a[i].id]=get_sum(a[i].r)-get_sum(a[i].l-1);79         }80         printf("Case %d:\n",kase++);81         for(i=1;i<=m;i++)82         printf("%d\n",ans[i]);83     }84 }

 

聯繫我們

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