【洛谷P4148】简单题(kd-tree)
题意:
给出一个\(n*n\)的棋盘,现在有两种操作:一种是某个格子里的数字加上\(A\),另一种是询问矩阵和。
空间限制:\(20MB\),强制在线。
思路:
直接\(kd-tree\)来搞,复杂度是\(O(n\sqrt{n})\)的。
但这个题丧心病狂,卡空间不说,还卡时间。
我就是因为一开始结构体里面的构造函数多写了几条语句,卡了我整整几个小时,难受。
感觉\(kd-tree\)就这样了,本质是一种玄学暴力,只有矩阵查询复杂度稳定一点(直接上cdq分治啊)。刷不动了,这种码量大的数据结果还是有点恶心。
/*
* Author: heyuhhh
* Created Time: 2019/11/26 10:29:18
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <iomanip>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 2e5 + 5;
int n;
int D;
struct Point {
int d[2], val;
}tmp[N], T;
//取消了构造函数,另写一个结构体。
struct Node {
int mn[2], mx[2];
int l, r, sumv, sz;
Point t;
}tr[N];
bool operator < (const Point &A, const Point &B) {
return A.d[D] < B.d[D];
}
int rt;
int rub[N], top, tot;
struct kdtree {
const double E = 0.75;
int ans;
int new_node() {
if(top) return rub[top--];
return ++tot;
}
void push_up(int o) {
int ls = tr[o].l, rs = tr[o].r;
for(int i = 0; i < 2; i++) {
tr[o].mn[i] = tr[o].mx[i] = tr[o].t.d[i];
if(ls) {
tr[o].mn[i] = min(tr[o].mn[i], tr[ls].mn[i]);
tr[o].mx[i] = max(tr[o].mx[i], tr[ls].mx[i]);
}
if(rs) {
tr[o].mn[i] = min(tr[o].mn[i], tr[rs].mn[i]);
tr[o].mx[i] = max(tr[o].mx[i], tr[rs].mx[i]);
}
}
tr[o].sumv = tr[ls].sumv + tr[rs].sumv + tr[o].t.val;
tr[o].sz = 1 + tr[ls].sz + tr[rs].sz;
}
void pia(int o, int num) {
int ls = tr[o].l, rs = tr[o].r;
if(ls) pia(ls, num);
tmp[tr[ls].sz + num + 1] = Point{tr[o].t.d[0], tr[o].t.d[1], tr[o].t.val};
rub[++top] = o;
if(rs) pia(rs, tr[ls].sz + num + 1);
}
int rebuild(int l, int r, int now) {
if(l > r) return 0;
D = now;
int mid = (l + r) >> 1;
nth_element(tmp + l, tmp + mid, tmp + r + 1);
int node = new_node();
tr[node].t = tmp[mid];
tr[node].l = rebuild(l, mid - 1, now ^ 1);
tr[node].r = rebuild(mid + 1, r, now ^ 1);
push_up(node);
return node;
}
void chk(int &o, int now) {
if(tr[o].sz * E <= tr[tr[o].l].sz || tr[o].sz * E <= tr[tr[o].r].sz) {
pia(o, 0);
o = rebuild(1, tr[o].sz, now);
}
}
void insert(int &o, int now) {
if(!o) {
tr[o = new_node()].t = T;
tr[o].l = tr[o].r = 0;
push_up(o);
return;
}
D = now;
if(tr[o].t.d[D] < T.d[D]) insert(tr[o].r, now ^ 1);
else insert(tr[o].l, now ^ 1);
push_up(o);
chk(o, now);
}
bool in(int x, int y, int x1, int y1, int x2, int y2) {
return x >= x1 && x <= x2 && y >= y1 && y <= y2;
}
void query(int o, int x1, int y1, int x2, int y2) {
if(o == 0) return;
if(tr[o].mn[0] >= x1 && tr[o].mx[0] <= x2 && tr[o].mn[1] >= y1 && tr[o].mx[1] <= y2) {
ans += tr[o].sumv;
return;
}
if(tr[o].mn[0] > x2 || tr[o].mx[0] < x1 || tr[o].mn[1] > y2 || tr[o].mx[1] < y1) return;
if(in(tr[o].t.d[0], tr[o].t.d[1], x1, y1, x2, y2)) ans += tr[o].t.val;
query(tr[o].l, x1, y1, x2, y2);
query(tr[o].r, x1, y1, x2, y2);
}
}kd;
void run(){
int ans = 0;
while(true) {
int op; cin >> op;
if(op == 3) return;
if(op == 1) {
int x, y, A; cin >> x >> y >> A;
x ^= ans;
y ^= ans;
A ^= ans;
T = Point {x, y, A};
kd.insert(rt, 0);
} else {
int x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1 ^= ans;
y1 ^= ans;
x2 ^= ans;
y2 ^= ans;
kd.ans = 0;
kd.query(rt, x1, y1, x2, y2);
ans = kd.ans;
cout << ans << '\n';
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n) run();
return 0;
}
【洛谷P4148】简单题(kd-tree)的更多相关文章
- 洛谷 P4148 简单题 KD-Tree 模板题
Code: //洛谷 P4148 简单题 KD-Tree 模板题 #include <cstdio> #include <algorithm> #include <cst ...
- 洛谷 P4148 简单题 解题报告
P4148 简单题 题意 维护单点加与矩形求和,强制在线 说明 \(n\le 500000,m\le 200000\),\(4000ms / 20MB\) kd-tree 复杂度我不懂 是一颗平衡树, ...
- P4148 简单题 k-d tree
思路:\(k-d\ tree\) 提交:2次 错因:整棵树重构时的严重错误:没有维护父子关系(之前写的是假重构所以没有维护父子关系) 题解: 遇到一个新的点就插进去,如果之前出现过就把权值加上. 代码 ...
- BZOJ4066:简单题(K-D Tree)
Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 ...
- 洛谷试炼场-简单数学问题-P1045 麦森数-高精度快速幂
洛谷试炼场-简单数学问题 B--P1045 麦森数 Description 形如2^P−1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果PP是个素数,2^P-1 不一定也是素数.到19 ...
- 洛谷试炼场-简单数学问题-P1088 火星人
洛谷试炼场-简单数学问题 A--P1088 火星人 Description 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法 ...
- 洛谷P5274 优化题(ccj)
洛谷P5274 优化题(ccj) 题目背景 CCJCCJ 在前往参加 Universe \ OIUniverse OI 的途中... 题目描述 有一个神犇 CCJCCJ,他在前往参加 Universe ...
- 洛谷 P1167 刷题
洛谷 P1167 刷题 洛谷传送门 题目描述 noip临近了,小A却发现他已经不会写题了.好在现在离竞赛还有一段时间,小A决定从现在开始夜以继日地刷题.也就是说小A废寝忘食,一天二十四小时地刷题. 今 ...
- 【noip】跟着洛谷刷noip题2
noip好难呀. 上一个感觉有点长了,重开一个. 36.Vigenère 密码 粘个Openjudge上的代码 #include<cstdio> #include<iostream& ...
- 洛谷试炼场-简单数学问题-P1403 [AHOI2005]-因数
洛谷试炼场-简单数学问题 P1403 [AHOI2005]约数研究 Description 科学家们在Samuel星球上的探险得到了丰富的能源储备,这使得空间站中大型计算机"Samuel I ...
随机推荐
- coalesce搭配nullif使用
with t1 as ( select NUll as col1, '' as col2, 'aaa' as col3 ) --关于COALESCE用法 当col1 为 Null时候返回 col2 依 ...
- 服务器 vim模式下报错E37: No write since last change (add ! to override)
故障现象: 使用vim修改文件报错,系统提示如下: E37: No write since last change (add ! to override) 故障原因: 文件为只读文件,无法修改. 解决 ...
- Codeforces Round #606 (Div. 2)
传送门 A. Happy Birthday, Polycarp! 签到. Code /* * Author: heyuhhh * Created Time: 2019/12/14 19:07:57 * ...
- leetcode 1041. 困于环中的机器人
题目地址 : https://leetcode-cn.com/problems/robot-bounded-in-circle/ 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以 ...
- Java流程控制之循环语句
循环概述 循环语句可以在满足循环条件的情况下,反复执行某一段代码,这段被重复执行的代码被称为循环体语句,当反复执行这个循环体时,需要在合适的时候把循环判断条件修改为false,从而结束循环,否则循环将 ...
- JVM-Jinfo命令
jinfo JVM Configuration info这个命令作用是实时查看和调整虚拟机运行参数. 之前的jps -v口令只能查看到显示指定的参数,如果想要查看未被显示指定的参数的值就要使用jinf ...
- Samba基础配置
本文环境:CentOS 7 简介 在UNIX-like之间共享文件系统主要是通过NFS实现的,而Windows之间共享文件系统主要是通过基于NetBIOS的网上邻居实现的,1984年Andrew Tr ...
- Day02stu
⦁ 环境搭建之接口测试工具Jmeter搭建 1) 什么是Jmeter? Jmeter是一个接口测试工具,基于Java开发,是是Apche公司使用Java平台开发的一款测试工具. 运行环境需要配置:JD ...
- 在wcharczuk/go-chart图表上打印文字
先看效果: 源码 package main import ( "bytes" "fmt" "io/ioutil" & ...
- 利用zabbix监控ogg进程(Linux平台下)
前段时间生产的一个数据库的ogg进程挂了快半个月才被发现,已经起不来了,只有重新初始化再同步.因此很有必要监控下ogg的进程,这里给大家介绍如何使用zabbix监控oracle的ogg的进程.思路就是 ...