【BZOJ4066】简单题 KDtree
【BZOJ4066】简单题
Description
|
命令 |
参数限制 |
内容 |
|
1 x y A |
1<=x,y<=N,A是正整数 |
将格子x,y里的数字加上A |
|
2 x1 y1 x2 y2 |
1<=x1<= x2<=N 1<=y1<= y2<=N |
输出x1 y1 x2 y2这个矩形内的数字和 |
|
3 |
无 |
终止程序 |
Input
Output
Sample Input
1 2 3 3
2 1 1 3 3
1 1 1 1
2 1 1 0 7
3
Sample Output
5
HINT
题解:想了一上午KDtree为什么不能旋转,发现显然不能啊~(但听说能搞成替罪的?蒟蒻表示不懂~)
依旧是改一改估价函数就行了,这题需要每加入一些点就暴力重构整棵树
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define rep for(int i=0;i<=1;i++)
using namespace std;
struct kd
{
int sm[2],sn[2],v[2],sum,s,ls,rs;
kd (int a,int b,int c) {ls=rs=0,sm[0]=sn[0]=v[0]=a,sm[1]=sn[1]=v[1]=b,s=sum=c;}
kd (){}
};
kd t[200010],p[200010];
int n,m,A,B,C,D,root,ans;
bool cmp(kd a,kd b)
{
return (a.v[D]==b.v[D])?(a.v[D^1]<b.v[D^1]):(a.v[D]<b.v[D]);
}
void pushup(int x,int y)
{
rep t[x].sm[i]=max(t[x].sm[i],t[y].sm[i]),t[x].sn[i]=min(t[x].sn[i],t[y].sn[i]);
t[x].sum+=t[y].sum;
}
int build(int l,int r,int d)
{
if(l>r) return 0;
int mid=l+r>>1;
D=d;
nth_element(p+l,p+mid,p+r+1,cmp);
t[mid]=p[mid];
t[mid].ls=build(l,mid-1,d^1),t[mid].rs=build(mid+1,r,d^1);
if(t[mid].ls) pushup(mid,t[mid].ls);
if(t[mid].rs) pushup(mid,t[mid].rs);
return mid;
}
void ins(int y)
{
int x=root,d=0;
while(x!=y)
{
pushup(x,y);
if(t[y].v[0]==t[x].v[0]&&t[y].v[1]==t[x].v[1])
{
t[x].s+=t[y].s,n--;
return ;
}
if(t[y].v[d]<t[x].v[d]) t[x].ls=(!t[x].ls)?y:t[x].ls,x=t[x].ls;
else t[x].rs=(!t[x].rs)?y:t[x].rs,x=t[x].rs;
d^=1;
}
}
int query(int x)
{
if(!x||t[x].sm[0]<A||t[x].sn[0]>C||t[x].sm[1]<B||t[x].sn[1]>D) return 0;
if(t[x].sm[0]<=C&&t[x].sn[0]>=A&&t[x].sm[1]<=D&&t[x].sn[1]>=B) return t[x].sum;
int ret=0;
if(t[x].v[0]>=A&&t[x].v[0]<=C&&t[x].v[1]>=B&&t[x].v[1]<=D) ret+=t[x].s;
ret+=query(t[x].ls)+query(t[x].rs);
return ret;
}
int rd()
{
int ret=0; char gc=getchar();
while(gc<'0'||gc>'9') gc=getchar();
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret;
}
int main()
{
rd();
int i,j,e;
root=1,m=10000;
while(1)
{
e=rd();
if(e==3) break;
if(e==1)
{
A=rd()^ans,B=rd()^ans,C=rd()^ans;
t[++n]=kd(A,B,C),ins(n);
if(n==m)
{
for(i=1;i<=n;i++) p[i]=kd(t[i].v[0],t[i].v[1],t[i].s);
root=build(1,n,0),m+=10000;
}
}
else
{
A=rd()^ans,B=rd()^ans,C=rd()^ans,D=rd()^ans;
ans=query(root);
printf("%d\n",ans);
}
}
return 0;
}
【BZOJ4066】简单题 KDtree的更多相关文章
- bzoj4066: 简单题 K-Dtree
bzoj4066: 简单题 链接 bzoj 思路 强制在线.k-dtree. 卡常啊.空间开1e6就T了. 代码 #include <bits/stdc++.h> #define my_m ...
- [BZOJ2683][BZOJ4066]简单题
[BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...
- 洛谷 P4148 简单题 KD-Tree 模板题
Code: //洛谷 P4148 简单题 KD-Tree 模板题 #include <cstdio> #include <algorithm> #include <cst ...
- Bzoj4066 简单题
Time Limit: 50 Sec Memory Limit: 20 MBSubmit: 2185 Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...
- bzoj 4066: 简单题 kd-tree
4066: 简单题 Time Limit: 50 Sec Memory Limit: 20 MBSubmit: 234 Solved: 82[Submit][Status][Discuss] De ...
- BZOJ4066 简单题(KD-Tree)
板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- 【kd-tree】bzoj4066 简单题
同p1176. #include<cstdio> #include<cmath> #include<algorithm> using namespace std; ...
- 【bzoj4066】简单题 KD-tree
题目描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 将格子x,y里的数 ...
- bzoj 4066 简单题——KDtree(带重构)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4066 带部分重构的KDtree.就是那个替罪羊树思想的. 写了对拍,调了半天,发现忘了 re ...
随机推荐
- Bat 获取本地代码的Svn Revision并保存到变量
echo off & color 0A for /f "usebackq delims=" %%i in (`"svn info Server | findstr ...
- re中match和search的不同
re.match与re.search的区别:re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None:而re.search匹配整个字符串,直到找到一个匹配.
- libevent2源码分析之一:前言
event的本质 libevent2中的event的本质是什么?只要是非同步阻塞的运行方式,肯定遵循事件的订阅-发布模型.通过event_new的函数原型可以理解,一个event即代表一次订阅,建立起 ...
- Docker 使用docker-compose部署项目
原文地址:https://github.com/eacdy/spring-cloud-book/blob/master/3%20%E4%BD%BF%E7%94%A8Docker%E6%9E%84%E5 ...
- Android源代码解析之(四)-->HandlerThread
转载请标明出处:一片枫叶的专栏 上一篇文章中我们解说了AsyncTast的基本使用以及实现原理,我们知道AsyncTask内部是通过线程池和Handler实现的.通过对线程池和handler的封装实现 ...
- ansible远程切换用户执行命令
ansible test -l 10.0.10.1 -e "ansible_become_user=www" -m shell -a "/data/publish/pub ...
- tony_LVS DR模式 RealServer 为 Windows客户端配置
LVS DR模式 Windows 2000,Windows2003客户端配置 控制面板-添加硬件-选“是,我已经连接了此设备”点击下一步-在列表中选择添加新的硬件设备-选“安 装我充从手 ...
- YII2 学习
下载地址: http://www.yiiframework.com/download/ http://www.yiichina.com/download 直接选择basic模板下载即可 下载之后解压到 ...
- mariadb mysql 报'Access denied for user 'root'@'localhost' (using password: NO)'错误的解决
C:\Program Files\MariaDB 10.2\bin>mysql admin -u root password "x123456789" mysql Ver 1 ...
- 给 JavaScript 开发者讲讲函数式编程
本文译自:Functional Programming for JavaScript People 和大多数人一样,我在几个月前听到了很多关于函数式编程的东西,不过并没有更深入的了解.于我而言,可能只 ...