UVA10869 - Brownie Points II(线段树)

题目链接

题目大意:平面上有n个点,Stan和Ollie在玩游戏,游戏规则是:Stan先画一条竖直的线作为y轴,条件是必需要经过这个平面上的某一点,而ollie则画x轴,可是要在Stany画的y轴上经过的点中随意选择一点来作为原点画x轴。然后这个平面就被划分为4个象限,轴上的点都不算,1,3象限的点的个数就是Stan的得分,2,4就是ollie的得分。问Stan每画一条y轴,每条轴上都有个得分的最小值,求这些最小值中的最大值,而且输出这时ollie的得分。

解题思路:将y轴离散化建树,希望每次枚举一个点就能够得到这个点的Stan的得分。从左往右,从上往下处理每一个点,通过线段树能够得到y高度上的点的个数,就是第一象限的得分。然后每次查询完一个点就要将这个点在线段树中删掉。同理得到第三象限的得分。这题还要输出ollie的得分,而且要按顺序,用set来存储。

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream> using namespace std; const int maxn = 2e5;
#define lson(x) (x<<1)
#define rson(x) ((x<<1) | 1) int n;
vector<int> pos;
map<int, int>x, y;
set<int> score, vec; struct Point { int x, y, score;
}p[maxn]; int cmp_x (const Point &a, const Point &b) {
if (a.x == b.x)
return a.y > b.y;
return a.x < b.x;
} struct Node { int l, r, v, addv;
void set (int l, int r, int addv, int v) { this->l = l;
this->r = r;
this->v = v;
this->addv = addv;
}
}node[4 * maxn + 5]; void add_node (int u, int addv) { node[u].addv += addv;
node[u].v += (node[u].r - node[u].l + 1) * addv;
} void pushdown (int u) { if (node[u].addv) {
add_node(lson(u), node[u].addv);
add_node(rson(u), node[u].addv);
}
} void pushup (int u) { node[u].set (node[lson(u)].l, node[rson(u)].r, 0, node[lson(u)].v + node[rson(u)].v);
} void build (int u, int l, int r) { if (l == r) { node[u].set (l, r, 0, y[pos[l]]);
return ;
}
int m = (l + r)>>1;
build (lson(u), l, m);
build (rson(u), m + 1, r);
pushup(u);
} void update (int u, int l, int r, int add) { if (node[u].l >= l && node[u].r <= r) {
add_node(u, add);
return;
} pushdown(u);
int m = (node[u].l + node[u].r)>>1;
if (l <= m)
update (lson(u), l, r, add);
if (r > m)
update (rson(u), l, r, add);
pushup(u);
} int query (int u, int l, int r) { if (node[u].l >= l && node[u].r <= r)
return node[u].v; pushdown(u);
int m = (node[u].l + node[u].r)>>1;
int ans = 0;
if (l <= m)
ans += query(lson(u), l, r);
if (r > m)
ans += query(rson(u), l, r);
pushup(u);
return ans;
} void init () { x.clear();
y.clear();
pos.clear(); for (int i = 0; i < n; i++) { scanf ("%d%d", &p[i].x, &p[i].y);
p[i].score = 0;
x[p[i].x]++;
y[p[i].y]++;
pos.push_back(p[i].y);
} sort (pos.begin(), pos.end());
sort (p, p + n, cmp_x);
pos.erase(unique (pos.begin(), pos.end()), pos.end());
} void solve_rtop () { int x;
build(1, 0, (int)pos.size() - 1);
for (int i = 0; i < n; i++) { x = lower_bound(pos.begin(), pos.end(), p[i].y) - pos.begin();
if (x + 1 <= pos.size() - 1) {
p[i].score += query(1, x + 1, pos.size() - 1);
//printf ("%d %d %d\n", p[i].x, p[i].y, x);
}
update (1, x, x, -1);
}
} void solve_lboutom () { int x;
build(1, 0, (int)pos.size() - 1);
for (int i = n - 1; i >= 0; i--) { x = lower_bound(pos.begin(), pos.end(), p[i].y) - pos.begin();
if (x - 1 >= 0) {
p[i].score += query (1, 0, x - 1);
//printf ("%d %d %d\n", p[i].x, p[i].y, p[i].score);
}
update (1, x, x, -1);
}
} void add_ans (int tmp) { for (set<int>::iterator it = vec.begin(); it != vec.end(); it++)
score.insert(n - tmp - x[p[*it].x] - y[p[*it].y] + 1);
} void solve () { init();
solve_lboutom();
solve_rtop();
score.clear();
vec.clear(); int ans = -1;
int tmp = p[0].score;
int pre = p[0].x; for (int i = 0; i < n; i++) { if (pre == p[i].x) {
if (tmp == p[i].score)
vec.insert(i);
else if (tmp > p[i].score) {
vec.clear();
vec.insert(i);
tmp = p[i].score;
}
} else { if (ans <= tmp) {
if (ans < tmp)
score.clear();
add_ans(tmp);
}
ans = max (ans, tmp);
tmp = p[i].score;
pre = p[i].x;
vec.clear();
vec.insert(i);
}
} if (ans <= tmp) {
if (ans < tmp)
score.clear();
add_ans(tmp);
}
ans = max (ans, tmp); printf("Stan: %d; Ollie:", ans);
for (set<int>::iterator it = score.begin(); it != score.end(); it++)
printf(" %d", *it);
printf(";\n");
} int main () { while (scanf ("%d", &n) && n) {
solve();
}
return 0;
}

