裸题不多说,注意在sqrt(n*log(n))次插入后重构树以保持深度。

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 170011
#define KD 2//ά¶ÈÊý
int qp[2][2];
int n,root=1,m;
int Begin;
bool dn;
struct Node
{
int minn[KD],maxx[KD],p[KD],ch[2];
int sumv,w;
void Init()
{
for(int i=0;i<KD;++i)
minn[i]=maxx[i]=p[i];
sumv=w;
}
}T[N];
inline void Clear()
{
for(int i=1;i<=n;++i)
T[i].ch[0]=T[i].ch[1]=0;
}
inline void Update(int rt)
{
T[rt].sumv=T[rt].w;
for(int i=0;i<2;++i)
if(T[rt].ch[i])
{
T[rt].sumv+=T[T[rt].ch[i]].sumv;
for(int j=0;j<KD;++j)
{
T[rt].minn[j]=min(T[rt].minn[j],T[T[rt].ch[i]].minn[j]);
T[rt].maxx[j]=max(T[rt].maxx[j],T[T[rt].ch[i]].maxx[j]);
}
}
}
bool operator < (const Node &a,const Node &b){return a.p[dn]<b.p[dn];}
inline int Buildtree(int l=1,int r=n,bool d=0)
{
dn=d;
int m=(l+r>>1);
nth_element(T+l,T+m,T+r+1);
T[m].Init();
if(l!=m) T[m].ch[0]=Buildtree(l,m-1,d^1);
if(m!=r) T[m].ch[1]=Buildtree(m+1,r,d^1);
Update(m);
return m;
}
inline void Insert(int rt=root,bool d=0)
{
bool f=(T[n].p[d]>T[rt].p[d]);
if(T[rt].ch[f]) Insert(T[rt].ch[f],d^1);
else T[rt].ch[f]=n;
Update(rt);
}
int ans;
void Query(int rt=root)
{
if(T[rt].p[0] >= qp[0][0] && T[rt].p[0] <= qp[1][0]
&& T[rt].p[1] >= qp[0][1] && T[rt].p[1] <= qp[1][1])
ans+=T[rt].w;
for(int i=0;i<2;++i)
if(T[rt].ch[i]
&& T[T[rt].ch[i]].maxx[0] >= qp[0][0] && T[T[rt].ch[i]].minn[0] <= qp[1][0]
&& T[T[rt].ch[i]].maxx[1] >= qp[0][1] && T[T[rt].ch[i]].minn[1] <= qp[1][1])
{
if(T[T[rt].ch[i]].minn[0] >= qp[0][0] && T[T[rt].ch[i]].maxx[0] <= qp[1][0]
&& T[T[rt].ch[i]].minn[1] >= qp[0][1] && T[T[rt].ch[i]].maxx[1] <= qp[1][1])
ans+=T[T[rt].ch[i]].sumv;
else
Query(T[rt].ch[i]);
}
}
int op[N],X1[N],Y1[N],X2[N],Y2[N];
int Vs[N];
int main()
{
// freopen("data7.in","r",stdin);
// freopen("bzoj4066.out","w",stdout);
scanf("%d%d",&Begin,&m); m=0;
while(1)
{
++m;
scanf("%d",&op[m]);
if(op[m]==3)
{
--m;
break;
}
if(op[m]==1)
{
scanf("%d%d%d",&X1[m],&Y1[m],&Vs[m]);
++n;
}
else
scanf("%d%d%d%d",&X1[m],&Y1[m],&X2[m],&Y2[m]);
}
int blo=(int)sqrt((double)n*log2((double)n));
n=0;
for(int i=1;i<=m;++i)
{
if(op[i]==1)
{
++n;
T[n].p[0]=X1[i];
T[n].p[1]=Y1[i];
T[n].w=Vs[i];
T[n].Init();
if(n>1)
Insert();
if(blo==1 || blo==0 || n%blo==0)
{
Clear();
Buildtree();
root=(1+n>>1);
}
}
else
{
qp[0][0]=X1[i];
qp[0][1]=Y1[i];
qp[1][0]=X2[i];
qp[1][1]=Y2[i];
ans=0;
if(n)
Query();
printf("%d\n",ans+(qp[1][1]-qp[1][0]+1)*(qp[0][1]-qp[0][0]+1)*Begin);
}
}
return 0;
}

