标签:man center long ase time end out ++ ==
题目链接:http://poj.org/problem?id=2481
解题思路:可以先尝试HDU 1541 Stars 。这道题相当于Stars的变形,只不过需要稍微思考。按E从大到小排序,E相同S从小到大对S E数组排序,使用一个一维BIT,初始为0,之后对于每一个[S, E]:
ans[i] = sum(S[i]) - 已经出现过的与当前[S, E]完全一样的区间个数
add(S[i], 1)
最后记得按原来顺序输出。
代码:
1 const int inf = 0x3f3f3f3f; 2 const int maxn = 1e5 + 5; 3 4 int bit[maxn], S[maxn], E[maxn], f[maxn]; 5 int n, maxp = 0, anss[maxn]; 6 7 int add(int i, int x){ 8 while(i <= maxp){ 9 bit[i] += x; 10 i += (i & -i); 11 } 12 return 0; 13 } 14 int sum(int i){ 15 int ans = 0; 16 while(i > 0){ 17 ans += bit[i]; 18 i -= (i & -i); 19 } 20 return ans; 21 } 22 int cmp(int i, int j){ 23 if(E[i] >= E[j]){ 24 if(E[i] == E[j]) return S[i] < S[j]; 25 else return true; 26 } 27 return false; 28 } 29 30 int main(){ 31 while(scanf("%d", &n) && n != 0){ 32 memset(anss, 0, sizeof(anss)); 33 memset(bit, 0, sizeof(bit)); 34 for(int i = 1; i <= n; i++) { 35 scanf("%d %d", &S[i], &E[i]); 36 S[i]++; 37 E[i]++; 38 maxp = max(maxp, E[i]); 39 f[i] = i; 40 } 41 sort(f + 1, f + n + 1, cmp); 42 int cnt = 0; 43 add(S[f[1]], 1); 44 anss[f[1]] = 0; 45 for(int i = 2; i <= n; i++){ 46 int ti = f[i], lai = f[i - 1]; 47 if(S[ti] == S[lai] && E[ti] == E[lai]) cnt++; 48 else cnt = 0; 49 int tmans = sum(S[ti]); 50 tmans -= cnt; 51 anss[ti] = tmans; 52 add(S[ti], 1); 53 } 54 for(int i = 1; i <= n; i++){ 55 printf("%d", anss[i]); 56 if(i != n) printf(" "); 57 else printf("\n"); 58 } 59 } 60 }
题目:
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 19614 | Accepted: 6670 |
Description
Input
Output
Sample Input
3 1 2 0 3 3 4 0
Sample Output
1 0 0
Hint
Source
标签:man center long ase time end out ++ ==
原文地址:http://www.cnblogs.com/bolderic/p/7288333.html