Time Limit: 50 Sec  Memory Limit: 20 MB

Submit: 1919  Solved: 533

[Submit][Status][Discuss]

Description

你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作:

命令

参数限制

内容

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

输入文件第一行一个正整数N。
接下来每行一个操作。每条命令除第一个数字之外,
均要异或上一次输出的答案last_ans,初始时last_ans=0。

Output

对于每个2操作,输出一个对应的答案。

Sample Input

4

1 2 3 3

2 1 1 3 3

1 1 1 1

2 1 1 0 7

3

Sample Output

3

5

HINT

数据规模和约定
1<=N<=500000,操作数不超过200000个,内存限制20M,保证答案在int范围内并且解码之后数据仍合法。
样例解释见OJ2683
新加数据一组,但未重测----2015.05.24

Source

【题解】

在做kd-tree的时候会记录这个子树的最大矩形范围。如果整个最大矩形范围都在所求的矩形范围内。就直接返回这个最大矩形所在的子树的和。

如果不满足的话就看看当前节点是否在所求矩形内。

如果在就先累加这个节点的。

再递归求解左子树和右子树;

然后加的那组数据会卡时.

要不定时重建整棵树。(10000为周期)

【代码】

#include <cstdio>
#include <algorithm> const int MAXN = 209000; using namespace std; struct point
{
int d[2], ma_x[2], mi_n[2], l, r, sum, v;
}; int n, la = 0, v, root = 0, totn, a1, b1, a2, b2, now;
point t[MAXN], op; void up_data(int rt)
{
int l = t[rt].l, r = t[rt].r;
for (int i = 0; i <= 1; i++)
{
if (l)
{
t[rt].ma_x[i] = max(t[l].ma_x[i], t[rt].ma_x[i]);
t[rt].mi_n[i] = min(t[l].mi_n[i], t[rt].mi_n[i]);
}
if (r)
{
t[rt].ma_x[i] = max(t[r].ma_x[i], t[rt].ma_x[i]);
t[rt].mi_n[i] = min(t[r].mi_n[i], t[rt].mi_n[i]);
}
}
t[rt].sum = t[l].sum + t[r].sum + t[rt].v;//重建后要更新sum值。
} void insert(int &rt, int fx)
{
if (rt == 0) //创节点。更新信息
{
rt = ++totn;
t[rt] = op;
t[rt].l = t[rt].r = 0;
for (int i = 0; i <= 1; i++)
t[rt].ma_x[i] = t[rt].mi_n[i] = t[rt].d[i];
t[rt].sum = v;
t[rt].v = v;
return;
}
else
{
if (op.d[fx] <= t[rt].d[fx])
insert(t[rt].l, 1 - fx);
else
insert(t[rt].r, 1 - fx);
}
up_data(rt);
} int query(int rt, int fx)
{
if (!rt) return 0;
if (op.mi_n[0] <= t[rt].mi_n[0] && t[rt].ma_x[0] <= op.ma_x[0] &&
op.mi_n[1] <= t[rt].mi_n[1] && t[rt].ma_x[1] <= op.ma_x[1])
return t[rt].sum; //整个子树都在范围内
int temp = 0, l = t[rt].l, r = t[rt].r;
if (op.mi_n[0] <= t[rt].d[0] && t[rt].d[0] <= op.ma_x[0] &&
op.mi_n[1] <= t[rt].d[1] && t[rt].d[1] <= op.ma_x[1]) //这个点在范围内
temp += t[rt].sum - t[l].sum - t[r].sum; //减去两个子树的就是当前这个点的
//或直接加t[rt].v
if (op.mi_n[fx] <= t[rt].d[fx])
temp += query(l, 1 - fx);
if (t[rt].d[fx] <= op.ma_x[fx])
temp += query(r, 1 - fx);
return temp;
} bool cmp(point a, point b)
{
if (a.d[now] < b.d[now])
return true;
return false;
} int build(int begin, int end, int fx)
{
int m = (begin + end) >> 1;
now = fx;
nth_element(t + begin, t + m, t + end + 1, cmp);
for (int i = 0; i <= 1; i++)
t[m].ma_x[i] = t[m].mi_n[i] = t[m].d[i];
t[m].sum = t[m].v;
if (begin < m)
t[m].l = build(begin, m - 1, 1 - fx);
else
t[m].l = 0;
if (m < end)
t[m].r = build(m + 1, end, 1 - fx);
else
t[m].r = 0;
up_data(m);
return m;
} void input_data()
{
scanf("%d", &n);
while (true)
{
int cz;
scanf("%d", &cz);
if (cz == 1)
{
if ((totn != 0) && (totn % 10000) == 0) //重建树
root = build(1, totn, 0);
scanf("%d%d%d", &op.d[0], &op.d[1], &v);
op.d[0] ^= la; op.d[1] ^= la; v ^= la;
insert(root, 0);
}
else
if (cz == 2)
{
scanf("%d%d%d%d", &op.mi_n[0], &op.mi_n[1], &op.ma_x[0], &op.ma_x[1]);
op.mi_n[0] ^= la; op.mi_n[1] ^= la;
op.ma_x[0] ^= la; op.ma_x[1] ^= la;
la = query(root, 0);
printf("%d\n", la);
}
else
break;
}
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input_data();
return 0;
}

