ZOJ 3299 线段树

//题意:有n排板砖,m个木板,边界的l和r的n列板砖从天上掉下来,然后有m个边界的a,b的高度为h的木板去接那些板砖,一排板砖中的部分板砖如果掉到木板上就停止下落,
//剩下的继续下落,问最后每块木板上有多少块板砖。
//
//开结构体去存储线段树中节点的信息MLE了。。
//
//按木板的高度,从低到高,去更新线段树节点对应的木板的编号。
//
//板砖落下时,将对应区间的覆盖次数加一。
//
//最后查询的时候,如果已经是叶子结点,或者这个区间的对应的木板的编号是确定的,那么就要在对应的木板编号上加上,覆盖次数乘以区间长度的值。
//
////all is for 普吉岛*******************************************************************************************
//
//就是我要好好的敲一下离散化
//
//先是更新一遍,高的模板覆盖低的模板,这样的话就变成一条木板了;
//然后再用木块覆盖上去,更新每一段的覆盖次数;
//最后查询每一块的覆盖次数*区间长度。。
//首先离散化。。
//
//想到最好要养成代码写完就能一遍过的情况,不要每次都在那里等调试。
//还有一个很重要的问题,做题的速度问题!!!
//一开始为什么会mle?过了
//传参?map?vector?re?

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string.h>
#define maxn 100010
#include<vector>
#include<map>
#define ll long long
using namespace std;
#define maxm 1600100
#define rep(i,n) for(int i=0;i<n;++i)
map<int,int>H;
vector<int> Y;


//where is the problem
struct node{
int sum,l,r,col;
}tree[maxm];
long long ans[maxn];
int LL[401000];
void build(int root ,int left,int right)
{
//    cout<<left<<" "<<right<<" "<<root<<endl;
    tree[root].col=-1;
    tree[root].sum=0;
    tree[root].l=left;
    tree[root].r=right;
    if(left==right-1) return ;
    else{
    int mid=(left+right)>>1;
    build(root<<1,left,mid);
    build(root<<1|1,mid,right);}
}


void updatec(int root,int s,int t,int cl)
{
//    cout<<root<<" "<<s<<" "<<t<<endl;
    int left=tree[root].l;
    int right=tree[root].r;
//    cout<<LL[left]<<" "<<LL[right]<<" "<<s<<" "<<t<<endl;
    if(LL[left]>=s&& LL[right]<=t) {tree[root].col=cl;return;}
//    cout<<LL[left]<<" "<<LL[right]<<" "<<cl<<endl;return;}
    if(left+1==right) return;
    if(tree[root].col != -1)
    {
    tree[root<<1|1].col=tree[root].col;
    tree[root<<1].col=tree[root].col;
    tree[root].col=-1;
    }
    int mid=(left+right)>>1;
    if(s<=LL[mid]) updatec(root<<1,s,t,cl);
    if(t>=LL[mid]) updatec(root<<1|1,s,t,cl);
}


void updateS(int root,int s,int t)
{
//    cout<<root<<" "<<s<<" "<<t<<endl;
    int left=tree[root].l;
    int right=tree[root].r;
    if(LL[left]>=s &&LL[right]<=t)
    {
        tree[root].sum++;
        return;
    }
    if(left+1==right) return;

    if(tree[root].sum>0)
    {
       tree[root<<1].sum+=tree[root].sum;
        tree[root<<1|1].sum+=tree[root].sum;
        tree[root].sum=0;
    }
    int mid=(left+right)>>1;
    if(s<=LL[mid]) updateS(root<<1,s,t);
    if(t>=LL[mid]) updateS(root<<1|1,s,t);

}

void query(int root)
{
//    cout<<root<<endl;
    int left=tree[root].l;
    int right=tree[root].r;
    if(left+1 == right){
		if(tree[root].col != -1) ans[tree[root].col] += (long long)(LL[tree[root].r] - LL[tree[root].l]) * (long long )tree[root].sum;
		return ;
	}
    if(tree[root].sum > 0){
		tree[root<<1].sum += tree[root].sum;
		tree[root<<1|1].sum += tree[root].sum;
		tree[root].sum = 0;
	}
	if(tree[root].col != -1){
		tree[root<<1].col= tree[root<<1|1].col = tree[root].col;
	}
	int mid = (left + right) >> 1;
	query(root<<1);
	query(root<<1|1);
}

void print(int root)
{
//    cout<<tree[root].l<<" "<<tree[root].r<<" "<<tree[root].sum<<" "<<tree[root].col<<endl;
    if(tree[root].l==tree[root].r-1) return;
    print(root<<1);
    print(root<<1|1);
}
struct Node
{
    int l,r,h,index;
}boa[maxn];

int l[maxn];
int r[maxn];

bool cmp(Node a,Node b)
{
    return a.h<b.h;
}


int pos;
struct Plane{
    int x,y;
}plane[maxn];

struct Block{
    int x,y,z;
	int idx;
}block[maxn];
bool cmp1(Block a,Block b){
    return a.z < b.z;
}

int main() {
//	freopen("in.txt","r",stdin);
	int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        pos = 0;
        rep(i,n){
            scanf("%lld%lld",&plane[i].x,&plane[i].y);
            LL[pos++] = plane[i].x;
            LL[pos++] = plane[i].y;
        }
        rep(i,m){
            scanf("%d%d%d",&block[i].x,&block[i].y,&block[i].z);
            block[i].idx = i+1;
           LL[pos++] = block[i].x;
            LL[pos++] = block[i].y;
        }
        sort(block,block+m,cmp1);
        sort(LL,LL+pos);
        int len = unique(LL,LL+pos)-LL-1;
        build(1,0,len);
        rep(i,m){
            updatec(1,block[i].x,block[i].y,block[i].idx);
        }
        memset(ans,0,sizeof(ans));
        rep(i,n){
            updateS(1,plane[i].x,plane[i].y);
        }
        query(1);
//        print(1);
        for(int i=1;i<=m;++i){
         	printf("%lld\n",ans[i]);
        }
        printf("\n");
    }
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值