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分治的更多相关文章

  1. BZOJ.1935.[SHOI2007]Tree园丁的烦恼(CDQ分治 三维偏序)

    题目链接 矩形查询可以拆成四个点的前缀和查询(树套树显然 但是空间不够) 每个操作表示为(t,x,y),t默认有序,对x分治,y用树状数组维护 初始赋值需要靠修改操作实现. //119964kb 43 ...

  2. BZOJ 1935: [Shoi2007]Tree 园丁的烦恼( 差分 + 离散化 + 树状数组 )

    假如矩阵范围小一点就可以直接用二维树状数组维护. 这道题,  差分答案, 然后一维排序, 另一维离散化然后树状数组维护就OK了. ----------------------------------- ...

  3. BZOJ 1935: [Shoi2007]Tree 园丁的烦恼 [树状数组 离线 离散化]

    传送门 刚才我还在郁闷网上怎么没人用$CDQ$分治做 突然发现根本没有时间序.... #include<iostream> #include<cstdio> #include& ...

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

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

  5. BZOJ1935:[SHOI2007]Tree 园丁的烦恼(CDQ分治)

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

  6. bzoj1382 1935: [Shoi2007]Tree 园丁的烦恼

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

  7. 1935: [Shoi2007]Tree 园丁的烦恼

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

  8. BZOJ 1935 Tree 园丁的烦恼 CDQ分治/主席树

    CDQ分治版本 我们把询问拆成四个前缀和,也就是二维前缀和的表达式, 我们把所有操作放入一个序列中 操作1代表在x,y出现一个树 操作2代表加上在x,y内部树的个数 操作3代表减去在x,y内部树的个数 ...

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

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

随机推荐

  1. C# 委托(delegate)、泛型委托和Lambda表达式

    目录 # 什么是委托 # 委托声明.实例化和调用 1.声明 2.委托的实例化 3.委托实例的调用 4.委托完整的简单示例 #泛型委托 1.Func委托 2.Action委托 3.Predicate委托 ...

  2. 【JDK】JDK源码分析-HashMap(1)

    概述 HashMap 是 Java 开发中最常用的容器类之一,也是面试的常客.它其实就是前文「数据结构与算法笔记(二)」中「散列表」的实现,处理散列冲突用的是“链表法”,并且在 JDK 1.8 做了优 ...

  3. [系列] Go - chan 通道

    目录 概述 声明 chan 写入 chan 读取 chan 关闭 chan 示例 推荐阅读 概述 原来分享基础语法的时候,还未分享过 chan 通道,这次把它补上. chan 可以理解为队列,遵循先进 ...

  4. 【Python-Django后端开发】配置静态文件详解!!!

    配置前端静态文件 1. 准备静态文件 2. 指定静态文件加载路径 STATIC_URL = '/static/' # 配置静态文件加载路径 STATICFILES_DIRS = [os.path.jo ...

  5. Downgrade extraction on phones running Android 7/8/9

    Now it's more and more difficult for forensic tools to extract evidence from smartphone running Andr ...

  6. Is it a full physical image???

    My friend asked me why she could not find some important files in a physical image acquired from an ...

  7. 调试过程中发现按f5无法走进jdk源码

    debug 模式 ,在fis=new FileInputStream(file); 行打断点 调试过程中发现按f5无法走进jdk源码 package com.lzl.spring.test; impo ...

  8. 洛谷P2763题解

    吐槽一下:蜜汁UKE是什么玩意?! 题目分析: 观察题面,对于给定的组卷要求,计算满足要求的组卷方案,可以发现这是一道明显的有条件的二分图匹配问题,于是考虑建模. 建一个超级源点,一个超级汇点:源点与 ...

  9. Python学习系列(四)Python 入门语法规则2

    Python学习系列(四)Python 入门语法规则2 2017-4-3 09:18:04 编码和解码 Unicode.gbk,utf8之间的关系 2.对于py2.7, 如果utf8>gbk, ...

  10. ubuntu安装伪分布式Hadoop3.1.2

    作业要求:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3223 本文是基于已经安装好的ubuntu环境上搭建伪分布式hadoop,在 ...