比賽之樹狀數組題

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   strong   

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 #define F first 7 #define S second 8 #define MP(a,b) make_pair( make_pair(a,b) , -1 ) 9 typedef pair< pair<int,int> , int> coder ;10 const int maxn = 300010 ;11 const int N = 100000 ;12 13 int sum[N+1] , ans[maxn] ;14 coder p[maxn] ;15 16 inline int lowbit(int x){17     return x & -x ;18 }19 void update(int x) {20     for(; x<=N; x+=lowbit(x))  sum[x]++ ;21 }22 int query(int x) {23     int ret = 0 ;24     for(; x>0 ; x-=lowbit(x)) ret += sum[x] ;25     return ret ;26 }27 28 int main()29 {30     int n ;31     scanf("%d", &n);32     for(int i=1; i<=n; i++)33         scanf("%d%d", &p[i].F.F , &p[i].F.S) , p[i].S = i ;34     sort(p+1 , p+1+n) ;35     int y = 1;36 37     for(int i=1; i<=n; i++) {38         int u = p[i].S ;39         while(y<i && p[y].F.F < p[i].F.F) {40             update(p[y++].F.S) ;41         }42         ans[u] += query(p[i].F.S) ;43         if(y < i) {44             ans[u] += lower_bound(p+y , p+i , MP(p[i].F.F , p[i].F.S)) - p  - y ;45         }46     }47 48     for(int i=1; i<=n; i++)49         printf("%d\n" , ans[i]) ;50 51     return 0;52 }
AC參考代碼題目:C - Coder RatingsTime Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %lluSubmit Status Practice SPOJ RATING

Description

Some of the more elite (and not-so-elite) coders around take part in a certain unnamed programming contest. In said contest, there are multiple types of competitions. Here, we consider the Open and High School competition types. For each type, each competitor receives a rating, an integer between 1 and 100000, inclusive. A coder‘s rating is based upon his or her level of performance in matches and is calculated using a complicated formula which, thankfully, you will not be asked to implement.

Although the Open and High School ratings for a coder who has participated in both competition types lately are usually close, this is not always the case. In particular, High School matches are more about speed, since many coders are able to solve all the problems, whereas Open matches require more thinking and there is a steeper curve in terms of problem difficulty.

Problem Statement
You are given N coders (1 ≤ N ≤ 300000), conveniently numbered from 1 to N. Each of these coders participates in both High School and Open matches. For each coder, you are also given an Open rating Ai and a High School rating Hi. Coder i is said to be better than coder j if and only if both of coder i‘s ratings are greater than or equal to coder j‘s corresponding ratings, with at least one being greater. For each coder i, determine how many coders coder i is better than.

Input Format
On the first line of input is a single integer N, as described above.
N lines then follow. Line i+1 contains two space-separated integers, Ai and Hi.

Output Format
Line i should contain the number of coders that coder i is better than.

Sample Input

81798 1832862 7001075 10891568 15572575 19841033 9501656 16491014 1473

 

Sample Output

60247151

 

聯繫我們

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