HDU 5454 Excited Database (2015年沈阳赛区网络赛E题)

1.题目描述:点击打开链接

2.解题思路:本题利用线段树解决,根据题意,我们需要建立两棵线段树,分别维护主对角线,副对角线。每个线段树的结点需要维护sum,sumL,sumR,其中,sum表示当前区间元素的和,sumL表示[L,R]这段区间中三角形和(第L层从1开始算)的值,sumR类似于sumL。查询时候把矩形看做一个平行四边形加上上下两个三角形来分3次查询。这样,本题即可在O(logN)时间内查询完毕。

3.代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)
#define me(s) memset(s,0,sizeof(s))
#define pb push_back
#define lid (id<<1)
#define rid (id<<1|1)
typedef long long ll;
typedef unsigned long long ull;
typedef pair P;

const int N=400000+5;
struct Node
{
    int l,r;
    ll sum;
    ll sumL,sumR;
    ll lazy;
    int length(){return r-l+1;}
    int mid(){return l+(r-l)/2;}
    void maintain(ll a)
    {
        lazy+=a;
        sum+=a*length();
        ll add=a*(length()+1)*length()/2;
        sumL+=add;
        sumR+=add;
    }
};

Node A[N<<2],D[N<<2];

void pushup(Node*t,int id)
{
    t[id].sum=t[lid].sum+t[rid].sum;
    t[id].sumL=t[lid].sumL+t[rid].sumL+t[rid].sum*t[lid].length(); //大三角形=左儿子三角形+右儿子三角形+平行四边形
    t[id].sumR=t[lid].sumR+t[rid].sumR+t[lid].sum*t[rid].length();
}

void pushdown(Node*t,int id)
{
    if(t[id].lazy)
    {
        t[lid].maintain(t[id].lazy);
        t[rid].maintain(t[id].lazy);
        t[id].lazy=0;
    }
}

void build(Node*t,int id,int L,int R)
{
    t[id].l=L,t[id].r=R;
    t[id].sum=t[id].sumL=t[id].sumR=t[id].lazy=0;
    if(L==R)return;
    int mid=t[id].mid();
    build(t,lid,L,mid);
    build(t,rid,mid+1,R);
    pushup(t,id);
}

void update(Node*t,int id,int L,int R,int v)
{
    int l=t[id].l,r=t[id].r;
    if(L<=l&&r<=R)
    {
        t[id].maintain(v);
        return;
    }
    pushdown(t,id);
    int mid=t[id].mid();
    if(L<=mid)update(t,lid,L,R,v);
    if(R>mid)update(t,rid,L,R,v);
    pushup(t,id);
}

ll query(Node*t,int id,int L,int R)
{
    int l=t[id].l,r=t[id].r;
    if(L<=l&&r<=R) return t[id].sum;
    pushdown(t,id);
    int mid=t[id].mid();
    ll res=0;
    if(L<=mid)res+=query(t,lid,L,R);
    if(R>mid)res+=query(t,rid,L,R);
    pushup(t,id);
    return res;
}

ll queryLeft(Node*t,int id,int L,int R)
{
    int l=t[id].l,r=t[id].r;
    if(L<=l&&r<=R)
        return t[id].sumL+t[id].sum*(l-L); //小三角形+平行四边形(平行四边形可能不存在)
    pushdown(t,id);
    int mid=t[id].mid();
    ll res=0;
    if(L<=mid)res+=queryLeft(t,lid,L,R);
    if(R>mid)res+=queryLeft(t,rid,L,R);
    pushup(t,id);
    return res;
}

ll queryRight(Node*t,int id,int L,int R)
{
    int l=t[id].l,r=t[id].r;
    if(L<=l&&r<=R)
        return t[id].sumR+t[id].sum*(R-r);
    pushdown(t,id);
    int mid=t[id].mid();
    ll res=0;
    if(L<=mid)res+=queryRight(t,lid,L,R);
    if(R>mid)res+=queryRight(t,rid,L,R);
    pushup(t,id);
    return res;
}

int n,q;

int main()
{
    int T;
    int rnd=0;
    scanf("%d",&T);
    while(T--)
    {
        printf("Case #%d:\n",++rnd);
        scanf("%d%d",&n,&q);
        build(A,1,2,2*n);
        build(D,1,1,2*n-1);
        int cmd;
        while(q--)
        {
            scanf("%d",&cmd);
            int L,R;
            int x1,x2,y1,y2;
            if(cmd==1)
            {
                scanf("%d%d",&L,&R);
                update(A,1,L,R,1);
            }
            if(cmd==2)
            {
                scanf("%d%d",&L,&R);
                update(D,1,L+n,R+n,1);
            }
            if(cmd==3)
            {
                scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
                ll ans=0;
                ll num=min(x2-x1,y2-y1)+1;

                int l=x2+y1,r=x1+y2;
                if(l>r)swap(l,r);
                ans+=query(A,1,l,r)*num;
                ans+=queryLeft(A,1,x1+y1,l-1);
                ans+=queryRight(A,1,r+1,x2+y2);

                l=x1-y1+n,r=x2-y2+n;
                if(l>r)swap(l,r);
                ans+=query(D,1,l,r)*num;
                ans+=queryLeft(D,1,x1-y2+n,l-1);
                ans+=queryRight(D,1,r+1,x2-y1+n);
                printf("%lld\n",ans);
            }
        }
    }
}


你可能感兴趣的:(ACM/ICPC区域赛真题,数据结构——线段树)