HDU 5454 Excited Database【线段树】

简单线段树,就是推公式的过程有点不是特别熟练。
按照叉姐的方法,可以用三个树状数组维护,三种情况。
按照坦克工程师的方法,用容斥,将一个矩形分解成三个三角形,一个大的直角三角形减去两个小的直角三角形,一种情况,复杂度 O(n(logn)2)
我的写法无非就是将叉姐的方法换成两个线段树。

我们先将对角线缩成一个点,所以总共有 2n 条主对角线, 2n 条负对角线,所以是 4n×2 的空间复杂度。
将矩阵拆成三个部分,一个三角形一个平行四边形一个三角形。
那么对于三角形的情况,我们对于每一个条对角线,覆盖的点数是一个等差数列。一个递增一个递减。
那么我们就维护一个 a[i] ia[i] a[i] 表示第i条对角线被update的次数。
然后递增等差数列是 RLia[i](L1)RLa[i] 就是其覆盖的值。
递减等差数列是 (R+1)RLa[i]RLia[i]
中间的平行四边形覆盖的点数是常数,所以 a[i]
假设矩阵的宽为 P ,长为Q,那么三角形的区间长度就为 min(P,Q) ,还要特判一下 P==Q 情况,这时候两个三角形的区间长度是相等的。那么有中间部分的情况是 max(P,Q)>min(P,Q)+1 ,平行四边形的长度为 min(P,Q)+1

//      whn6325689
//      Mr.Phoebe
//      http://blog.csdn.net/u013007900
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")


using namespace std;

#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62
#define speed std::ios::sync_with_stdio(false);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;

#define CLR(x,y) memset(x,y,sizeof(x))
#define CPY(x,y) memcpy(x,y,sizeof(x))
#define clr(a,x,size) memset(a,x,sizeof(a[0])*(size))
#define cpy(a,x,size) memcpy(a,x,sizeof(a[0])*(size))
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }

#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))

#define MID(x,y) (x+((y-x)>>1))
#define getidx(l,r) (l+r | l!=r)
#define ls getidx(l,mid)
#define rs getidx(mid+1,r)
#define lson l,mid
#define rson mid+1,r

