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 ...
随机推荐
- SpringMVC+Spring+Mybatis整合程序之整合
因为每个人思路不一样,所以我在这边先分享自己的思路对于mybatis开发持久层(DAO:DataBase Access Object 持久层访问对象)有两种.第一种:传统的开发持久层方式即需要程序员开 ...
- perl学习之I/O基础
1.从标准输入进行输入<STDIN> 2.从钻石操作符进行输入<> 3.参数调用@ARGV 4.向标准输出进行输出 5.用printf进行格式化输出 1.<STDIN&g ...
- Linux文件权限基础(一)
Linux中每个文件或者目录对都有一组共9个基础权限位,没三位字符被分为一组,他们分别是属主权限位,用户组权限位,其他用户权限位. 示例: 权限位说明: r --read 可读权限 对应数字4 w - ...
- 【css】清楚浏览器端缓存
/css/common.css?version=1.0.7 在css链接后面加个参数版本号控制,刷新浏览器缓存
- 关于json的dump和dumps
首先说明基本功能: dumps是将dict转化成str格式,loads是将str转化成dict格式. dump和load也是类似的功能,只是与文件操作结合起来了. 1.把python的数据,转换为js ...
- shell-code-6-输入输出重定向
解释: 1. 文件描述符0通常是标准输入(STDIN,终端),1 是标准输出(STDOUT,终端),2 是标准错误输出(STDERR). 2. 如果希望 stderr 追加到 file 文件末尾,可以 ...
- (原)iOS 用recursiveDescription打印View
今天要做一个搜索功能,用到UISearchBar 无奈背景太丑,就自定义了一个,首先用View私有方法打印一下searchBar的层次, 具体修改代码如下 for (UIView *view in _ ...
- BFS:UVa220 ACM/ICPC 1992-Othello(黑白棋)
Othello Othello is a game played by two people on an 8 x 8 board, using disks that are white on one ...
- 学习Gulp过程中遇到的一些单词含义
注:以下有的单词的含义不仅仅在gulp里面是一样的,在其他某些语言里面也是一样 nodejs Doc:https://nodejs.org/api/stream.html gulp Api:http: ...
- Java-构造一个字符串
实用StringBuffer构造字符串 package com.tj; public class MyClass implements Cloneable { public static void m ...