题:http://codeforces.com/gym/101964/problem/K

分析:https://blog.csdn.net/qq_43202683/article/details/98115901

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read(){
int sum=,x=;
char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')
x=;
ch=getchar();
}
while(ch>=''&&ch<='')
sum=(sum<<)+(sum<<)+(ch^),ch=getchar();
return x?sum:-sum;
}
inline void write(ll x){
if(x<)
putchar('-'),x=-x;
if(x>)
write(x/);
putchar(x%+'');
}
#define pb push_back
#define lowbit(x) (x&(-x))
const int M=5e5+;
int ans[M];
int tree[M];
void add(int i,int x){
while(i<M)
tree[i]+=x,i+=(i&(-i));
return ;
}
int query(int i){
int res=;
while(i){
res+=tree[i];
i-=(i&(-i));
}
return res;
}
struct node{
int x,y,op,id;
bool operator <(const node &b)const{
return id<b.id;
}
}a[M],temp[M];
vector<pair<int, int> > book;
void cdq1(int l, int r)//算点对矩阵的贡献(小于等于)
{
if(r-l<=) return;
int midd = (r+l)>>;
cdq1(l, midd);
cdq1(midd, r);
int i= l,j= midd,n = ;
while(i< midd &&j< r){
if(a[i].x <= a[j].x){
if(a[i].op == ){
add(a[i].y, );
book.push_back(make_pair(a[i].y, ));
}
temp[n++] = a[i++];
}
else{
if(a[j].op==){
ans[a[j].id]+=query(a[j].y);
}
else if(a[j].op==)
ans[a[j].id]-=query(a[j].y);
temp[n++]=a[j++];
}
}
while(i<midd)
temp[n++] = a[i++];
while(j<r){
if(a[j].op == )
ans[a[j].id] += query(a[j].y);
else if(a[j].op == )
ans[a[j].id] -= query(a[j].y);
temp[n++]=a[j++];
}
for(i =l;i<r;++i)
a[i]=temp[i - l];
for(i = ; i <book.size(); ++i)
add(book[i].first, -book[i].second);
book.clear();
return;
}
void cdq2(int l,int r)
{
if(r-l<= )
return;
int midd=(l+r)>>;
cdq2(l,midd);
cdq2(midd,r);
int i= l,j=midd,n=;
while(i<midd&&j<r){
if(a[i].x<a[j].x){
if(a[i].op == ){
add(a[i].y,);
book.pb(make_pair(a[i].y, ));
}
else if(a[i].op==){
add(a[i].y,-);
book.pb(make_pair(a[i].y,-));
}
temp[n++]=a[i++];
}
else{
if(a[j].op == ){
ans[a[j].id] +=query(a[j].y-);
}
temp[n++]=a[j++];
}
}
while(i<midd)
temp[n++]=a[i++];//temp[o++] = e[lp++]?? = temp[lp++],debug???
while(j<r){
if(a[j].op == ) {
ans[a[j].id] +=query(a[j].y - );
}
temp[n++]=a[j++];
}
for(i=l;i<r;i++)
a[i]=temp[i-l];
for(i=;i<book.size();i++)
add(book[i].first,-book[i].second);
book.clear();
return;
}
int lisan[M<<];
int main(){
int n=read();
int tot=,num=;
for(int i=;i<n;i++){
int op=read();
if(op==){
a[tot].x=read(),a[tot].y=read();
a[tot].op=,a[tot].id=i;
lisan[++num]=a[tot].x,lisan[++num]=a[tot].y;
tot++;
}
else{
int x1=read(),y1=read(),x2=read(),y2=read();
a[tot].op=,a[tot].x=x1-,a[tot].y=y1-,a[tot].id=i,tot++;
a[tot].op=,a[tot].x=x2,a[tot].y=y2,a[tot].id=i,tot++;
a[tot].op=,a[tot].x=x1-,a[tot].y=y2,a[tot].id=i,tot++;
a[tot].op=,a[tot].x=x2,a[tot].y=y1-,a[tot].id=i,tot++;
lisan[++num]=x1-,lisan[++num]=x2,lisan[++num]=y1-,lisan[++num]=y2;
} }
sort(lisan+,lisan+num+);
int m=unique(lisan+,lisan++num)-lisan-;
for(int i=;i<tot;i++){
a[i].x=lower_bound(lisan+,lisan++m,a[i].x)-lisan;
a[i].y=lower_bound(lisan+,lisan++m,a[i].y)-lisan;
// cout<<a[i].x<<"~~~~"<<a[i].y<<endl;
}
cdq1(,tot); sort(a,a+tot);
cdq2(,tot);
ll ANS=0ll;
for(int i=;i<n;i++){
ANS+=ans[i];
write(ANS);
puts("");
}
return ;
}

