hdu 4533(树状数组区间更新+单点查询)

题意:给你N个矩形,每个矩形给出左下角坐标,右上角坐标,有M个询问,每个询问给出一个时间t,问(0,0),(t,t)的范围内矩形的面积和(重叠的也算)。

参考博客:http://blog.csdn.net/shiqi_614/article/details/8869676

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=200005;
typedef long long LL;
struct BIT
{
    LL T[N];
    int lowbit(int x) {return x&(-x);}
    void clear(){memset(T,0,sizeof(T));}
    void add(int st,int ed,LL valu)
    {
        for(int i=st;i<N;i+=lowbit(i)) T[i]+=valu;
        for(int i=ed+1;i<N;i+=lowbit(i)) T[i]-=valu;
    }
    LL query(int pos)
    {
        LL sum=0;
        for(int i=pos;i>0;i-=lowbit(i)) sum+=T[i];
        return sum;
    }
}A,B,C;
void fun(int st,int ed,LL a,LL b,LL c)
{
    A.add(st,ed,a);
    B.add(st,ed,b);
    C.add(st,ed,c);
}
int main()
{

    int t;
    scanf("%d",&t);
    while(t--)
    {
        A.clear(); B.clear(); C.clear();

        int n,m;
        scanf("%d",&n);

        for(int i=0;i<n;i++)
        {
            LL x1,y1,x2,y2;
            scanf("%I64d%I64d%I64d%I64d",&x1,&y1,&x2,&y2);

            if(max(x1,y1)<min(x2,y2))
                fun(max(x1,y1),min(x2,y2),1,-(x1+y1),x1*y1);

            if(x2<y2) fun(max(x2,y1)+1,y2,0,-x1+x2,y1*(x1-x2));
            if(y2<x2) fun(max(y2,x1)+1,x2,0,-y1+y2,x1*(y1-y2));

            fun(max(x2,y2)+1,N,0,0,(x2-x1)*(y2-y1));
        }

        scanf("%d",&m);
        while(m--)
        {
            LL t;
            scanf("%I64d",&t);


            LL ans=0;
            ans+=A.query(t)*t*t;
            ans+=B.query(t)*t;
            ans+=C.query(t);

            printf("%I64d\n",ans);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值