参考文献
四叉树
树套树
以及和zhoufangyuan巨佬的激烈♂讨论
参考文献
大家好我口糊大师又回来了。
给你一个 n ∗ n n*n n∗n矩阵,然后让你支持两种操作,对子矩阵加值和对子矩阵查和。
暴力写法
对于每一行开一个线段树,然后跑,时间复杂度 n 2 l o g n n^2logn n2logn。
优点:
- 代码较短
- 较为灵活
缺点:
- 常数大
- 容易卡
二叉树
我们对于平面如此处理,一层维护横切,一层竖切。
当然,这个做法也是 n 2 l o g n n^2logn n2logn的,卡法就是任意一行的全加值。
优点:
- 时间复杂度比较平均。
缺点:
- 时间复杂度还是这么垃圾。
四叉树
那么不能两两分就四四分!
即把一个区域分成四份。
当然还有这两种特殊情况,不过在代码中并没有特判(貌似这种写法不需要)。
很可惜,一样的卡法,还是那个 n 2 l o g n n^2logn n2logn的味道。
来自参考文献的代码
//神奇的码风,等我哪一天码了一个类似的代码,就把这个换了吧,但是最近没时间。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<cmath>
#define ll long long
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define fur(i,x,y) for(i=x;i<=y;i++)
#define fdr(i,x,y) for(i=x;i>=y;i--)
#define Fur(i,x,y) for(ll i=x;i<=y;i++)
#define Fdr(x,y) for(ll i=x;i>=y;i--)
#define in2(x,y) in(x);in(y)
#define in3(x,y,z) in2(x,y);in(z)
#define in4(a,b,c,d) in2(a,b);in2(c,d)
#define clr(x,y) memset(x,y,sizeof(x))
#define cpy(x,y) memcpy(x,y,sizeof(x))
using namespace std;
/*---------------------------------------*/
namespace fib{
char b[300000]= {
},*f=b;}
#define gc ((*fib::f)?(*(fib ::f++)):(fgets(fib::b,sizeof(fib::b),stdin)?(fib::f=fib::b,*(fib::f++)):-1))
inline void in(ll &x){
x=0;char c;bool f=0;while((c=gc)>'9'||c<'0')if(c=='-')f=!f;x=c-48;while((c=gc)<='9'&&c>='0')x=x*10+c-48;if(f)x=-x;}
namespace fob{
char b[300000]= {
},*f=b,*g=b+300000-2;}
#define pob (fwrite(fob::b,sizeof(char),fob::f-fob::b,stdout),fob::f=fob::b,0)
#define pc(x) (*(fob::f++)=(x),(fob::f==fob::g)?pob:0)
struct foce{
~foce(){
pob;fflush(stdout);}} _foce;
namespace ib{
char b[100];}
inline void out(ll x){
if(x==0){
pc(48);return;}if(x<0){
pc('-');x=-x;}char *s=ib::b;while(x) *(++s)=x%10,x/=10;while(s!=ib::b) pc((*(s--))+48);}
inline void outn(ll x){
out(x);pc('\n');}//快速输出
inline ll jdz(ll x){
return x>0?x:-x;}//绝对值
/*------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------*/
#define N 2334
#define c1 (rt*4-2)//儿子1
#define c2 (rt*4-1)//儿子2
#define c3 (rt*4)//儿子3
#define c4 (rt*4+1)//儿子4
#define z1 x1,y1,mx,my
#define z2 x1,my+1,mx,y2
#define z3 mx+1,y1,x2,my
#define z4 mx+1,my+1,x2,y2
#define Z ll mx=(x1+x2)>>1,my=(y1+y2)>>1//处理mid
#define U ll x1,ll y1,ll x2,ll y2,ll rt
#define pu s[rt]=s[c1]+s[c2]+s[c3]+s[c4]//push_up
ll s[N*N*4],laz[N*N*4