阅读: 23 评论: 0 作者: over 发表于 2009-10-24 10:10 原文链接
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 #include<iostream>
2 using namespace std;
3 #define max(a,b) (a >= b ? a : b)
4 #define min(a,b) (a <= b ? a : b)
5 struct node
6 {
7 int left,right;
8 int tsum;
9 int csum;
10 node()
11 {
12 tsum = csum = 0;
13 }
14 };
15 node Tree[130001];
16 int input[15001];
17 int ans[15001];
18
19 void BuildTree(int l, int r, int root)
20 {
21 Tree[root].left = l;
22 Tree[root].right = r;
23 if(l < r)
24 {
25 int mid = (l + r) / 2;
26 BuildTree(l, mid, root * 2);
27 BuildTree(mid + 1, r, root * 2 + 1);
28 }
29 }
30 void Add(int l, int r, int root)
31 {
32 Tree[root].tsum += (r - l + 1);
33 if(Tree[root].left == l && Tree[root].right == r)
34 {
35 Tree[root].csum ++;
36 }
37 else
38 {
39 int mid = (Tree[root].left + Tree[root].right) / 2;
40 if(l <= mid)
41 {
42 Add(l, min(r,mid), root * 2);
43 }
44 if(r >= mid + 1)
45 {
46 Add(max(mid, l), r , root * 2 + 1);
47 }
48 }
49 }
50 int GetValue(int l, int r, int root)
51 {
52 int res = 0;
53 if(Tree[root].left == l && Tree[root].right == r)
54 {
55 res += Tree[root].tsum;
56 }
57 else
58 {
59 res += (r - l + 1)*Tree[root].csum;
60 int mid = (Tree[root].left + Tree[root].right) / 2;
61 if(l <= mid)
62 {
63 res += GetValue(l, min(r,mid), root * 2);
64 }
65 if(r >= mid + 1)
66 {
67 res += GetValue(max(mid + 1, l), r, root * 2 + 1);
68 }
69 }
70 return res;
71 }
72 int main()
73 {
74 int i,j,m,temp,minX,maxX,leave;
75 minX = INT_MAX;
76 maxX = INT_MIN;
77 scanf("%d",&m);
78 for(i=0; i<m; i++)
79 {
80 scanf("%d%d",&input[i],&temp);
81 minX = min(minX, input[i]);
82 maxX = max(maxX, input[i]);
83 }
84 BuildTree(minX,maxX,1);
85 for(i=0; i<m; i++)
86 {
87 leave = GetValue(minX,input[i],1);
88 Add(input[i], input[i], 1);
89 ans[leave]++;
90 }
91 for(i=0; i<m; i++)
92 {
93 printf("%d\n",ans[i]);
94 }
95 return 0;
96 }
97
推荐链接:Windows 7专题发布