传送门

题意:

给出一个\(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. MSSQL 删除重复数据

    --删除重复数据,无标识列情况 if object_id(N'test',N'U') is not null drop table test go create table test( id INT, ...

  2. 安装 tensorflow 1.1.0;以及安装其他相似版本tensorflow遇到的问题;tensorflow 1.13.2 cuda-10环境变量配置问题;Tensorflow 指定训练时如何指定使用的GPU;

    # 安装 2.7 环境conda create -n python2. python= conda activate python2. # 安装 1.1.0 gpu版本pip # 配置环境变量expo ...

  3. PM8909 linear charger硬件概述

    电池充电是由qpnp-vm-bus.c(电池驱动BMS)和qpnp-linear-charger.c(线性充电器)组成: SMMB charger:Switch-ModeBattery Charger ...

  4. EOJ Monthly 2019.11 E. 数学题(莫比乌斯反演+杜教筛+拉格朗日插值)

    传送门 题意: 统计\(k\)元组个数\((a_1,a_2,\cdots,a_n),1\leq a_i\leq n\)使得\(gcd(a_1,a_2,\cdots,a_k,n)=1\). 定义\(f( ...

  5. 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222。

    方法一: var num = ""; var nums = []; var a = Number(prompt());//所要拼接的数字 var b = Number(prompt ...

  6. 201871010116-祁英红《面向对象程序设计(java)》第十四周学习总结

    博文正文开头格式:(2分) 项目 内容 <面向对象程序设计(java)> https://home.cnblogs.com/u/nwnu-daizh/ 这个作业的要求在哪里 https:/ ...

  7. WPF 动态生成对象属性 (dynamic)

    原文:WPF 动态生成对象属性 (dynamic) 项目中列行的数据 都需要动态生成 所以考虑到对象绑定  可需要一个动态生成属性的意思 缺点 加载速度会慢 很明显的慢 解决办法 尽量减轻动态属性的量 ...

  8. 2. 词法"陷阱"

    1. 练习2-1 某些编译器允许嵌套注释.请写一个程序测试,要求:无论是对允许嵌套注释的编译器,还是对不允许嵌套注释的编译器,该程序都能正常通过编译,但是这两者情况下执行的结果却不相同. #inclu ...

  9. mongodb重点知识总结

    Mongodb总结 一.NoSQL型数据库介绍 NoSQL,泛指非关系型的数据库.NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,尤其是大数据应用难题.NoSQL(NoSQL ...

  10. windows10安装最新的redis

    官方给的redis的windows版本最新为3,而linux版本是5 这里通过win10子系统安装,win10子系统的配置见另一篇博客https://www.cnblogs.com/MC-Curry/ ...