传送门

题意:

给出一个\(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)的更多相关文章

  1. 洛谷 P4148 简单题 KD-Tree 模板题

    Code: //洛谷 P4148 简单题 KD-Tree 模板题 #include <cstdio> #include <algorithm> #include <cst ...

  2. 洛谷 P4148 简单题 解题报告

    P4148 简单题 题意 维护单点加与矩形求和,强制在线 说明 \(n\le 500000,m\le 200000\),\(4000ms / 20MB\) kd-tree 复杂度我不懂 是一颗平衡树, ...

  3. P4148 简单题 k-d tree

    思路:\(k-d\ tree\) 提交:2次 错因:整棵树重构时的严重错误:没有维护父子关系(之前写的是假重构所以没有维护父子关系) 题解: 遇到一个新的点就插进去,如果之前出现过就把权值加上. 代码 ...

  4. BZOJ4066:简单题(K-D Tree)

    Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作:   命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 ...

  5. 洛谷试炼场-简单数学问题-P1045 麦森数-高精度快速幂

    洛谷试炼场-简单数学问题 B--P1045 麦森数 Description 形如2^P−1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果PP是个素数,2^P-1 不一定也是素数.到19 ...

  6. 洛谷试炼场-简单数学问题-P1088 火星人

    洛谷试炼场-简单数学问题 A--P1088 火星人 Description 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法 ...

  7. 洛谷P5274 优化题(ccj)

    洛谷P5274 优化题(ccj) 题目背景 CCJCCJ 在前往参加 Universe \ OIUniverse OI 的途中... 题目描述 有一个神犇 CCJCCJ,他在前往参加 Universe ...

  8. 洛谷 P1167 刷题

    洛谷 P1167 刷题 洛谷传送门 题目描述 noip临近了,小A却发现他已经不会写题了.好在现在离竞赛还有一段时间,小A决定从现在开始夜以继日地刷题.也就是说小A废寝忘食,一天二十四小时地刷题. 今 ...

  9. 【noip】跟着洛谷刷noip题2

    noip好难呀. 上一个感觉有点长了,重开一个. 36.Vigenère 密码 粘个Openjudge上的代码 #include<cstdio> #include<iostream& ...

  10. 洛谷试炼场-简单数学问题-P1403 [AHOI2005]-因数

    洛谷试炼场-简单数学问题 P1403 [AHOI2005]约数研究 Description 科学家们在Samuel星球上的探险得到了丰富的能源储备,这使得空间站中大型计算机"Samuel I ...

随机推荐

  1. Oracle impdp导入数据临时表空间与undo表空间爆满解决实例

    Oracle impdp导入数据临时表空间与undo表空间爆满解决实例 [日期:2018-01-24] 来源:Linux社区  作者:rangle [字体:大 中 小]   针对Oracle数据迁移, ...

  2. 微信小程序通过getPhoneNumber后台PHP解密获取用户手机号码

    之前做的版本用户这块是以获取用户openid为凭证,最近改版重新整理了一下,新增注册登录以手机号码为主, 两种(正常注册手机号码-密码+一键获取当前用户手机号码) getPhoneNumber这个组件 ...

  3. Linux Thermal Framework分析及实施

    关键词:Zone.Cooling.Governor.Step Wise.Fair Share.trip等等. Linux Thermal的目的是控制系统运行过程中采样点温度,避免温度过高造成器件损坏, ...

  4. 机器学习--K近邻 (KNN)算法的原理及优缺点

    一.KNN算法原理 K近邻法(k-nearst neighbors,KNN)是一种很基本的机器学习方法. 它的基本思想是: 在训练集中数据和标签已知的情况下,输入测试数据,将测试数据的特征与训练集中对 ...

  5. 利用Github建立博客专用图库

    0.前言 当我们写博客或者文档的时候常常需要引用图片.倘或引用图片的链接是外网的,常常会出现加载过慢的情况,并且不稳定的图片来源不方便管理.所以如果建立一个博客专用的图片仓库,统一管理维护方面就方便得 ...

  6. python爬虫之csv文件

     一.二维数据写入csv文件 题目要求: 读入price2016.csv文件,将其中的数据读出,将数字部分计算百分比后输出到price2016out.csv文件中 知识点: 对于列表中存储的二维数据, ...

  7. LG5337/BZOJ5508 「TJOI2019」甲苯先生的字符串 线性动态规划+矩阵加速

    问题描述 LG5337 BZOJ5508 题解 设\(opt_{i,j}(i \in [1,n],j \in [1,26])\)代表区间\([1,i]\),结尾为\(j\)的写法. 设\(exist_ ...

  8. LG1131 「ZJOI2007」时态同步 树形DP

    问题描述 LG1131 题解 正难则反,把从一个点出发到叶子结点看做从叶子结点走到那个点. DP方程很显然. \(\mathrm{Code}\) #include<bits/stdc++.h&g ...

  9. redhat7.7(centOS7)安装ORACLE 11g出坑教程及问题总结与解决

    写在前面: 环境建议:VM 15.5,因为15.5修复了诸多bug,可以在安装过程中省去不少麻烦 添加新的虚拟机安装redhat7.7 准备redhat7.7的安装包...百度云不让上传噢噢噢噢,这里 ...

  10. HTML文件通过jQuery引入其他HTML文件报错has been blocked by CORS policy

    HTML通过jQuery引入模板 完整报错 新创建一个chrome快捷方式,命名为chrome-debug 右键属性,在目标后添加参数,原始路径如下 "C:\Program Files (x ...