原题链接
题目大意
给出一些星星的二维坐标,求星星左方,下方,左下方的星星个数。
思路
题目已经把星星按照 y 坐标从小到大排序,若 y 相等则按 x 从小到大排序。因此,在每次对一个星星进行统计时,之前出现过的星星,只要 x 坐标比其小,则必在其左,左下方,x 坐标相等的自然在下方。
代码
#include <iostream> using namespace std;
const int N = 4e4; int arr[N] = {0}; int ans[N/2] = {0}, n;
int lowbit(int x) { return x & (-x); }
void add(int i, int v) { while(i < N) { arr[i] += v; i += lowbit(i); } }
int Sum(int x) { int res = 0; while(x) { res += arr[x]; x -= lowbit(x); } return res; }
int main() { ios::sync_with_stdio(0); cin>>n; for(int i = 0; i < n; i++) { int x, y; cin>>x>>y; ans[Sum(++x)]++; add(x, 1); } for(int i = 0; i < n; i++) cout<<ans[i]<<endl;
return 0; }
|
- By:wywwzjj.top
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
点赞
https://cn-sec.com/archives/2624251.html
复制链接
复制链接
-
左青龙
- 微信扫一扫
-
-
右白虎
- 微信扫一扫
-
评论