UVA10869 - Brownie Points II(线段树)的更多相关文章

  1. HDOJ-1156 Brownie Points II 线段树/树状数组(模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1156 在一张二位坐标系中,给定n个点的坐标,玩一个划线游戏(线必须穿过点),Stan先手画一条垂直的线,然后Ol ...

  2. POJ 2464 Brownie Points II(树状数组)

    一开始还以为对于每根竖线,只要与过了任意一点的横线相交都可以呢,这样枚举两条线就要O(n^2),结果发现自己想多了... 其实是每个点画根竖线和横线就好,对于相同竖线统计(一直不包含线上点)右上左下总 ...

  3. POJ 2464 Brownie Points II (树状数组,难题)

    题意:在平面直角坐标系中给你N个点,stan和ollie玩一个游戏,首先stan在竖直方向上画一条直线,该直线必须要过其中的某个点,然后ollie在水平方向上画一条直线,该直线的要求是要经过一个sta ...

  4. POJ - 2464 Brownie Points II 【树状数组 + 离散化】【好题】

    题目链接 http://poj.org/problem?id=2464 题意 在一个二维坐标系上 给出一些点 Stan 先画一条过一点的水平线 Odd 再画一条 过Stan那条水平线上的任一点的垂直线 ...

  5. UVA 10869 - Brownie Points II(树阵)

    UVA 10869 - Brownie Points II 题目链接 题意:平面上n个点,两个人,第一个人先选一条经过点的垂直x轴的线.然后还有一个人在这条线上穿过的点选一点作垂直该直线的线,然后划分 ...

  6. hdu 1156 && poj 2464 Brownie Points II (BIT)

    2464 -- Brownie Points II Problem - 1156 hdu分类线段树的题.题意是,给出一堆点的位置,stan和ollie玩游戏,stan通过其中一个点画垂线,ollie通 ...

  7. LightOJ 1089 - Points in Segments (II) 线段树区间修改+离散化

    http://www.lightoj.com/volume_showproblem.php?problem=1089 题意:给出许多区间,查询某个点所在的区间个数 思路:线段树,由于给出的是区间,查询 ...

  8. Day6 - E - Brownie Points II POJ - 2464

    Stan and Ollie play the game of Odd Brownie Points. Some brownie points are located in the plane, at ...

  9. CDOJ 1259 昊昊爱运动 II 线段树+bitset

    昊昊爱运动 II 昊昊喜欢运动 他N天内会参加M种运动(每种运动用一个[1,m]的整数表示) 现在有Q个操作,操作描述如下 昊昊把第l天到第r天的运动全部换成了x(x∈[1,m]) 问昊昊第l天到第r ...

随机推荐

  1. mongoDB global,startUplog

    1,show logs (Ruiy看到一些人干些事就跟拉shit一样(磨叽),就来气,娃儿的,还不知所谓,抱怨,叫器一大堆!!!,喝喝,就这它还三心二意学一个故事中的主人公小小明上课偷打`皮卡`玩,三 ...

  2. Java语言程序设计(基础篇) 第八章 多维数组

    第八章 多维数组 8.2 二维数组的基础知识 二维数组中的元素通过行和列的下标来访问. 8.2.1 声明二维数组变量并创建二维数组 下面是二维数组的语法: 数据类型[][] 数组名; int[][] ...

  3. 格而知之7:我所理解的Runtime(2)

    消息发送(Messaging) 8.以上便是runtime相关的一些数据结构,接下来我们回看一开始的疑问: objc_msgSend()函数在执行的过程中是如何找到对应的类,找到对应的方法实现的呢? ...

  4. kiki's game

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: ...

  5. Keepalived+Lvs+Mysql主主复制

    一简单介绍 Keepalived+lvs+mysql主主复制是比較经常使用的一种Mysql高可用方案,当中lvs 提供读负载均衡,Keepalived通过虚拟vip漂移实现故障自己主动转移,而Mysq ...

  6. 关于iconfont

    1. 从FONT-FACE说起 要想了解iconfont,得从一个新的css3规则说起.css3中,新增了一种样式规则,@font-face,这个规则可以用来引入自定义的字体,到客户端.以前,我们的字 ...

  7. AAC ADTS解析

    1.ADTS ADTS全称是(Audio Data Transport Stream),是AAC的一种十分常见的传输格式. 一般的AAC解码器都需要把AAC的ES流打包成ADTS的格式,一般是在AAC ...

  8. 对Textbox的值转换为带千位符和小数的Decimal字符串

    以下Function可以用于textbox的KeyUp事件: 2014-06-06 发现旧版IE不支持selectionStart还有字符串的"[]"索引获取值, 已经修复这个bu ...

  9. Matplotlib不显示图形

    安装好了Matplotlib,使用官方一个例子测试运行时,发现使用画图功能时,运行脚本老是显示不出图像,Google了一下,后来发现是matplotlibrc文件没配置好. 参考了官方文档,修改步骤如 ...

  10. python给多个发送邮件附件,参考于《python自动化运维》

    #!/usr/bin/env python #coding: utf-8 #author:luodi date:2015/02/12 #description:this is a send mail ...