Rikka with Mista 线段树求交点个数
由于上下线段是不可能有交点的
可以先看左右线段树,按照y递增的顺序,对点进行排序。
升序构造,那么对于从某一点往下的射线,对于L,R进行区间覆盖,线段交点个数就是单点的被覆盖的次数。
降序构造,那么对于从某个点从下往上的射线,所有y坐标比期大的点都进行了区间覆盖,那么单点就是答案。
最近脑子不太好。。。思路一定要清晰啊。。。
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<vector>
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define LL long long
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
const int maxx = 2e5+;
struct node{
int l,r,laze;
int w;
}tree[maxx<<];
vector<int>vx;
vector<int>vy;
struct Point{
int x,y;
char op;
}p[maxx];
int getx(int x){
return lower_bound(vx.begin(),vx.end(),x)-vx.begin()+;
}
int gety(int x){
return lower_bound(vy.begin(),vy.end(),x)-vy.begin()+;
}
void push_donw(int rt){
if (tree[rt].laze){
tree[lson].laze+=tree[rt].laze;
tree[rson].laze+=tree[rt].laze;
tree[lson].w+=tree[rt].laze*(tree[lson].r-tree[lson].l+);
tree[rson].w+=tree[rt].laze*(tree[rson].r-tree[rson].l+);
tree[rt].laze=;
}
}
void buildtree(int rt,int l,int r){
tree[rt].l=l;
tree[rt].r=r;
tree[rt].laze=;
tree[rt].w=;
if (l==r){
return ;
}
int mid=(l+r)>>;
buildtree(lson,l,mid);
buildtree(rson,mid+,r);
}
void update(int rt,int ql,int qr,int w){
int l=tree[rt].l;
int r=tree[rt].r;
if (ql<=l && r<=qr){
tree[rt].laze+=w;
tree[rt].w+=w*(r-l+);
return;
}
int mid=(l+r)>>;
push_donw(rt);
if (qr<=mid){
update(lson,ql,qr,w);
}else if(ql>mid){
update(rson,ql,qr,w);
}else {
update(lson,ql,mid,w);
update(rson,mid+,qr,w);
}
tree[rt].w=tree[lson].w+tree[rson].w;
}
int query(int rt,int pos){
int l=tree[rt].l;
int r=tree[rt].r;
if (l==r){
return tree[rt].w;
}
int mid=(l+r)>>;
push_donw(rt);
if (pos<=mid){
return query(lson,pos);
}else {
return query(rson,pos);
}
tree[rt].w=tree[lson].w+tree[rson].w;
}
bool cmp(Point a,Point b){
return a.y<b.y;
}
int main(){
int t;
int n,m,k;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&k);
vx.clear();
vy.clear();
for (int i=;i<=k;i++){
scanf("%d%d %c",&p[i].x,&p[i].y,&p[i].op);
vx.push_back(p[i].x);
vy.push_back(p[i].y);
}
sort(vx.begin(),vx.end());
sort(vy.begin(),vy.end());
vx.erase(unique(vx.begin(),vx.end()),vx.end());
vy.erase(unique(vy.begin(),vy.end()),vy.end());
sort(p+,p++k,cmp);
int sz=vx.size();
buildtree(,,sz);
LL ans=;
for (int i=;i<=k;i++){
if (p[i].op=='D'){
ans+=query(,getx(p[i].x));
}else if (p[i].op=='L'){
update(,,getx(p[i].x),);
}else if (p[i].op=='R'){
update(,getx(p[i].x),sz,);
}
}
buildtree(,,sz);
for (int i=sz;i>=;i--){
if (p[i].op=='U'){
ans+=query(,getx(p[i].x));
}else if (p[i].op=='L'){
update(,,getx(p[i].x),);
}else if (p[i].op=='R'){
update(,getx(p[i].x),sz,);
}
}
printf("%d\n",ans+);
}
return ;
}
Rikka with Mista 线段树求交点个数的更多相关文章
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- BNU 2418 Ultra-QuickSort (线段树求逆序对)
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=2418 解题报告:就是给你n个数,然后让你求这个数列的逆序对是多少?题目中n的范围是n & ...
- HDU5634 Rikka with Phi 线段树
// HDU5634 Rikka with Phi 线段树 // 思路:操作1的时候,判断一下当前区间是不是每个数都相等,在每个数相等的区间上操作.相当于lazy,不必更新到底. #include & ...
- hdu 1394 (线段树求逆序数)
<题目链接> 题意描述: 给你一个有0--n-1数字组成的序列,然后进行这样的操作,每次将最前面一个元素放到最后面去会得到一个序列,那么这样就形成了n个序列,那么每个序列都有一个逆序数,找 ...
- <Sicily>Inversion Number(线段树求逆序数)
一.题目描述 There is a permutation P with n integers from 1 to n. You have to calculate its inversion num ...
- HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由 ...
- hdu 1754 I Hate It (线段树求区间最值)
HDU1754 I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- UVA 11983 Weird Advertisement --线段树求矩形问题
题意:给出n个矩形,求矩形中被覆盖K次以上的面积的和. 解法:整体与求矩形面积并差不多,不过在更新pushup改变len的时候,要有一层循环,来更新tree[rt].len[i],其中tree[rt] ...
随机推荐
- P5562 [Celeste-B]Center of the Earth 题解
构造 因为题目只要求两位相同,所以可以暴力枚举这两位所有的可能性,方案数为\(O(n^2)\). 但是,这么做是显然不优的,因为完全没有用到第三位. 观察题目条件:n为偶数. 就想一想能不能奇数偶数分 ...
- Java IO:为什么InputStream只能读一次
http://zhangbo-peipei-163-com.iteye.com/blog/2021879 InputStream的接口规范就是这么设计的. /** * Reads the next b ...
- 转:Android新特性介绍,ConstraintLayout完全解析
转:http://blog.csdn.net/guolin_blog/article/details/53122387 本篇文章的主题是ConstraintLayout.其实ConstraintLay ...
- php7不再支持HTTP_RAW_POST_DATA,微信支付$GLOBALS[‘HTTP_RAW_POST_DATA’]获取不到数据,
升级到php7后, 发现旧的web系统有些问题, 查看后才发现原来是php7不再支持HTTP_RAW_POST_DATA 原来系统一些地方, 使用$GLOBALS[‘HTTP_RAW_POST_DAT ...
- python之高阶函数--map()和reduce()
以下为学习笔记:来自廖雪峰的官方网站 1.高阶函数:简单来说是一个函数里面嵌入另一个函数 2.python内建的了map()和reduce()函数 map()函数接收两参数,一个是函数,一个是Iter ...
- qt获取本机用户名
//获取用户名 QString getUserName() { #if 1 QStringList envVariables; envVariables << "USERNAME ...
- JavaScript--结合CSS变形、缩放能拖拽的登录框
上例图: 代码块: <!DOCTYPE html> <html> <head lang="en"> <meta charset=" ...
- KDD2016,Accepted Papers
RESEARCH TRACK PAPERS - ORAL Title & Authors NetCycle: Collective Evolution Inference in Heterog ...
- Spring_使用(JDBC)
Spring_对JDBC的支持 使用JdbcTemplate更新数据库 导入jar包 创建applicationcontext.xml <?xml version="1.0" ...
- 为什么学习React Native三点原因
React Native不到两岁,兼容Android平台刚刚1年.我学习React Native其实也就不到1年,不算长,也不算短. Paul Graham在文章中写过:大多数人真正注意到你的时候,不 ...