【洛谷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 ...
随机推荐
- glibc提供的malloc()的调试工具
关键词:MALLOC_CHECK_.mtrace().muntrace().MALLOC_TRACE.mprobe().-lmcheck等等. 1. MALLOC_CHECK_环境变量(double ...
- 实战项目-用例评审-问题总结-Dotest-董浩
实战项目-用例评审-问题总结 内部班项目用例评审,总结的问题:供大家参考!提升用例最好的方式,可以互相执行下(评审),就会明白自己的差距或者需要避免的点在哪里.(前提是会) 1)覆盖率 原型中提到的一 ...
- 【转】Cookie和Session和Cache
1 Cookie和Session Cookie和Session都为了用来保存状态信息,都是保存客户端状态的机制,它们都是为了解决HTTP无状态的问题而所做的努力. Session可以用Cookie来实 ...
- 【洛谷5368】[PKUSC2018] 真实排名(组合数学)
点此看题面 大致题意: 有\(n\)个数字,定义一个数的排名为不小于它的数的个数.现要随机将其中\(k\)个数乘\(2\),求对于每个数有多少种方案使其排名不变. 分类讨论 对于这种题目,我们可以分类 ...
- windows10安装最新的redis
官方给的redis的windows版本最新为3,而linux版本是5 这里通过win10子系统安装,win10子系统的配置见另一篇博客https://www.cnblogs.com/MC-Curry/ ...
- 使用canal增量同步mysql数据库信息到ElasticSearch
本文介绍如何使用canal增量同步mysql数据库信息到ElasticSearch.(注意:是增量!!!) 1.简介 1.1 canal介绍 Canal是一个基于MySQL二进制日志的高性能数据同步系 ...
- 第四组项目总结(UML图设计)
第四组项目总结(UML图设计) 相关链接: 墨刀原型链接:https://pan.baidu.com/s/1qrVI_je8NONVHT_FwH6Pwg 需求文档链接:https://www.cnbl ...
- video调用直播接口:防止缓存方案
有时候我们需要调用解析过直播接口,使用video播放,但是在暂停又开始后,直播视频不会自动刷新,而是继续从暂停之前的时间点开始播放. 下面是我的解决方案代码,弟弟们请看我的下面: <!DOCTY ...
- H5混合应用之上下文切换
一.native/web/hybrid 简介 目前主流应用程序大体分为:Native App(原生应用).Web App(网页应用).Hybrid App(混合应用),它们三者的优缺点比较如下表: 应 ...
- npm install 安装依赖报错
npm ERR! Unexpected end of JSON input while parsing near '...2.4.8","karma":"~0. ...