2018SEERC Points and Rectangles (CDQ分治)的更多相关文章

  1. BZOJ 2716 Violet 3 天使玩偶 CDQ分治

    题目大意:初始给定平面上的一个点集.提供两种操作: 1.将一个点增加点集 2.查询距离一个点最小的曼哈顿距离 K-D树是啥...不会写... 我仅仅会CDQ分治 对于一个询问,查询的点与这个点的位置关 ...

  2. HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)

    Jam's problem again Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  3. HDU - 6183:Color it (线段树&动态开点||CDQ分治)

    Do you like painting? Little D doesn't like painting, especially messy color paintings. Now Little B ...

  4. CodeForces - 762E:Radio stations (CDQ分治||排序二分)

    In the lattice points of the coordinate line there are n radio stations, the i-th of which is descri ...

  5. HDU 6183 Color it cdq分治 + 线段树 + 状态压缩

    Color it Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Pro ...

  6. 【教程】简易CDQ分治教程&学习笔记

    前言 辣鸡蒟蒻__stdcall终于会CDQ分治啦!       CDQ分治是我们处理各类问题的重要武器.它的优势在于可以顶替复杂的高级数据结构,而且常数比较小:缺点在于必须离线操作. CDQ分治的基 ...

  7. BZOJ 2683 简单题 ——CDQ分治

    [题目分析] 感觉CDQ分治和整体二分有着很本质的区别. 为什么还有许多人把他们放在一起,也许是因为代码很像吧. CDQ分治最重要的是加入了时间对答案的影响,x,y,t三个条件. 排序解决了x ,分治 ...

  8. HDU5618 & CDQ分治

    Description: 三维数点 Solution: 第一道cdq分治...感觉还是很显然的虽然题目不能再傻逼了... Code: /*=============================== ...

  9. 初识CDQ分治

    [BZOJ 1176:单点修改,查询子矩阵和]: 1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 200 ...

随机推荐

  1. 了解redis

    redis:非关系型数据库,基于内存高性能,key-value存储,一般用作缓存,开源的使用ANSI C语言编写,遵守BSD协议,支持网络,可基于内存亦可持久化的日志型.Key-Value数据库,并提 ...

  2. jquery的读、写、增、删、查方法

    # 注:jquery需要导包  格式<script type="text/javascript" src="jquery-3.2.1.js">> ...

  3. 计算广告-GD广告

    算法 分配算法主要是解优化问题. 流量预测(traffic forecasting), 流量预估 库存分配, 粗力度的分配. 流量分配, 排单算法 在线分配(Online Allocation) 资料 ...

  4. SQL基础教程(第2版)第6章 函数、谓词、CASE表达式:6-3 CASE表达式

    ● 虽然CASE表达式中的ELSE子句可以省略,但为了让SQL语句更加容易理解,还是希望大家不要省略. ● CASE表达式中的END不能省略. ● 使用CASE表达式能够将SELECT语句的结果进行组 ...

  5. HDU - 1251 字典树模板题

    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).  Input输入数据的第一部 ...

  6. Maven--mirror 和 repository

    参考:http://blog.csdn.net/isea533/article/details/22437511   http://www.cnblogs.com/xdouby/p/6502925.h ...

  7. 利用python分析泰坦尼克号数据集

    1 引言 刚接触python与大数据不久,这个是学长给出的练习题目.知识积累太少,学习用了不少的时间.尽量详细的写,希望对各位的学习有所帮助. 2 背景 2.1 Kaggle 本次数据集来自于Kagg ...

  8. JavaScript学习总结(七)

    这一讲我们来学习DOM编程(十分重要),有了DOM编程,我们就可以操作任意的HTML元素了. DOM,文档对象模型 一个html页面被浏览器加载的时候,浏览器就会对整个html页面上的所有标签都会创建 ...

  9. 并发与高并发(七)-线程安全性-原子性-atomic

    一.线程安全性定义 定义:当多个线程访问某个类时,不管运行时环境采用何种调度方式或者这些线程将如何交替执行,并且在主调代码中不需要任何额外的同步或协同,这个类都能表现出正确的行为,那么就称这个类是线程 ...

  10. Dynamics CRM - 利用 JavaScript 打开指定 entity 的新建窗口并传递需要的参数

    由于业务逻辑需要,需要从某个 Entity 的 Record 中弹出其他 Entity 的创建窗口,并将当前 Entity 中的某些值传递到新打开的窗口中,具体的 JS 代码如下: //定义一个参数对 ...