[fjwc2015]Screen [从hzw神犇那里扒来的题]

【题目描述】

码农有一块超新星屏幕,它有N个像素点,每个像素点有亮度和灰度两个参数,记为I和H, 范围都是0~32000.

一天,码农突发奇想,想知道哪个点比较容易亮瞎眼睛。为此,他定义了一个瞎眼指数: 瞎眼指数就是灰度和亮度均不大于该像素点的像素个数。

现在,码农希望知道,瞎眼指数为0~N-1的像素点分别有多少个

【输入格式】

第一行一个数字N,代表有N个像素点。接下来N行,每行两个数字,代表该像素点的亮度和灰度。

N个像素的亮度和灰度。像素按照灰度从小到大的顺序给出,(0 <= I, H <= 32000, N < 20000)

【输出格式】

输出:瞎眼指数从0到H-1之间各等级的像素个数。

【样例输入】

5

1 1

5 1

7 1

3 3

5 5

【样例输出】

1

2

1

1

0

排序树状数组。

先按亮度从小到大排好序,这样扫到一个点的时候,之前处理过的点亮度都不大于它。

然后用树状数组标记灰度,然后统计……

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 const int mxn=35000;
 9 struct point{
10     int l,h;
11 }d[mxn];
12 int a[mxn];
13 int ans[mxn];
14 int n;
15 int cmp(point a,point b){
16     if(a.l==b.l)return a.h<b.h;
17     return a.l<b.l;
18 }
19  inline int lowbit(int x){
20     return x&-x;
21 }
22 void add(int p,int x){
23     while(p<=n){
24         a[p]+=x;
25         p+=lowbit(p);
26     }
27 }
28 int ask(int x){
29     int res=0;
30     while(x){
31         res+=a[x];
32         x-=lowbit(x);
33     }
34     return res;
35 }
36 int main(){
37     scanf("%d",&n);
38     int i,j;
39     for(i=1;i<=n;i++)scanf("%d%d",&d[i].l,&d[i].h);
40     sort(d+1,d+n+1,cmp);
41     for(i=1;i<=n;i++){
42         int tmp=ask(d[i].h);
43         add(d[i].h,1);
44         ans[tmp]++;
45     }
46     for(i=0;i<n;i++)printf("%d
",ans[i]);
47 }
原文地址:https://www.cnblogs.com/SilverNebula/p/5682814.html