bzoj1513 Tet-Tetris 3D(二維線段樹)
時間限制:1秒 記憶體限制:64M
【問題描述】
Tetris 3D “Tetris” 遊戲的作者決定做一個新的遊戲, 一個三維的版本。 在裡面很多立方體落在平面板,一個立方體開始落下直到碰上一個以前落下的立方體或者落地即停止。作者想改變一下游戲的目的使得它更大眾化,在新遊戲中你將知道落下的立方體資訊以及位置。
你的任務就是回答所有立方體落下後最高的方塊的高度。所有的立方體在下落過程中都是垂直的並且不會旋轉。平板左下角座標為原點,並且平行於座標軸。
【輸入格式】
第一行給出三個整數 D, S and N , 分別表示平板的長和寬以及下落立方體的數目。
接下來N 行每行描述一個立方體. 每行包含5個整數: d, s, w, x and y (1<= d, 0 <=x, d + x<= D, 1 <=s, 0<= y, s + y<= S, 1<= w <=100 000), 分別表示立方體的長\寬\高以及落下的左下角座標, 長和寬都是平行於平板座標軸的,落下後立方體著地的四個角座標分別為: (x, y), (x + d, y), (x, y + s) and (x + d, y + s).
【輸出格式】
一個整數表示所有立方體落下後最高的方塊的高度.
【輸入樣例】
7 5 4
4 3 2 0 0
3 3 1 3 0
7 1 2 0 3
2 3 3 2 2
【輸出樣例】
6
【資料範圍】
1<= N<= 20 000, 1<= D, S <=1 000)
【來源】
bzoj1513
一道二維線段樹的題,轉化一下題意,就是每次求一個二維空間範圍內的最大值x,然後在把這個範圍內的值變成x+h.
我們先建立一個x軸的線段樹,然後再在每個點建立一個y軸的線段樹,再維護就好。
打了一個很醜的程式碼,將就看看吧。
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1005;
int lc[maxn*2],rc[maxn*2],root[maxn*2],root2,cnt=0;
int lc2[maxn*maxn*4],rc2[maxn*maxn*4],cnt2=0,maxv[maxn*maxn*4]={0};
int n,D,S;
int maxv2[maxn*maxn*4]={0},inq2[maxn*maxn *4]={0},inq[maxn*maxn*4]={0};
void in2(int now,int l,int r,int i,int j,int x){
maxv[now]=max(x,maxv[now]);
if(l>=i&&r<=j) {inq[now]=max(inq[now],x);return;}
int m=(l+r)>>1;
if(i<=m)in2(lc2[now],l,m,i,j,x);
if(j>m)in2(rc2[now],m+1,r,i,j,x);
}
void in3(int now,int l,int r,int i,int j,int x){
maxv2[now]=max(x,maxv2[now]);
if(l>=i&&r<=j) {inq2[now]=max(inq2[now],x);return;}
int m=(l+r)>>1;
if(i<=m)in3(lc2[now],l,m,i,j,x);
if(j>m)in3(rc2[now],m+1,r,i,j,x);
}
void in1(int now,int l,int r,int i,int j,int x,int y,int w){
in3(root[now],1,S,x,y,w);
if(l>=i&&r<=j) {in2(root[now],1,S,x,y,w);return;}
if(l==r) return;
int m=(l+r)>>1;
if(i<=m)in1(lc2[now],l,m,i,j,x,y,w);
if(j>m)in1(rc2[now],m+1,r,i,j,x,y,w);
}
void rebuid(int &now,int l,int r){
now=++cnt2;
if(l==r) return;
int m=(l+r)>>1;
rebuid(lc2[now],l,m);
rebuid(rc2[now],m+1,r);
}
void buid(int &now,int l,int r){
now=++cnt;
rebuid(root[now],1,S);
if(l==r) return;
int m=(l+r)>>1;
buid(lc[now],l,m);
buid(rc[now],m+1,r);
}
int find2(int now,int l,int r,int i,int j){
if(l>=i&&r<=j) return maxv[now];
int m=(l+r)>>1;
int t1=0,t2=0,t3=inq[now];
if(i<=m) t1=find2(lc2[now],l,m,i,j);
if(j>m) t2=find2(rc2[now],m+1,r,i,j);
return max(t3,max(t1,t2));
}
int find3(int now,int l,int r,int i,int j){
if(l>=i&&r<=j) return maxv2[now];
int m=(l+r)>>1;
int t1=0,t2=0,t3=inq2[now];
if(i<=m) t1=find3(lc2[now],l,m,i,j);
if(j>m) t2=find3(rc2[now],m+1,r,i,j);
return max(t3,max(t1,t2));
}
int find(int now,int l,int r,int i,int j,int x,int y){
if(l>=i&&r<=j) return find3(root[now],1,S,x,y);
int m=(l+r)>>1;
int t1=0,t2=0,t3=find2(root[now],1,S,x,y);
if(i<=m) t1=find(lc[now],l,m,i,j,x,y);
if(j>m) t2=find(rc[now],m+1,r,i,j,x,y);
return max(t3,max(t1,t2));
}
int read(){
int x=0;
char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch<='9'&&ch>='0')
{
x=x*10+ch-'0';
ch=getchar();
}
return x;
}
int main()
{
//freopen("tetris3d.in","r",stdin);
//freopen("tetris3d.out","w",stdout);
D=read()+1,S=read()+1; n=read();
buid(root2,1,D);
int d,s,ans=0,h,x,y,t;
for(int i=1;i<=n;i++)
{
d=read();s=read();h=read();x=read()+1;y=read()+1;
t=find(root2,1,D,x,x+d-1,y,y+s-1);
t+=h;
in1(root2,1,D,x,x+d-1,y,y+s-1,t);
ans=max(ans,t);
}
cout<<ans;
return 0;
}