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] ...
随机推荐
- 2019-4-16-C#-在-8.0-对比-string-和-string_-的类型
title author date CreateTime categories C# 在 8.0 对比 string 和 string? 的类型 lindexi 2019-04-16 10:16:56 ...
- 洛谷P4145 上帝造题的七分钟2 / 花神游历各国(重题:洛谷SP2713 GSS4 - Can you answer these queries IV)
题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...
- Leetcode50. Pow(x, n)(快速幂)
实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, 3 输出: 9.26100 ...
- linux(centos) 下安装phpstudy 如何命令行进入mysql
配置了phpstudy 可是进不去MySQL 老是报-bash: mysqld: command not found 解决方法:在Linux环境下运行:ln -s /phpstudy/mysql/bi ...
- js对象属性方法大总结
数组(Array):系列元素的有序集合: 详细演示请看:[js入门系列演示·数组 ] http://www.cnblogs.com/thcjp/archive/2006/08/04/467761.ht ...
- Go开发 之 Go如何引用github包
- Faster RCNN算法训练代码解析(1)
这周看完faster-rcnn后,应该对其源码进行一个解析,以便后面的使用. 那首先直接先主函数出发py-faster-rcnn/tools/train_faster_rcnn_alt_opt.py ...
- leetcode 1-20 easy
1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...
- node安装镜像和webpack
先安装node 安装指南:https://npm.taobao.org/
- myeclipse10 java builder path libraries 添加tomcat
Error: The import javax.servlet cannot be resolved The import javax.servlet.http.HttpServlet ...