【BZOJ4066】简单题

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范围内并且解码之后数据仍合法。

题解:想了一上午KDtree为什么不能旋转,发现显然不能啊~(但听说能搞成替罪的?蒟蒻表示不懂~)

依旧是改一改估价函数就行了,这题需要每加入一些点就暴力重构整棵树

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define rep for(int i=0;i<=1;i++)
using namespace std;
struct kd
{
int sm[2],sn[2],v[2],sum,s,ls,rs;
kd (int a,int b,int c) {ls=rs=0,sm[0]=sn[0]=v[0]=a,sm[1]=sn[1]=v[1]=b,s=sum=c;}
kd (){}
};
kd t[200010],p[200010];
int n,m,A,B,C,D,root,ans;
bool cmp(kd a,kd b)
{
return (a.v[D]==b.v[D])?(a.v[D^1]<b.v[D^1]):(a.v[D]<b.v[D]);
}
void pushup(int x,int y)
{
rep t[x].sm[i]=max(t[x].sm[i],t[y].sm[i]),t[x].sn[i]=min(t[x].sn[i],t[y].sn[i]);
t[x].sum+=t[y].sum;
}
int build(int l,int r,int d)
{
if(l>r) return 0;
int mid=l+r>>1;
D=d;
nth_element(p+l,p+mid,p+r+1,cmp);
t[mid]=p[mid];
t[mid].ls=build(l,mid-1,d^1),t[mid].rs=build(mid+1,r,d^1);
if(t[mid].ls) pushup(mid,t[mid].ls);
if(t[mid].rs) pushup(mid,t[mid].rs);
return mid;
}
void ins(int y)
{
int x=root,d=0;
while(x!=y)
{
pushup(x,y);
if(t[y].v[0]==t[x].v[0]&&t[y].v[1]==t[x].v[1])
{
t[x].s+=t[y].s,n--;
return ;
}
if(t[y].v[d]<t[x].v[d]) t[x].ls=(!t[x].ls)?y:t[x].ls,x=t[x].ls;
else t[x].rs=(!t[x].rs)?y:t[x].rs,x=t[x].rs;
d^=1;
}
}
int query(int x)
{
if(!x||t[x].sm[0]<A||t[x].sn[0]>C||t[x].sm[1]<B||t[x].sn[1]>D) return 0;
if(t[x].sm[0]<=C&&t[x].sn[0]>=A&&t[x].sm[1]<=D&&t[x].sn[1]>=B) return t[x].sum;
int ret=0;
if(t[x].v[0]>=A&&t[x].v[0]<=C&&t[x].v[1]>=B&&t[x].v[1]<=D) ret+=t[x].s;
ret+=query(t[x].ls)+query(t[x].rs);
return ret;
}
int rd()
{
int ret=0; char gc=getchar();
while(gc<'0'||gc>'9') gc=getchar();
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret;
}
int main()
{
rd();
int i,j,e;
root=1,m=10000;
while(1)
{
e=rd();
if(e==3) break;
if(e==1)
{
A=rd()^ans,B=rd()^ans,C=rd()^ans;
t[++n]=kd(A,B,C),ins(n);
if(n==m)
{
for(i=1;i<=n;i++) p[i]=kd(t[i].v[0],t[i].v[1],t[i].s);
root=build(1,n,0),m+=10000;
}
}
else
{
A=rd()^ans,B=rd()^ans,C=rd()^ans,D=rd()^ans;
ans=query(root);
printf("%d\n",ans);
}
}
return 0;
}

【BZOJ4066】简单题 KDtree的更多相关文章

  1. bzoj4066: 简单题 K-Dtree

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

  2. [BZOJ2683][BZOJ4066]简单题

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

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

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

  4. Bzoj4066 简单题

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

  5. bzoj 4066: 简单题 kd-tree

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

  6. BZOJ4066 简单题(KD-Tree)

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

  7. 【kd-tree】bzoj4066 简单题

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

  8. 【bzoj4066】简单题 KD-tree

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

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

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

随机推荐

  1. Appium Python 一:环境搭建

    安装Android SDK以及模拟器 由于Appium依赖于Android SDK,所以需要先安装SDK. 这里由于需要在Android模拟器上跑测试用例,所以同时需要安装Android 模拟器. 1 ...

  2. 微信小程序 - 文字走马灯

    转载于csdn maid_04,总之多谢了!节省了不少时间呢 最近在做一个类似uu跑腿的项目,时间也特别紧,搞完以后再继续贴地图代码(高德.腾讯) 以下代码拷贝即可用,拿走谢谢上面的人吧(~.~) w ...

  3. 火狐 http://localhost:8080自动跳转到http://www.localhost.com:8080

    用火狐调试PHP时 偶尔会出现连接被重置的情况:http://localhost:8080自动跳转到http://www.localhost.com:8080 解决方案1: 打开网络-属性,往下拉找到 ...

  4. Python学习笔记(三)多线程的使用

    这节记录学习多线程的心得.   Python提供了thread模块,不过该模块的缺点很多,例如无法方便的等待线程结束,所以我们使用更加高级的threading模块.   threading模块的使用一 ...

  5. EffectManager

    using UnityEngine; using System.Collections; public class EffectManager : MonoBehaviour { public Ani ...

  6. react-native 模仿原生 实现下拉刷新/上拉加载更多(RefreshListView)

    1.下拉刷新/上拉加载更多 组件(RefreshListView) src/components/RefreshListView/index.js /** * 下拉刷新/上拉加载更多 组件(Refre ...

  7. javascript入门系列演示·三种弹出对话框的用法实例

    对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...

  8. javascript获取日期的年,月,日

    var date = new Date(strTime); return date.getFullYear()+"-"+(date.getMonth()+1)+"-&qu ...

  9. js - 模块化开发的兼容exports的套路

    补充:除了第一种的套路,还可以这样使用第二种.都是用来自执行函数的.第二种的好处是,还可以返回一个true. // 常用 ;(function () {})(); // 小技巧(如果不加上!会报错,加 ...

  10. java - day15 - NonameInner

    匿名内部类 public interface Inter {} interface Inter2{ void show(); } main(){ //错误,接口不能实例化 Inter t = new ...