template<class T>
inline bool read(T &n)
{
    T x = 0, tmp = 1;
    char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template <class T>
inline void write(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
//-----------------------------------

const int MAXN=200000;

int n,q;

struct Node
{
    ll sum,pro,la;
    void init()
    {
        sum=pro=la=0;
    }
} t[2][MAXN<<2];

void build(int l,int r,int i)
{
    int idx=getidx(l,r);
    t[i][idx].init();
    if(l==r)    return;
    int mid=MID(l,r);
    build(l,mid,i);
    build(mid+1,r,i);
}

void pushdown(int l,int r,int i)
{
    int idx=getidx(l,r),mid=MID(l,r);
    if(!t[i][idx].la)  return;
    t[i][ls].la+=t[i][idx].la;
    t[i][rs].la+=t[i][idx].la;
    t[i][ls].sum+=1LL*(mid-l+1)*t[i][idx].la;
    t[i][rs].sum+=1LL*(r-mid)*t[i][idx].la;
    t[i][ls].pro+=1LL*(mid-l+1)*(mid+l)/2*t[i][idx].la;
    t[i][rs].pro+=1LL*(r-mid)*(r+mid+1)/2*t[i][idx].la;
    t[i][idx].la=0;
}

void pushup(int l,int r,int i)
{
    int idx=getidx(l,r),mid=MID(l,r);
    t[i][idx].sum=t[i][ls].sum+t[i][rs].sum;
    t[i][idx].pro=t[i][ls].pro+t[i][rs].pro;
}

void update(int l,int r,int L,int R,int i)
{
    int idx=getidx(l,r);
    if(L==l && r==R)
    {
        t[i][idx].la++;
        t[i][idx].sum+=1LL*(r-l+1);
        t[i][idx].pro+=1LL*(r-l+1)*(r+l)/2;
        return;
    }
    pushdown(l,r,i);
    int mid=MID(l,r);
    if(R<=mid)
        update(l,mid,L,R,i);
    else if(L>mid)
        update(mid+1,r,L,R,i);
    else
    {
        update(l,mid,L,mid,i);
        update(mid+1,r,mid+1,R,i);
    }
    pushup(l,r,i);
}

ll querysum(int l,int r,int L,int R,int i)
{
    if(L>R) return 0;
    int idx=getidx(l,r);
    if(L==l && r==R)
    {
        return t[i][idx].sum;
    }
    pushdown(l,r,i);
    int mid=MID(l,r);
    if(R<=mid)
        return querysum(l,mid,L,R,i);
    else if(L>mid)
        return querysum(mid+1,r,L,R,i);
    else
        return querysum(l,mid,L,mid,i)+querysum(mid+1,r,mid+1,R,i);
}

ll querypro(int l,int r,int L,int R,int i)
{
    if(L>R) return 0;
    int idx=getidx(l,r);
    if(L==l && r==R)
    {
        return t[i][idx].pro;
    }
    pushdown(l,r,i);
    int mid=MID(l,r);
    if(R<=mid)
        return querypro(l,mid,L,R,i);
    else if(L>mid)
        return querypro(mid+1,r,L,R,i);
    else
        return querypro(l,mid,L,mid,i)+querypro(mid+1,r,mid+1,R,i);
}

ll queryzhu(int x1,int x2,int y1,int y2)
{
    int P=(x2-x1);
    int Q=(y2-y1);
    //debug(P);debug(Q);
    int L=x1+y1;
    int R=L+min(P,Q);

    //debug(L);debug(R);

    ll sum=querysum(1,n<<1,L,R,0);
    ll pro=querypro(1,n<<1,L,R,0);
    ll ans=pro-sum*(L-1);

    //debug(sum);debug(pro);debug(ans);

    R=x2+y2;
    L=R-min(P,Q)+(P==Q);

    //debug(L);debug(R);

    sum=querysum(1,n<<1,L,R,0);
    pro=querypro(1,n<<1,L,R,0);
    ans+=(R+1)*sum-pro;

    //debug(sum);debug(pro);debug(ans);

    if(max(P,Q)>min(P,Q)+1)
    {
        L=x1+y1+min(P,Q)+1;
        R=x2+y2-min(P,Q)-1;

        //debug(L);debug(R);

        ans+=(min(P,Q)+1)*querysum(1,n<<1,L,R,0);
    }
    //debug(ans);
    return ans;
}

ll queryfu(int x1,int x2,int y1,int y2)
{
    int P=(x2-x1);
    int Q=(y2-y1);
//    debug(P);debug(Q);
    int L=x1-y2+n;
    int R=L+min(P,Q);

//    debug(L);debug(R);

    ll sum=querysum(1,n<<1,L,R,1);
    ll pro=querypro(1,n<<1,L,R,1);
    ll ans=pro-sum*(L-1);

//    debug(sum);debug(pro);debug(ans);

    R=x2-y1+n;
    L=R-min(P,Q)+(P==Q);

//    debug(L);debug(R);

    sum=querysum(1,n<<1,L,R,1);
    pro=querypro(1,n<<1,L,R,1);
    ans+=(R+1)*sum-pro;

//    debug(sum);debug(pro);debug(ans);

    if(max(P,Q)>min(P,Q)+1)
    {
        L=x1-y2+n+min(P,Q)+1;
        R=x2-y1+n-min(P,Q)-1;

//        debug(L);debug(R);

        ans+=(min(P,Q)+1)*querysum(1,n<<1,L,R,1);
    }
//    debug(ans);
    return ans;
}

int main()
{
//freopen("data.txt","r",stdin);
    int T,op,L,R,x1,x2,y1,y2;
    int cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&q);
        build(1,n<<1,0);
        build(1,n<<1,1);
        printf("Case #%d:\n",cas++);
        while(q--)
        {
            scanf("%d",&op);
            if(op==1)
            {
                scanf("%d %d",&L,&R);
                update(1,n<<1,L,R,0);
            }
            else if(op==2)
            {
                scanf("%d %d",&L,&R);
                L+=n;R+=n;
                update(1,n<<1,L,R,1);
            }
            else
            {
                scanf("%d %d %d %d",&x1,&x2,&y1,&y2);
                ll ans=queryzhu(x1,x2,y1,y2);
                ans+=queryfu(x1,x2,y1,y2);
                printf("%lld\n",ans);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值