【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 ...
随机推荐
- hibernate学习系列-----(9)hibernate对集合属性的操作之Map集合篇
照旧,先新建一个StudentMap.java实体类,将hobby属性使用map集合接口来存放: package com.joe.entity; import java.util.Map; publi ...
- Centos 7 安装 Mysql5.7(压缩包方式)
今天装的了mysql,遇到了很多问题,好在最后一一解决了,现在记录在此,防止日后老路重走... 1.下载 当然是去官网,下一个linux下的版本,64位的 tar.gz,好吧这里贴个名字——[mysq ...
- poj 3696 The Luckiest number 欧拉函数在解a^x=1modm的应用
题意: 给一个L,求长度最小的全8数满足该数是L的倍数. 分析: 转化为求方程a^x==1modm. 之后就是各种数学论证了. 代码: //poj 3696 //sep9 #include <i ...
- 数据库性能优化之SQL语句优化1
一.问题的提出 在 应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用系统提交实 际应用后,随着数据库中数据的增加, ...
- C++ Primer Plus的若干收获--(二)
哎,真是不想吐槽考驾照的艰辛历程了.跑到大西郊,顶着大太阳,一天就能摸上个十几分钟二十分钟的车,简直不要太坑爹,这两天真是做的我屁股疼的不行. .. 今天果断不去了.仅仅可惜我的大阿根廷啊,坚持到最后 ...
- 无线网络RSSI、SSID、BSSID
获取无线网络,及无线网络的参数之前,我们先了解一下RSSI,SSID和BSSID分别是什么,当然你可以去百度或者维基百科查阅,我这里只是简单的说明一下.RSSI就是无线网络的信号强度,这个是和无线AP ...
- RandomForest 调参
在scikit-learn中,RandomForest的分类器是RandomForestClassifier,回归器是RandomForestRegressor,需要调参的参数包括两部分,第一部分是B ...
- cURL命令行工具请求网页
http://curl.haxx.se/download.html curl非常博大,用户要想使用好这个工具,除了详细学习参数之外,还需要深刻理解http的各种协议与URL的各个语法. 这里推荐几个读 ...
- valgrind的编译和使用
ubuntu 平台: valgrind 3.8.1 一. 编译 ./configure --prefix=/home/frank/test/valgrind/PC/local 报错:checking ...
- SQL : IN 和 Exists 的区别
Sql语句中IN和exists的区别及应用 表展示 首先,查询中涉及到的两个表,一个user和一个order表,具体表的内容如下: user表: order表: in 确定给定的值是否与子查询或列表中 ...