标签:color rip numbers amp open with family ems clr
Input
Output
Sample Input
5 1 1 5 1 7 1 3 3 5 5
Sample Output
1 2 1 1 0
Hint
Means:
给你好多星星,问你这些Level的星星有多少个,也就是说,每个星星Level是他左下角的(包括正下,正左)
Solve:
BIT || ST,因为BIT从1开始,所以算的时候每个x要先+1,然后就是算比当前x小的星星的level数就行了.PS:BIT跑的比ST快.jpg
Code(BinaryIndexTree):
1 #include <cstdio> 2 #include <cstring> 3 #define lowbit(x) x&(-x) 4 #define Clear(data , v) memset(data , v , sizeof(data)) 5 #define maxn 32010 6 int n; 7 int tree[maxn] = {0}; 8 int level[maxn] = {0}; 9 void Add(int pos , int data) 10 { 11 while(pos < maxn) 12 { 13 tree[pos] += data; 14 pos += lowbit(pos); 15 } 16 } 17 int Query(int x) 18 { 19 int sum = 0; 20 while(x > 0) 21 { 22 sum += tree[x]; 23 x -= lowbit(x); 24 } 25 return sum; 26 } 27 int main() 28 { 29 while(~scanf("%d" , &n)) 30 { 31 Clear(tree , 0) , Clear(level , 0); 32 int x , y; 33 for(int i = 0 ; i < n ; ++i) scanf("%d%d" , &x , &y) , ++x , ++level[Query(x)] , Add(x , 1); 34 for(int i = 0 ; i < n ; ++i) printf("%d\n" , level[i]); 35 } 36 }
Code(SegmentTree):
1 #include <cstdio> 2 #include <cstring> 3 #define MAXN 32010 4 #define CLR(data) memset(data , 0 , sizeof(data)) 5 int n; 6 int tree[4 * MAXN] = {0}; 7 int level[MAXN] = {0}; 8 void Update(int id , int l , int r , int x , int y) 9 { 10 if(l == r) 11 { 12 tree[id] += y; 13 return; 14 } 15 int mid = (l + r) >> 1; 16 if(x <= mid) Update(id << 1 , l , mid , x , y); 17 else Update(id << 1 | 1 , mid + 1 , r , x , y); 18 tree[id] = tree[id << 1] + tree[id << 1 | 1]; 19 } 20 int Query(int id , int l , int r , int x , int y) 21 { 22 if(x <= l && y >= r) 23 { 24 return tree[id]; 25 } 26 int mid = (l + r) >> 1; 27 if(y <= mid) Query(id * 2 , l , mid , x , y); 28 else if (x > mid) Query(id << 1 | 1 , mid + 1 , r , x , y); 29 else return Query(id << 1 , l , mid , x , mid) + Query(id << 1 | 1 , mid + 1 , r , mid + 1 , y); 30 } 31 int main() 32 { 33 while(~scanf("%d" , &n)) 34 { 35 CLR(tree) , CLR(level); 36 int x , y; 37 for(int i = 0 ; i < n ; ++i) 38 { 39 scanf("%d%d" , &x , &y); 40 ++x; 41 ++level[Query(1 , 1 , MAXN , 1 , x)]; 42 Update(1 , 1 , MAXN , x , 1); 43 } 44 for(int i = 0 ; i < n ; ++i) 45 printf("%d\n" , level[i]); 46 } 47 }
POJ2352-Stars(SegmentTree || BinaryIndexTree)
标签:color rip numbers amp open with family ems clr
原文地址:http://www.cnblogs.com/jianglingxin/p/6617342.html