【kd-tree】bzoj1176 [Balkan2007]Mokia的更多相关文章

  1. [BZOJ1176][Balkan2007]Mokia cdq+树状数组

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 3134  Solved: 1395[Submit][S ...

  2. BZOJ1176 [Balkan2007]Mokia 【CDQ分治】

    题目 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000. 输入格式 ...

  3. bzoj1176: [Balkan2007]Mokia【cdq分治】

    把询问搞成4个,cdq分治. #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a;i <= b; i++) #d ...

  4. 【BZOJ】1176: [Balkan2007]Mokia

    [题意]n*n的矩阵,初始值为0(题面有误),m次操作,增加一个格子的权值,或查询子矩阵和.n<=2*10^6.(m应该较题面所述偏大). [算法]CDQ分治(算法知识见数据结构) [题解]三维 ...

  5. 【BZOJ】1176: [Balkan2007]Mokia(cdq分治)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1176 在写这题的时候思维非常逗啊........2333................... 最后 ...

  6. 【Symmetric Tree】cpp

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

  7. 【Same Tree】cpp

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  8. 【LSGDOJ1834 Tree】树链剖分

    题目描述 给定一个N个结点的无向树,树中的结点按照1...N编号,树中的边按照1...N − 1编号,每条边都赋予一个权值.你需要编写程序支持以下三种操作: 1.    CHANGE i v:将i号边 ...

  9. 【BZOJ4154】Generating Synergy【kd树】

    题意 给定一棵以1为根的有根树,初始所有节点颜色为1,每次将距离节点a不超过l的a的子节点染成c,或询问点a的颜色 分析 我们以dfs序为横坐标,深度为纵坐标,建kd树.我们每次更新,都是在kd树中更 ...

随机推荐

  1. WordPress后台edit-tags.php里无限栏目分类实现

    在 WordPress 里 http://localhost/wordpress3.6.1/wp-admin/edit-tags.php?taxonomy=category 这个链接可以显示 WP 里 ...

  2. 近期对于windows服务的理解

    1.APP.config的作用   在开发环境下时,根目录下的APP.config里面会填写一些参数之类的.当生成之后,这些参数将会被自动生成在*.exe文件目录中.如图: 其中,.exe文件为Win ...

  3. [hdu 2586]lca模板题(在线+离线两种版本)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 在线版本: 在线方法的思路很简单,就是倍增.一遍dfs得到每个节点的父亲,以及每个点的深度.然后 ...

  4. git学习,哇瑟说实话我想要的

    1.Git 简介及安装Git是目前世界上最先进的分布式版本控制系统(没有之一).它的诞生也颇具传奇,Linux创始人Linus花了两周时间自己用C写了一个分布式版本控制系统,这就是Git!有兴趣的话, ...

  5. ubuntu12.04 Qt WebKit编译

    转载自:http://my.oschina.net/u/257674/blog/167050 官方文档: http://trac.webkit.org/wiki/BuildingQtOnLinux#D ...

  6. es6+最佳入门实践(10)

    10.Generator 10.1.Generator是什么? Generator函数是ES6提供的一种异步编程解决方案.在它的内部封装了多个状态,因此,又可以理解为一种状态机,执行Generator ...

  7. HDU 1840 Equations (数学)

    title: Equations 数学 杭电1840 tags: [数学] 题目链接 Problem Description All the problems in this contest tota ...

  8. xampp命令

    XAMPP命令安装 XAMPPtar xvfz xampp-linux-1.6.4.tar.gz -C /opt启动 XAMPP/opt/lampp/lampp start停止 XAMPP/opt/l ...

  9. 破解wifi时遇到rtl8187 - [phy1]SIOCSIFFLAGS: Name not unique on network

    当我使用我的ubuntu利用aircrack-ng套件进行wifi破解时 遇到如下问题 rtl8187 - [phy1]SIOCSIFFLAGS: Name not unique on network ...

  10. POJ2495(棋盘分治,染色)

    Incomplete chess boards Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2483   Accepted ...