1. 程式人生 > >HDU 5454 Excited Database (2015年瀋陽賽區網路賽E題)

HDU 5454 Excited Database (2015年瀋陽賽區網路賽E題)

2.解題思路:本題利用線段樹解決,根據題意,我們需要建立兩棵線段樹,分別維護主對角線,副對角線。每個線段樹的結點需要維護sum,sumL,sumR,其中,sum表示當前區間元素的和,sumL表示[L,R]這段區間中三角形和(第L層從1開始算)的值,sumR類似於sumL。查詢時候把矩形看做一個平行四邊形加上上下兩個三角形來分3次查詢。這樣,本題即可在O(logN)時間內查詢完畢。

3.程式碼:

#include<iostream>
#include<algorithm>
#include<cassert>
#include<string>
#include<sstream>
#include<set>
#include<bitset>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cctype>
#include<complex>
#include<functional>
#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<int,int> 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);
            }
        }
    }
}