Time Limit: 50 Sec  Memory Limit: 20 MB
Submit: 2185  Solved: 581

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

同Bzoj2683。

嘛,真是简单题啊,才调了两天就过了。

由于强制在线,所以不能像2683那样各种方法乱搞,只能老实写K-Dtree(好像还有块链之类的解法)

K-Dtree定期重构,强行维护数据。常数写不好的话会T飞。

之前把47行的左右边界取错了,时间复杂度直接突破天际。

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#define LL long long
using namespace std;
const int mxn=;
LL read(){
LL x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct node{
int l,r;
int min[],max[];
int d[];
int w;
LL sum;
}t[mxn];
int root=,nowD;
int cmp(const node a,const node b){
return a.d[nowD]<b.d[nowD];
}
int n,cnt;
int lim=;
inline void pushup(int rt,int x){
t[rt].max[]=max(t[rt].max[],t[x].max[]);
t[rt].max[]=max(t[rt].max[],t[x].max[]);
t[rt].min[]=min(t[rt].min[],t[x].min[]);
t[rt].min[]=min(t[rt].min[],t[x].min[]);
return;
}
inline bool in(int x1,int y1,int x2,int y2,int k){
return (x1<=t[k].min[] && t[k].max[]<=x2 &&
y1<=t[k].min[] && t[k].max[]<=y2);
}
inline bool out(int x1,int y1,int x2,int y2,int k){
return (x1>t[k].max[] || x2<t[k].min[] || y1>t[k].max[] || y2<t[k].min[]);
}
int Build(int l,int r,int D){
if(l>r)return ;
nowD=D;int mid=(l+r)>>;
nth_element(t+l,t+mid,t+r+,cmp);///
t[mid].max[]=t[mid].min[]=t[mid].d[];
t[mid].max[]=t[mid].min[]=t[mid].d[];
t[mid].sum=t[mid].w;
//
t[mid].l=Build(l,mid-,D^);
if(t[mid].l)pushup(mid,t[mid].l);
t[mid].r=Build(mid+,r,D^);
if(t[mid].r)pushup(mid,t[mid].r);
//
t[mid].sum=t[mid].w+t[t[mid].l].sum+t[t[mid].r].sum;
return mid;
}
void insert(int &now,int x,int D){
if(!now){now=x;return;}
if(t[x].d[D]==t[now].d[D] && t[x].d[!D]==t[now].d[!D]){
t[now].w+=t[x].w;
t[now].sum+=t[x].w;
--cnt;
return;
}
if(t[x].d[D]<t[now].d[D]){
insert(t[now].l,x,D^);
pushup(now,t[now].l);
}
else{
insert(t[now].r,x,D^);
pushup(now,t[now].r);
}
t[now].sum=t[now].w+t[t[now].l].sum+t[t[now].r].sum;
return;
}
LL query(int rt,int x1,int y1,int x2,int y2){
if(!rt)return ;
LL res=;
if(in(x1,y1,x2,y2,rt)){return t[rt].sum;}
if(out(x1,y1,x2,y2,rt)){return ;}
if(x1<=t[rt].d[] && t[rt].d[]<=x2 &&
y1<=t[rt].d[] && t[rt].d[]<=y2) res+=t[rt].w;
res+=query(t[rt].l,x1,y1,x2,y2)+query(t[rt].r,x1,y1,x2,y2);
return res;
}
int main(){
int i,j,op,x,y,w;
n=read();lim=;
LL lans=;int X1,X2,Y1,Y2;
while(){
op=read();
if(op==)break;
if(op==){
t[++cnt].d[]=read()^lans;t[cnt].d[]=read()^lans;
t[cnt].w=read()^lans;t[cnt].sum=t[cnt].w;
t[cnt].max[]=t[cnt].min[]=t[cnt].d[];
t[cnt].max[]=t[cnt].min[]=t[cnt].d[];
insert(root,cnt,);
if(cnt==lim){
lim+=;
root=Build(,cnt,);
}
}
else{ X1=read()^lans;Y1=read()^lans;X2=read()^lans;Y2=read()^lans;
lans=query(root,X1,Y1,X2,Y2);
printf("%lld\n",lans);
}
}
return ;
}

Bzoj4066 简单题的更多相关文章

  1. [BZOJ2683][BZOJ4066]简单题

    [BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...

  2. bzoj4066: 简单题 K-Dtree

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

  3. BZOJ4066 简单题(KD-Tree)

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

  4. 【kd-tree】bzoj4066 简单题

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

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

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

  6. 【BZOJ4066】简单题(KD-Tree)

    [BZOJ4066]简单题(KD-Tree) 题面 BZOJ 题解 如果这题不卡空间,并且不强制在线的话 显然可以用\(CDQ\)分治做 但是它又卡空间又强制在线,于是我们欢快的来用\(KD-Tree ...

  7. 【BZOJ4066】简单题 KDtree

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

  8. [bzoj4066/2683]简单题_KD-Tree

    简单题 bzoj-4066 题目大意:n*n的棋盘,开始为均为0,支持:单点加权值,查询矩阵权值和,强制在线. 注释:$1\le n\le 5\cdot 10^5$,$1\le m \le 2\cdo ...

  9. BZOJ 2683: 简单题

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

随机推荐

  1. 开发环境python

    python开发环境搭建   虽然网上有很多python开发环境搭建的文章,不过重复造轮子还是要的,记录一下过程,方便自己以后配置,也方便正在学习中的同事配置他们的环境. 1.准备好安装包 1)上py ...

  2. script实现的日期表示

    function clockon(bgclock){ var now=new Date(); var year=now.getYear(); var month=now.getMonth(); var ...

  3. Reverse Words in a String

    void reverseWords(string &s) { string res = "", tmp = ""; int l = s.length() ...

  4. stack overflow错误分析

    stack overflow(堆栈溢出)就是不顾堆栈中分配的局部数据块大小,向该数据块写入了过多的数据,导致数据越界,结果覆盖了老的堆栈数据. 或者解释为 在长字符串中嵌入一段代码,并将过程的返回地址 ...

  5. ionic —— 开发环境搭建并编译运行第一个APP

    其实类似的环境已经玩了很多次了,最开始玩还是微信刚刚出来,那会儿没有智能机.只好安装一个模拟器,却只是为了注册一个微信.想想也就是够了~ 前前后后折腾了很多次,可是每一次都给人不一样的感觉,也许是这个 ...

  6. No goals have been specified for this build

    在pom.xml文件中build后面加上<defaultGoal>compile</defaultGoal>

  7. 关于#pragma once和#ifndefine组合的区别

    最近在看duilib代码,发现头文件既有#pragma once 又有 #ifndefine...#define,忽然就觉得有点不解,因为据我所知这两者都是防止头文件二次包含的. 经过下面两位的解释后 ...

  8. Dubbo_创建Dubbo服务并在ZooKeeper注册,然后通过Jar包执行

    一.安装ZooKeeper(略) 二.创建Dubbo服务  1.DemoService 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  9. 二、处理MVC多级目录问题——以ABP为基础架构的一个中等规模的OA开发日志

    就个人感觉而言.ASP.NET MVC是一种非常反人类的设计.(我没有接触过Java的MVC,不知道两者是否一样.如果一样,那么搞Java的同学也挺可怜.)尤其是MVC的路由机制,灰常灰常反动.路由所 ...

  10. mnsday1t1

    贪心地选取两个后缀,然后往前补全,贪心地补全前k个不同的字符 我写了个沙茶dp,结果T掉了,明明都是n3的... #include<iostream> #include<stdio. ...