[BZOJ2683][BZOJ4066]简单题

试题描述

你有一个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

终止程序

输入

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

输出

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

输入示例(BZOJ2683)


输出示例(BZOJ2683)


输入示例(BZOJ4066)


输出示例(BZOJ4066)


数据规模及约定

1<=N<=500000,操作数不超过200000个,内存限制20M,保证答案在int范围内并且解码之后数据仍合法。

题解

只有权限号可以享受的双倍经验题(雾)。

2683这道数据水,最裸的kd树就卡时限过了。。。

4066这题数据强,需要加一个定期重构,就是加入的元素达到了某一些固定值就暴力把整棵kd树重新构造一遍,这样就不会被卡成“n2 的优秀算法了”。

2683(不强制在线无定期重构慢的要死):

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 200010
#define oo 2147483647
int root, ToT, lc[maxn], rc[maxn];
struct Node {
int x[2], mx[2], mn[2], val, sum;
bool operator == (const Node& t) const { return x[0] == t.x[0] && x[1] == t.x[1]; }
} nodes[maxn]; Node x, y;
void maintain(int o) {
int l = lc[o], r = rc[o];
for(int i = 0; i < 2; i++) {
nodes[o].mx[i] = max(max(nodes[l].mx[i], nodes[r].mx[i]), nodes[o].x[i]);
nodes[o].mn[i] = min(min(nodes[l].mn[i], nodes[r].mn[i]), nodes[o].x[i]);
}
nodes[o].sum = nodes[l].sum + nodes[r].sum + nodes[o].val;
// printf("maintain(%d): %d %d %d %d %d\n", o, nodes[o].sum, nodes[o].mx[0], nodes[o].mn[0], nodes[o].mx[1], nodes[o].mn[1]);
return ;
}
void add(int& o, bool cur) {
if(!o){ nodes[o = ++ToT] = x; return maintain(o); }
if(nodes[o] == x){ nodes[o].val += x.val; nodes[o].sum += x.val; return maintain(o); }
add(x.x[cur] < nodes[o].x[cur] ? lc[o] : rc[o], cur ^ 1);
return maintain(o);
}
bool all(int o) { return x.x[0] <= nodes[o].mn[0] && nodes[o].mx[0] <= y.x[0] && x.x[1] <= nodes[o].mn[1] && nodes[o].mx[1] <= y.x[1]; }
bool has(int o) { return !(nodes[o].mx[0] < x.x[0] || nodes[o].mn[0] > y.x[0] || nodes[o].mx[1] < x.x[1] || nodes[o].mn[1] > y.x[1]); }
int query(int o) {
if(!o) return 0;
int ans = 0;
if(all(lc[o])) ans += nodes[lc[o]].sum;
else if(has(lc[o])) ans += query(lc[o]);
if(all(rc[o])) ans += nodes[rc[o]].sum;
else if(has(rc[o])) ans += query(rc[o]);
int nx = nodes[o].x[0], ny = nodes[o].x[1];
if(x.x[0] <= nx && nx <= y.x[0] && x.x[1] <= ny && ny <= y.x[1]) ans += nodes[o].val;
return ans;
} int main() {
nodes[0].mx[0] = nodes[0].mx[1] = -oo;
nodes[0].mn[0] = nodes[0].mn[1] = oo;
nodes[0].val = nodes[0].sum = 0;
int n = read();
n = read();
while(n < 3) {
if(n == 1) {
x.x[0] = read(); x.x[1] = read(); x.val = read();
add(root, 1);
}
if(n == 2) {
x.x[0] = read(); x.x[1] = read(); y.x[0] = read(); y.x[1] = read();
printf("%d\n", query(root));
}
n = read();
} return 0;
}

