BZOJ 1935: [Shoi2007]Tree 园丁的烦恼 +CDQ分治
1935: [Shoi2007]Tree 园丁的烦恼
参考与学习:https://www.cnblogs.com/mlystdcall/p/6219421.html
题意
在一个二维平面中有n颗树,有m次询问,要求回答在一个矩形方框中的树的个数。
思路
这是一个(x,y)为偏序的题目。这道题先用CDQ对x进行排序降维。然后利用树状数组对y进行处理。复杂度为O(N*logN * logN)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int ,pii> p3;
//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B; A <= C; ++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------show time----------------------*/
const int maxl = ;
const int maxn = ; struct node
{
int id,x,y,type,val; bool operator<(const node & a)const{
if(a.x == x)return type < a.type;
return x < a.x; //按照x排序
}
}a[maxn*],b[maxn*];
ll ans[maxn];
int n,m,tot,askid,maxy = -; void add(int type,int x,int y,int val,int id){
tot++;
a[tot].type = type;
a[tot].x = x;
a[tot].y = y;
a[tot].val = val;
a[tot].id = id;
}
ll sum[maxl]; //树状数组
int lowbit(int x){
return x & (-x);
}
void update(int x,int c){
while(x <= maxy){
sum[x]+=c;
x += lowbit(x);
}
}
ll query(int x){
ll cnt = ;
while(x > ){
cnt += sum[x];
x -= lowbit(x);
}
return cnt;
}
void clearb(int x){
while(x <= maxy){
if(sum[x])sum[x] = ;
else break;
x += lowbit(x);
}
}
void CDQ(int L, int R){
if(L >= R)return ;
int mid = (L + R)>>;
CDQ(L, mid); CDQ(mid+, R); int q1 = L, q2 = mid+;
for(int i=L; i<=R; i++){
if((q1 <= mid && a[q1] < a[q2]) || q2 > R){
if(a[q1].type == ){
update(a[q1].y, );
}
b[i] = a[q1++];
}
else {
if(a[q2].type == )ans[a[q2].id] += a[q2].val * query(a[q2].y);
b[i] = a[q2++];
}
}
for(int i=L; i<=R;i++){
a[i] = b[i];
clearb(b[i].y);
} }
int main(){
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++){ int x,y;
tot++;
scanf("%d%d", &x, &y);x+=,y+=;
a[tot].x = x;a[tot].y = y;
a[tot].id = ;a[tot].type = ;
maxy = max(maxy, y);
} for(int i=; i<=m; i++){
int x1,y1,x2,y2;
askid++;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
x1+=,y1+=,x2+=,y2+=;
maxy = max(maxy, max(y1,y2));
add(,x1-,y1-,,askid);
add(,x1-,y2,-,askid);
add(,x2,y1-,-,askid);
add(,x2,y2,,askid);
}
CDQ(,tot);
for(int i=; i<=askid; i++){
printf("%lld\n", ans[i]);
}
return ;
}
BZOJ1935
BZOJ 1935: [Shoi2007]Tree 园丁的烦恼 +CDQ分治的更多相关文章
- BZOJ.1935.[SHOI2007]Tree园丁的烦恼(CDQ分治 三维偏序)
题目链接 矩形查询可以拆成四个点的前缀和查询(树套树显然 但是空间不够) 每个操作表示为(t,x,y),t默认有序,对x分治,y用树状数组维护 初始赋值需要靠修改操作实现. //119964kb 43 ...
- BZOJ 1935: [Shoi2007]Tree 园丁的烦恼( 差分 + 离散化 + 树状数组 )
假如矩阵范围小一点就可以直接用二维树状数组维护. 这道题, 差分答案, 然后一维排序, 另一维离散化然后树状数组维护就OK了. ----------------------------------- ...
- BZOJ 1935: [Shoi2007]Tree 园丁的烦恼 [树状数组 离线 离散化]
传送门 刚才我还在郁闷网上怎么没人用$CDQ$分治做 突然发现根本没有时间序.... #include<iostream> #include<cstdio> #include& ...
- bzoj 1935: [Shoi2007]Tree 园丁的烦恼
Description 很久很久以前,在遥远的大陆上有一个美丽的国家.统治着这个美丽国家的国王是一个园艺爱好者,在他的皇家花园里种植着各种奇花异草.有一天国王漫步在花园里,若有所思,他问一个园丁道: ...
- BZOJ1935:[SHOI2007]Tree 园丁的烦恼(CDQ分治)
Description 很久很久以前,在遥远的大陆上有一个美丽的国家.统治着这个美丽国家的国王是一个园艺爱好者,在他的皇家花园里种植着各种奇花异草.有一天国王漫步在花园里,若有所思,他问一个园丁道: ...
- bzoj1382 1935: [Shoi2007]Tree 园丁的烦恼
1935: [Shoi2007]Tree 园丁的烦恼 Time Limit: 15 Sec Memory Limit: 357 MBSubmit: 1261 Solved: 578[Submit] ...
- 1935: [Shoi2007]Tree 园丁的烦恼
1935: [Shoi2007]Tree 园丁的烦恼 Time Limit: 15 Sec Memory Limit: 357 MBSubmit: 648 Solved: 273[Submit][ ...
- BZOJ 1935 Tree 园丁的烦恼 CDQ分治/主席树
CDQ分治版本 我们把询问拆成四个前缀和,也就是二维前缀和的表达式, 我们把所有操作放入一个序列中 操作1代表在x,y出现一个树 操作2代表加上在x,y内部树的个数 操作3代表减去在x,y内部树的个数 ...
- bzoj千题计划143:bzoj1935: [Shoi2007]Tree 园丁的烦恼
http://www.lydsy.com/JudgeOnline/problem.php?id=1935 二维偏序问题 排序x,离散化树状数组维护y #include<cstdio> #i ...
随机推荐
- dz6.0的一个sql注入漏洞
今天开始着手分析第一个漏洞,找了一上午靶机,发现一个含有成人内容的违法网站是用dz6.0搭的,今天就看看dz这个版本的洞了 问题函数位置:my.php第623行 if(is_array($descri ...
- GStreamer基础教程06 - 获取媒体信息
摘要 在常见的媒体文件中,通常包含一些数据(例如:歌手,专辑,编码类型等),用于描述媒体文件.通常称这些数据为元数据(Metadata:data that provides information a ...
- 【MySQL】Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and ...
线上遇到这个问题,详细信息如下: SQL state [HY000]; error code [1267]; Illegal mix of collations (utf8mb4_general_ci ...
- CentOS系统故障 | 一桩"血案"引发的容器存储驱动比较
写在前面: 由于红帽在Linux界的影响力,相信很多朋友在测试和生产系统用的是RedHat或者CentOS系统,这次我在CentOS系统上遇到了一个很有意思的故障,通过这次故障的原因分析及解决,特意写 ...
- Hadoop 系列(二)—— 集群资源管理器 YARN
一.hadoop yarn 简介 Apache YARN (Yet Another Resource Negotiator) 是 hadoop 2.0 引入的集群资源管理系统.用户可以将各种服务框架部 ...
- 不等"金九银十",金风八月,我早已拿下字节跳动的offer
字节跳动,我是在网上投的简历,之前也投过一次,简历都没通过删选,后来让师姐帮我改了一下简历,重新投另一个部门,获得了面试机会.7月23日,中午HR打电话过来预约了下午4点半面试,说会在线写代码,让我准 ...
- 阿里P8Java大牛仅用46张图让你弄懂JVM的体系结构与GC调优。
本PPT从JVM体系结构概述.GC算法.Hotspot内存管理.Hotspot垃圾回收器.调优和监控工具六大方面进行讲述.图文并茂不生枯燥. 此PPT长达46页,全部展示篇幅过长,本文优先分享前十六页 ...
- linux安装启动mongodb
1:下载 http://www.mongodb.org/downloads 在85机器上上传压缩包后解压缩. 首先在linux中解压缩安装程序 通过命令操作: 解压 tar -zxvf mongodb ...
- React的新特性 ---- Hooks ---- 的基本使用
一.react-hooks概念 React中一切皆为组件,React中组件分为类组件和函数组件,在React中如果需要记录一个组件的状态的时候,那么这个组件必须是类组件.那么能否让函数组件拥有类组件的 ...
- Unity进阶之ET网络游戏开发框架 02-ET的客户端启动流程分析
版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...