【27.77%】【BZOJ 4066】简单题的更多相关文章

  1. bzoj 4066: 简单题 kd-tree

    4066: 简单题 Time Limit: 50 Sec  Memory Limit: 20 MBSubmit: 234  Solved: 82[Submit][Status][Discuss] De ...

  2. BZOJ 4066 简单题(KD树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4066 [题目大意] 要求维护矩阵内格子加点和矩阵查询 [题解] 往KD树上加权值点,支 ...

  3. bzoj 4066 简单题——KDtree(带重构)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4066 带部分重构的KDtree.就是那个替罪羊树思想的. 写了对拍,调了半天,发现忘了 re ...

  4. bzoj 4066: 简单题 K-D树

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4066 题解 我们把每次的修改操作都当作二维平面上多了一个权值点 对于每组询问可以看做求一 ...

  5. BZOJ 4066 简单题 ——KD-Tree套替罪羊树

    [题目分析] 直接x,y二维轮番划分,暴力即可. 套上替罪羊,打碎重构,对于时间复杂度有了保证. 写起来好麻烦,重构的技巧很棒! [代码] #include <cstdio> #inclu ...

  6. bzoj 4066: 简单题

    #include<cstdio> #include<iostream> #include<cstdlib> #include<algorithm> #d ...

  7. BZOJ 2683: 简单题

    2683: 简单题 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 913  Solved: 379[Submit][Status][Discuss] ...

  8. BZOJ 3687: 简单题 bitset

    3687: 简单题 Time Limit: 10 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 小呆开始研究集合论了,他 ...

  9. bzoj 4066 & bzoj 2683 简单题 —— K-D树(含重构)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4066 https://www.lydsy.com/JudgeOnline/problem.p ...

  10. BZOJ 2683: 简单题(CDQ分治 + 树状数组)

    BZOJ2683: 简单题(CDQ分治 + 树状数组) 题意: 你有一个\(N*N\)的棋盘,每个格子内有一个整数,初始时的时候全部为\(0\),现在需要维护两种操作: 命令 参数限制 内容 \(1\ ...

随机推荐

  1. Echarts Y轴min显示奇葩问题(做此记录)

    项目需求是根据不同情况显示最大值最小值   有9-35  12-50 9-50 三种情况 前面两种可以显示出来  但是9-50的话  最小值9显示不出来  8,7等就可以显示出来 后面想到强制从最小值 ...

  2. 在window编写好的网站往linux上发布

    得知ASP.NET CORE可以跨平台,我内心很躁动,毕竟自己喜欢的,之前没有学过linux导致一开始上手linux有点困难,按照https://www.microsoft.com/net/core# ...

  3. 初学WCF需要注意的地方

    1.WCF的元数据发布有两种方式: a.HTTP-GET方式发布数据:让客户端使用HTTP-GET方式来获取数据是比较常见的方式.所谓HTTP—GET方式,是指当客户端发送一个HTTP-GET请求时, ...

  4. WPF 入门《常用控件》

    1.GroupBox 注意: GroupBox仍然需要布局容器来放置元素.如: StackPanel面板 <GroupBox Header="select number?"& ...

  5. 一种较为隐蔽ConcurrentModificationException情形

    ConcurrentModificationException是遍历过程中修改list而抛出的错误.就像前面分析的,单线程时这种错误主要是因为使用forEach造成:遍历一个拷贝,修改原始list,造 ...

  6. 12. ZooKeeper之Java客户端API使用—创建会话。

    转自:https://blog.csdn.net/en_joker/article/details/78686649 客户端可以通过创建一个ZooKeeper(org.apache.zookeeper ...

  7. 1.一个WEB应用的开发流程

    先说项目开发过程中团队人员的分工协作. 一.人员安排 毕业至今的大部分项目都是独立完成,虽然也有和其他同事协作的时候,但自认为对团队协作的了解和认知都还有所欠缺.很清楚团队协作的重要性,但尚未有很好的 ...

  8. 【CS Round #43 C】Rectangle Partition

    [链接]点击打开链接 [题意] 有一辆火车,它的长度为L,然后假设这辆车现在随机可能地出现在0..D之间,然后假设它已经耗光了油. 问你它需要走的期望距离是多少. 这里要走的距离指的是车里最近的加油站 ...

  9. [Mobx] Use MobX actions to change and guard state

    This lesson explains how actions can be used to control and modify the state of your application. Th ...

  10. LM4990音频功放芯片

    我们选用的一种封装:我们用的是DGK封装. 典型电路图: 下面是示意图:四中封装的示意图是不一样的: 下面是真正的原理图: 高放大倍数的原理图: 查分式的: 单个输入的原理图: 下面是有关电源的选择: