POJ 2352 Stars && POJ 2481 Cows
§树状数组小结:
- 数的形态是固定的,左低右高。
- 单点修改:向后update,向前getsum。求和时,只会计算到每个点的一个标记。
- 区间修改:向前update,向后getsum。对于每个新添加的点,相当于标记它的影响范围,即父节点记录字节点的标记。此处getsum相当于单点求值。
-
2352:Stars
- 总时间限制: 1000ms 内存限制:65536kB
- 描述
- Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.
![]()
For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.
You are to write a program that will count the amounts of the stars of each level on a given map. - 输入
- The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
- 输出
- The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.
- 样例输入
-
5 1 1 5 1 7 1 3 3 5 5
- 样例输出
-
1 2 1 1 0
- 提示
- This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
- 题解:将二维转化为一维,即对于有序数据,只有一维有影响。
- 注意数据范围取到0,故每个数据读入时+1;
View Code#include<cstdio> #include<cmath> #include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #define maxn 40000 using namespace std; int a[maxn],n,m,sum[maxn],p,q,ans[maxn]; int lowbit(int x) {return x&(-x);} void update(int x,int d){ for (int i=x;i<=maxn;i+=lowbit(i)) sum[i]+=d; } int getsum(int x){ int tmp=0; for (int i=x;i;i-=lowbit(i)) tmp+=sum[i]; return tmp; } int main(){ scanf("%d",&n); for (int i=1;i<=n;i++){ scanf("%d%d",&p,&q); ans[getsum(p+1)]++; update(p+1,1); } for (int i=0;i<n;i++) printf("%d\n",ans[i]); }
2481:Cows
- 总时间限制: 3000ms 内存限制: 65536kB
- 描述
- Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good.
Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John's N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E].
But some cows are strong and some are weak. Given two cows: cowi and cowj, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowi is stronger than cowj.
For each cow, how many cows are stronger than her? Farmer John needs your help! - 输入
- The input contains multiple test cases.
For each test case, the first line is an integer N (1 <= N <= 105), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 105) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge.
The end of the input contains a single 0. - 输出
- For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cowi.
- 样例输入
-
3 1 2 0 3 3 4 0
- 样例输出
-
1 0 0
- 提示
- Huge input and output,scanf and printf is recommended.
- 来源
- POJ Contest,Author:Mathematica@ZSU
- 题解:同上题,将S从小到大排,将E从大到小排,若将E作为一维树状数组的对象,则对于每个E,查找之前比它大的E的个数;反之,若处理S,则向后修改,向前查询。
- 注意判重,若后一组奶牛数据与之前完全相同,则ans[k[i].rank]=ans[k[i-1].rank]。
View Code#include<cstdio> #include<cmath> #include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #define maxn 110000 using namespace std; struct node{ int first,second,rank; }k[maxn]; int n,m,sum[maxn],p,q,ans[maxn]; bool cmp(node a,node b){ return a.first<b.first ||(a.first==b.first && a.second>=b.second); } bool check(node a,node b){ return (a.first==b.first) && (a.second==b.second); } int lowbit(int x) {return x&(-x);} void update(int x,int d){ for (int i=x;i;i-=lowbit(i)) sum[i]+=d; } int getsum(int x){ int tmp=0; for (int i=x;i<=maxn;i+=lowbit(i)) tmp+=sum[i]; return tmp; } int main(){ while (1){ scanf("%d",&n); if (n==0) return 0; for (int i=1;i<=n;i++){ scanf("%d%d",&k[i].first,&k[i].second); k[i].rank=i; } sort(k+1,k+n+1,cmp); for (int i=1;i<=n;i++){ if (i>1 && check(k[i],k[i-1])) ans[k[i].rank]=ans[k[i-1].rank]; else ans[k[i].rank]=getsum(k[i].second); update(k[i].second,1); } for (int i=1;i<=n;i++) printf("%d ",ans[i]);printf("\n"); memset(ans,0,sizeof(ans)); memset(sum,0,sizeof(sum)); memset(k,0,sizeof(k)); } }



浙公网安备 33010602011771号