题意:有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的更多相关文章

  1. TOJ 4105 Lines Counting(离线树状数组)

    4105.   Lines Counting Time Limit: 2.0 Seconds   Memory Limit: 150000K Total Runs: 152   Accepted Ru ...

  2. TOJ 4105 Lines Counting (树状数组)

    题意:给定N条线段,每条线段的两个端点L和R都是整数.然后给出M个询问,每次询问给定两个区间[L1,R1]和[L2,R2],问有多少条线段满足:L1≤L≤R1 , L2≤R≤R2 ? 题解,采用离线做 ...

  3. TOJ 2776 CD Making

    TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性...  贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...

  4. TOJ 1702.A Knight's Journey

    2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...

  5. TOJ 1139.Compromise

    2015-06-03 问题简述: 大概就是输入两段文本(用小写英文字母表示),分别用#表示一段话的结束输入,输出这两个文本的最长公共子序列. 简单的LCS问题,但是输入的是一段话了,而且公共部分比较是 ...

  6. bzoj:4105: [Thu Summer Camp 2015]平方运算

    Description   Input 第一行有三个整数N,M,p,分别代表序列的长度.平方操作与询问操作的总次数以及在平方操作中所要模的数.   接下来一行N个数代表一开始的序列{X1,X2,... ...

  7. 优先队列运用 TOJ 4123 Job Scheduling

    链接:http://acm.tju.edu.cn/toj/showp4123.html 4123.   Job Scheduling Time Limit: 1.0 Seconds   Memory ...

  8. 最小生成树 TOJ 4117 Happy tree friends

    链接http://acm.tju.edu.cn/toj/showp4117.html 4117.   Happy tree friends Time Limit: 1.0 Seconds   Memo ...

  9. TOJ 4120 Zombies VS Plants

    链接:http://acm.tju.edu.cn/toj/showp4120.html 4120.   Zombies VS Plants Time Limit: 1.0 Seconds   Memo ...

随机推荐

  1. VIM C语言函数名高亮

    VIM默认情况下,函数名是不会高亮的,将下面这段代码添加到/usr/share/vim/vim73/syntax/c.vim文件的末尾即可:   "highlight Functions s ...

  2. PAT (Basic Level) Practise (中文)-1032. 挖掘机技术哪家强(20)

    PAT (Basic Level) Practise (中文)-1032. 挖掘机技术哪家强(20) http://www.patest.cn/contests/pat-b-practise/1032 ...

  3. JAVA遍历map元素

    第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...

  4. Ukulele 原来你也在这里

  5. PHP必知必会

    MQ(消息队列) 消息队列主要用于以下场景: 1. 上传图片,用户需要迅速反馈,把上传图片的后续操作交给consumer 2. A用户对B用户发消息 3. 日志记录,APP发生的任何警告错误日志都要被 ...

  6. 【图论 搜索】bzoj1064: [Noi2008]假面舞会

    做到最后发现还是读题比赛:不过还是很好的图论题的 Description 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选 ...

  7. docker系列之基础命令-1

    1.docker基础命令 docker images 显示镜像列表 docker ps 显示容器列表 docker run IMAGE_ID 指定镜像, 运行一个容器 docker start/sto ...

  8. python安装numpy模块

    1.打开网址https://pypi.python.org/pypi/numpy,找到安装的python版本对应的numpy版本. 我的python版本是 下载的对应numpy版本是 2.将numpy ...

  9. 剑指Offer(书):对称的二叉树

    题目:请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. boolean isSymmetrical(TreeNode pRoot) { r ...

  10. localstorage与sessionstorage的使用

    cookie,sessionStorage,localeStorage的区别 cookie是存储在浏览器端,并且随浏览器的请求一起发送到服务器端的,它有一定的过期时间,到了过期时间自动会消失.sess ...