题意

给出第一象限的n个点,有m次询问,每次询问一个矩形中的点的个数.(0<=n,m<=500000,0<=xi,yi<=10000000)

题解

一眼望去不可做。

用二位前缀和的思想,一个矩形可以用以坐标轴为一对临边的四个矩形加减得到。

考虑离线,离散化。所以我们要求的只是若干个以坐标轴为一对临边的矩形的权值。

但还是不可做。

转化为序列问题,我们要求的矩形面积,其实就是每行前缀和的一段连续地和。

具体一些,就是,算出每一行的前缀和,对于横坐标相等的前缀和,我们维护他们,矩形的面积,就是这些前缀和的前面一段。同时维护一个横坐标指针x,在x右移时前缀和加上横坐标为x的点。我们发现这些操作可以用树状数组维护。

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
int const N=;
int head[N],hed[N],c[N],n,m;
int cnt,cntx,cnty,totx,toty,ans[N],tot,x[N],y[N];
struct zb{
int x,y;
}zb[N];
struct ask{
int x,xx,y,yy;
}q[N];
struct edge{
int to,nxt;
}e[N];
struct hhh{
int to,nxt,w,id;
}f[N];
void add(int u,int v){
cnt++;
e[cnt].nxt=head[u];
e[cnt].to=v;
head[u]=cnt;
}
void ad(int u,int v,int c,int x){
tot++;
f[tot].nxt=hed[u];
f[tot].w=c;
f[tot].to=v;
f[tot].id=x;
hed[u]=tot;
}
int lowbit(int x){
return x&-x;
}
void update(int x,int w){
for(int i=x;i<=cnty;i+=lowbit(i)){
c[i]+=;
}
}
int check(int x,int w){
if(x==)return ;
int ans=;
for(int i=x;i>=;i-=lowbit(i)){
ans+=c[i];
}
return ans*w;
}
void work(){
for(int i=;i<=cntx;i++){
for(int j=head[i];j;j=e[j].nxt){
int v=e[j].to;
update(v,);
}
for(int j=hed[i];j;j=f[j].nxt){
int v=f[j].to;
ans[f[j].id]+=check(v,f[j].w);
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d%d",&zb[i].x,&zb[i].y);
x[++totx]=zb[i].x;
y[++toty]=zb[i].y;
}
for(int i=;i<=m;i++){
scanf("%d%d%d%d",&q[i].x,&q[i].y,&q[i].xx,&q[i].yy);
x[++totx]=q[i].x;
y[++toty]=q[i].y;
x[++totx]=q[i].xx;
y[++toty]=q[i].yy;
}
sort(x+,x++totx);
sort(y+,y++toty);
cntx=unique(x+,x++totx)-(x+);
cnty=unique(y+,y++toty)-(y+);
for(int i=;i<=n;i++){
zb[i].x=lower_bound(x+,x++cntx,zb[i].x)-x;
zb[i].y=lower_bound(y+,y++cnty,zb[i].y)-y;
add(zb[i].x,zb[i].y);
}
for(int i=;i<=m;i++){
q[i].x=lower_bound(x+,x++cntx,q[i].x)-x;
q[i].y=lower_bound(y+,y++cnty,q[i].y)-y;
q[i].xx=lower_bound(x+,x++cntx,q[i].xx)-x;
q[i].yy=lower_bound(y+,y++cnty,q[i].yy)-y;
ad(q[i].x-,q[i].y-,,i);
ad(q[i].xx,q[i].yy,,i);
ad(q[i].x-,q[i].yy,-,i);
ad(q[i].xx,q[i].y-,-,i);
}
work();
for(int i=;i<=m;i++){
printf("%d\n",ans[i]);
}
return ;
}

[BZOJ1935][SHOI2007]Tree 园丁的烦恼(离线+树状数组)的更多相关文章

  1. [bzoj1935][Shoi2007]Tree 园丁的烦恼 _树状数组

    Tree 园丁的烦恼 bzoj-1935 Shoi-2007 题目大意:给定平面上的$n$个点,$m$次查询矩形点个数. 注释:$1\le n,m\le 5\cdot 10^5$. 想法:静态二维数点 ...

  2. [BZOJ1935][SHOI2007]Tree 园丁的烦恼(树状数组)

    题目描述 很久很久以前,在遥远的大陆上有一个美丽的国家.统治着这个美丽国家的国王是一个园艺爱好者,在他的皇家花园里种植着各种奇花异草. 有一天国王漫步在花园里,若有所思,他问一个园丁道: “最近我在思 ...

  3. [bzoj1935][shoi2007]Tree 园丁的烦恼(树状数组+离线)

    1935: [Shoi2007]Tree 园丁的烦恼 Time Limit: 15 Sec  Memory Limit: 357 MBSubmit: 980  Solved: 450[Submit][ ...

  4. BZOJ1935: [Shoi2007]Tree 园丁的烦恼

    1935: [Shoi2007]Tree 园丁的烦恼 Time Limit: 15 Sec  Memory Limit: 357 MBSubmit: 552  Solved: 220[Submit][ ...

  5. bzoj千题计划143:bzoj1935: [Shoi2007]Tree 园丁的烦恼

    http://www.lydsy.com/JudgeOnline/problem.php?id=1935 二维偏序问题 排序x,离散化树状数组维护y #include<cstdio> #i ...

  6. BZOJ1935: [Shoi2007]Tree 园丁的烦恼(树状数组 二维数点)

    题意 题目链接 Sol 二维数点板子题 首先把询问拆成四个矩形 然后离散化+树状数组统计就可以了 // luogu-judger-enable-o2 #include<bits/stdc++.h ...

  7. 初涉二维数点问题&&bzoj1935: [Shoi2007]Tree 园丁的烦恼

    离线好评 Description 很久很久以前,在遥远的大陆上有一个美丽的国家.统治着这个美丽国家的国王是一个园艺爱好者,在他的皇家花园里种植着各种奇花异草.有一天国王漫步在花园里,若有所思,他问一个 ...

  8. 【树状数组】bzoj1935 [Shoi2007]Tree 园丁的烦恼

    把y坐标离散化后,按x坐标排序,把询问拆成四个点,每次询问某个点左下角的点的个数,注意处理边界和重叠的情况. #include<cstdio> #include<algorithm& ...

  9. [bzoj4822][Cqoi2017]老C的任务&[bzoj1935][Shoi2007]Tree 园丁的烦恼

    来自FallDream的博客,未经允许,请勿转载,谢谢. 老 C 是个程序员.     最近老 C 从老板那里接到了一个任务——给城市中的手机基站写个管理系统.作为经验丰富的程序员,老 C 轻松地完成 ...

随机推荐

  1. Calender

    public static void main(String[] args) { // TODO 自动生成的方法存根 Calendar c = new GregorianCalendar(); c., ...

  2. Caffe学习--Layer分析

    Caffe_Layer 1.基本数据结构 //Layer层主要的的参数 LayerParamter layer_param_; // protobuf内的layer参数 vector<share ...

  3. RocketMQ学习笔记(8)----RocketMQ的Producer API简介

    在RocketMQ中提供了三种发送消息的模式: 1.NormalProducer(普通) 2.OrderProducer(顺序) 3.TransactionProducer(事务) 下面来介绍一下pr ...

  4. 运维派 企业面试题6 防dos攻击

    Linux运维必会的实战编程笔试题(19题) 企业实战题6:请用至少两种方法实现! 写一个脚本解决DOS攻击生产案例 提示:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到 ...

  5. 让div垂直居中

    参考链接:https://www.cnblogs.com/softwarefang/p/6095806.html 以前我的方法总是比较粗暴,纯粹通过margin来实现,这个方法的缺点不仅在于需要多次微 ...

  6. 【codeforces 29B】Traffic Lights

    [题目链接]:http://codeforces.com/problemset/problem/29/B [题意] 一辆车; 让从A开到B; 然后速度是v; (只有在信号灯前面才能停下来..否则其他时 ...

  7. linux的一页是多大

    命令 getconf PAGESIZE 结果为4096,即一页=4096字节=4KB(注意是Byte,1B=8bit) 在使用mmap映射函数时,它的实际映射单位也是以页为单位的,即不过我们把MAP_ ...

  8. ZOJ 2601 Warehouse Keeper

    Warehouse Keeper Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Origin ...

  9. ajax前台传到后台乱码,显示问号的问题

    response.setContentType("text/html;charset=gbk"); response.setHeader("Cache-Control&q ...

  10. VS2008 集成Lua解释器

    1. 登陆官网下载源代码 -> www.lua.org -> get started ->  installing  选择系统类型(这里是Windows的,所下面载 luaDist) ...