4066(强制在线加定期重构但还是很慢= =):

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 200010
#define oo 2147483647
int root, ToT, lc[maxn], rc[maxn];
bool Cur;
struct Node {
int x[2], mx[2], mn[2], val, sum;
bool operator == (const Node& t) const { return x[0] == t.x[0] && x[1] == t.x[1]; }
bool operator < (const Node& t) const { return x[Cur] < t.x[Cur]; }
} nodes[maxn]; Node x, y;
void maintain(int o) {
int l = lc[o], r = rc[o];
for(int i = 0; i < 2; i++) {
nodes[o].mx[i] = max(max(nodes[l].mx[i], nodes[r].mx[i]), nodes[o].x[i]);
nodes[o].mn[i] = min(min(nodes[l].mn[i], nodes[r].mn[i]), nodes[o].x[i]);
}
nodes[o].sum = nodes[l].sum + nodes[r].sum + nodes[o].val;
// printf("maintain(%d): %d %d %d %d %d\n", o, nodes[o].sum, nodes[o].mx[0], nodes[o].mn[0], nodes[o].mx[1], nodes[o].mn[1]);
return ;
}
void add(int& o, bool cur) {
if(!o){ nodes[o = ++ToT] = x; return maintain(o); }
if(nodes[o] == x){ nodes[o].val += x.val; nodes[o].sum += x.val; return maintain(o); }
add(x.x[cur] < nodes[o].x[cur] ? lc[o] : rc[o], cur ^ 1);
return maintain(o);
}
void build(int& o, int L, int R, int cur) {
if(L > R){ o = 0; return ; }
int M = L + R >> 1; o = M;
Cur = cur; nth_element(nodes + L, nodes + M, nodes + R + 1);
build(lc[o], L, M - 1, cur ^ 1); build(rc[o], M + 1, R, cur ^ 1);
return maintain(o);
}
bool all(int o) { return x.x[0] <= nodes[o].mn[0] && nodes[o].mx[0] <= y.x[0] && x.x[1] <= nodes[o].mn[1] && nodes[o].mx[1] <= y.x[1]; }
bool has(int o) { return !(nodes[o].mx[0] < x.x[0] || nodes[o].mn[0] > y.x[0] || nodes[o].mx[1] < x.x[1] || nodes[o].mn[1] > y.x[1]); }
int query(int o) {
if(!o) return 0;
int ans = 0;
if(all(lc[o])) ans += nodes[lc[o]].sum;
else if(has(lc[o])) ans += query(lc[o]);
if(all(rc[o])) ans += nodes[rc[o]].sum;
else if(has(rc[o])) ans += query(rc[o]);
int nx = nodes[o].x[0], ny = nodes[o].x[1];
if(x.x[0] <= nx && nx <= y.x[0] && x.x[1] <= ny && ny <= y.x[1]) ans += nodes[o].val;
return ans;
} int main() {
nodes[0].mx[0] = nodes[0].mx[1] = -oo;
nodes[0].mn[0] = nodes[0].mn[1] = oo;
nodes[0].val = nodes[0].sum = 0;
int n = read(), lastans = 0;
n = read();
while(n < 3) {
if(n == 1) {
x.x[0] = read() ^ lastans; x.x[1] = read() ^ lastans; x.val = read() ^ lastans;
add(root, 1);
if(ToT % 1000 == 0) build(root, 1, ToT, 1);
}
if(n == 2) {
x.x[0] = read() ^ lastans; x.x[1] = read() ^ lastans; y.x[0] = read() ^ lastans; y.x[1] = read() ^ lastans;
printf("%d\n", lastans = query(root));
}
n = read();
} return 0;
}

其实这道题我第一眼看上去是树套树删边题,结果一看内存限制20M。。。还好没写树套树= =

[BZOJ2683][BZOJ4066]简单题的更多相关文章

  1. 【BZOJ1176】[Balkan2007]Mokia/【BZOJ2683】简单题 cdq分治

    [BZOJ1176][Balkan2007]Mokia Description 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=1600 ...

  2. bzoj4066: 简单题 K-Dtree

    bzoj4066: 简单题 链接 bzoj 思路 强制在线.k-dtree. 卡常啊.空间开1e6就T了. 代码 #include <bits/stdc++.h> #define my_m ...

  3. Bzoj4066 简单题

    Time Limit: 50 Sec  Memory Limit: 20 MBSubmit: 2185  Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...

  4. 【BZOJ2683】简单题 [分治][树状数组]

    简单题 Time Limit: 50 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 你有一个N*N的棋盘,每个格子内有一 ...

  5. BZOJ4066 简单题(KD-Tree)

    板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...

  6. 【bzoj1176】[Balkan2007]Mokia/【bzoj2683】简单题 CDQ分治+树状数组

    bzoj1176 题目描述 维护一个W*W的矩阵,初始值均为S(题目描述有误,这里的S没有任何作用!).每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数 ...

  7. 【BZOJ2683】简单题

    cdq分治妙啊 (被改过题面的)原题: dydxh所出的题目是这样的:有一个N*N矩阵,给出一系列的修改和询问,修改是这样的:将(x,y)中的数字加上k,而询问是这样的:求(x1,y1)到(x2,y2 ...

  8. 【kd-tree】bzoj4066 简单题

    同p1176. #include<cstdio> #include<cmath> #include<algorithm> using namespace std; ...

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

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

随机推荐

  1. 判断一个点是否在多边形内部,射线法思路,C#实现

    感谢原作者,原理请看原作者的文章 http://www.html-js.com/article/1517 C#实现 public string rayCasting(PointF p, PointF[ ...

  2. OWIN-WebAPI-Windows Service

    tks: https://github.com/danesparza/OWIN-WebAPI-Service add 2015 0717:http://kb.cnblogs.com/page/5092 ...

  3. Windows7下出现“不支持此接口”的解决方案

    今天学校里的辅导员突然找到我说Windows 7下什么文件夹都打不开了,提示“不支持此接口”.怀疑是病毒所致,但运行杀毒软件没有结果.重启也问题依旧. 上网查了之后找到了修复方法: 在命令行中输入fo ...

  4. LightOJ1348 树链剖分

    简单题,看题目就懂. #include<queue> #include<stack> #include<cmath> #include<cstdio> ...

  5. Java原来如此-遍历Map的三种方法

    import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; pub ...

  6. 【bzoj2460】 BeiJing2011—元素

    www.lydsy.com/JudgeOnline/problem.php?id=2460 (题目链接) 题意 n个二元组(a,b),求一个∑b最大,且所有子集XOR<>0的集合 Solu ...

  7. [NOIP2010] 提高组 洛谷P1514 引水入城

    题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个海拔高度. ...

  8. The Honeynet ProjectThe Honeynet Project

    catalogue . 蜜罐基本概念 . Kippo: SSH低交互蜜罐安装.使用 . Dionaea: 低交互式蜜罐框架部署 . Thug . Amun malware honeypots . Gl ...

  9. java订电影票系统

    public class Test { public static void main(String[] args) { BookTicket bookTicket = new BookTicket( ...

  10. aapt aidl

    AIDL:Android Interface Definition Language,即Android接口定义语言 aapt即Android Asset Packaging Tool,在SDK的bui ...