TOJ 4105
题意:有10万个点,10万个询问,没有更新,求L1<=L<=L2,R1<=R<=R2,有多少个,
其实转换一下:就是求一个矩形 (L1,R1) ----(L2,R2) 中有多少个点(很经典的题)
这里有比较神奇的做法:基于某种性质。
解析:
首先按照 L坐标排序,每个点 也弄成 (R,R,L,0,I)这种形式
矩形形式是: (L2,R2,L,-1,I) ;和
(L2,R2,R+1,1,I);
那么,先按照L 排序;
struct Segment{
int x1,x2;
int y,t,id;
Segment(int x1 = 0,int x2 = 0,int y = 0,int t = 0,int id = 0):x1(x1),x2(x2),y(y),t(t),id(id){}
bool operator < (const Segment &a)const{
if(y != a.y) return y < a.y;
return abs(t) > abs(a.t);
}
这样;
#include<bits/stdc++.h>
using namespace std;
const int N = ; #define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define Mid int mid = (l + r) >> 1
#define root 1,(N - 5),1 struct Segment{
int x1,x2;
int y,t,id;
Segment(int x1 = ,int x2 = ,int y = ,int t = ,int id = ):x1(x1),x2(x2),y(y),t(t),id(id){}
bool operator < (const Segment &a)const{
if(y != a.y) return y < a.y;
return abs(t) > abs(a.t);
}
}ss[N * ];
int sz;
struct SegmentTree{
int sum[N << ];
void pushup(int rt){
sum[rt] = sum[rt << ] + sum[rt << | ];
}
void build(int l,int r,int rt){
sum[rt] = ;
if(l == r) return;
Mid;
build(lson);
build(rson);
}
void update(int loc,int l,int r,int rt){
if(l == r){
sum[rt] ++;
return;
}
Mid;
if(loc <= mid) update(loc,lson);
else update(loc,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L <= l && r <= R){
return sum[rt];
}
Mid;
int res = ;
if(L <= mid) res += query(L,R,lson);
if(mid + <= R) res += query(L,R,rson);
return res;
}
}sgt;
int res[N];
int main()
{
int n,m,l,r;
int l2,r2;
while(~scanf("%d",&n)){
sz = ;
for(int i = ; i < n ; i ++){
scanf("%d%d",&l,&r);
ss[sz ++] = Segment(r,r,l,);
}
scanf("%d",&m);
for(int i = ; i < m ; i ++){
scanf("%d%d%d%d",&l,&r,&l2,&r2);
ss[sz ++] = Segment(l2,r2,l,-,i);
ss[sz ++] = Segment(l2,r2,r + ,,i);
} for(int i = ; i < m ; i ++) res[i] = ;
sgt.build(root);
sort(ss,ss + sz);
for(int i = ; i < sz ; i ++){
if(ss[i].t == ) sgt.update(ss[i].x1,root);
else if(ss[i].t == -){
res[ ss[i].id ] -= sgt.query(ss[i].x1,ss[i].x2,root);
}
else{
res[ ss[i].id ] += sgt.query(ss[i].x1,ss[i].x2,root);
}
} for(int i = ; i < m ; i ++) printf("%d\n",res[i]);
}
return ;
}
先直接贴别人代码。
因为 我们是按照L 排序的,那么首先更新的事L在前的点。而它只可能对后面的点有影响,并且
是矩形 后面坐标 R1<=R<=R2;的点,因为询问时询问(R1,R2)区间,
只有一种是不符合的,R1<=R<=R2,但是L<L1的点 是不满足的 (想想)
于是我们 只有在去除这些更新的点就好了,所以对于会有:
else if(ss[i].t == -1){
res[ ss[i].id ] -= sgt.query(ss[i].x1,ss[i].x2,root);
}
在l1<=l<=l2的点都更新完了,我们在加上R+1所有的点,就是该矩形内有多少点的答案了,比较难想。
这道题也因为 区间比较大,所以普通 的二维树状数组 也是不行的
TOJ 4105的更多相关文章
- TOJ 4105 Lines Counting(离线树状数组)
4105. Lines Counting Time Limit: 2.0 Seconds Memory Limit: 150000K Total Runs: 152 Accepted Ru ...
- TOJ 4105 Lines Counting (树状数组)
题意:给定N条线段,每条线段的两个端点L和R都是整数.然后给出M个询问,每次询问给定两个区间[L1,R1]和[L2,R2],问有多少条线段满足:L1≤L≤R1 , L2≤R≤R2 ? 题解,采用离线做 ...
- TOJ 2776 CD Making
TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性... 贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...
- TOJ 1702.A Knight's Journey
2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...
- TOJ 1139.Compromise
2015-06-03 问题简述: 大概就是输入两段文本(用小写英文字母表示),分别用#表示一段话的结束输入,输出这两个文本的最长公共子序列. 简单的LCS问题,但是输入的是一段话了,而且公共部分比较是 ...
- bzoj:4105: [Thu Summer Camp 2015]平方运算
Description Input 第一行有三个整数N,M,p,分别代表序列的长度.平方操作与询问操作的总次数以及在平方操作中所要模的数. 接下来一行N个数代表一开始的序列{X1,X2,... ...
- 优先队列运用 TOJ 4123 Job Scheduling
链接:http://acm.tju.edu.cn/toj/showp4123.html 4123. Job Scheduling Time Limit: 1.0 Seconds Memory ...
- 最小生成树 TOJ 4117 Happy tree friends
链接http://acm.tju.edu.cn/toj/showp4117.html 4117. Happy tree friends Time Limit: 1.0 Seconds Memo ...
- TOJ 4120 Zombies VS Plants
链接:http://acm.tju.edu.cn/toj/showp4120.html 4120. Zombies VS Plants Time Limit: 1.0 Seconds Memo ...
随机推荐
- 洛谷 P2370 P2370 yyy2015c01的U盘
https://www.luogu.org/problemnew/show/P2370 二分+背包 #include <algorithm> #include <iostream&g ...
- RN调试
https://facebook.github.io/react-native/docs/debugging.html 热加载 RN的目标是极致的开发体验,修改文件后能在1秒内看到变化,通过以下三个特 ...
- 【js】【ios】【safari】【兼容问题】【转发】JS IOS/iPhone的Safari不兼容Javascript中的Date()问题
引用地址:http://www.cnblogs.com/yiven/p/6053872.html 1 var date = new Date('2016-11-11 11:11:11'); 2 d ...
- uboot顶层mkconfig分析
GNU make:http://www.gnu.org/software/make/manual/make.html#Rules 为了便于理解把uboot中的Makefile配置部分弄出来便于理解,这 ...
- MIP求解方法总结
*本文主要记录和分享学习到的知识,算不上原创 *参考文献见链接 本文主要简述了求解MIP问题的两大类(精确求解和近似求解),或者更细致地,三大类方法(精确算法,ε-近似算法和启发式算法).由于暂时不太 ...
- PAT Basic 1018
1018 锤子剪刀布 大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算最大. 输入格式: 输 ...
- python基础——10(三元运算符、匿名函数)
一.三元运算符 本质是if--else--的语法糖 前提:简化if--else--的结构,且两个分支有且只有一条语句 案例: a = 20 b = 30 res = a if a > b els ...
- struts向网页输出图片验证码
前言:今天做个功能需要展示图片到页面,并不是下载,在网上搜了老半天,大部分都是下载,有的话也是只能在IE下进行输出,其它浏览器就都是下载了. Action代码: public String proce ...
- mac securecrt自动保存密码
一.问题描述 mac有自带的终端,可以运行ssl和sftp,但是目录操作,文件操作和文件上传是分开的,很不方便,并且文件上传命令需要文件的全路路径. 使用securecrt能方便的解决上述的问题,并且 ...
- luogu2634 聪聪可可
点分治裸题 #include <iostream> #include <cstdio> using namespace std; int n, uu, vv, ww, ans, ...