hdu 1892 See you~

本文介绍了一种使用二维树状数组解决复杂查询更新问题的方法。通过具体的代码实现展示了如何进行区间查询与更新操作,适用于处理大规模数据集上的快速查询与更新任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写了好久才ac,之前一直是re。
主要是二维树状数组

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int c[2000][2000];
int lowbit(int x)
{
    return x&(-x);
}

int sum(int x,int y)
{
    int ret=0,t=y;
    while(x>0)
    {
        y=t;
        while(y>0)
        {
            ret+=c[x][y];
            y-=lowbit(y);
        }
        x-=lowbit(x);
    }
    return ret;
}

void add(int x,int y,int d)
{
    int t=y;
    while(x<=1002)
    {
        y=t;
        while(y<=1002)
        {
            c[x][y]+=d;
            y+=lowbit(y);
        }
        x+=lowbit(x);
    }
}

int main()
{
    int T,n,i,j,k,p,ans,t,a1,a2,b1,b2,n1;
    char s[10];
    scanf("%d",&T);
    for(k=1; k<=T; k++)
    {
        printf("Case %d:\n",k);
        memset(c,0,sizeof(c));
        for(i=1; i<=1002; i++)
            for(j=1; j<=1002; j++)
                add(i,j,1);
        scanf("%d",&n);
        for(p=1; p<=n; p++)
        {
            scanf("%s",s);
            if(s[0]=='S')
            {
                scanf("%d%d%d%d",&a1,&b1,&a2,&b2);
                //a1++;b1++;a2++;b2++;
                if(a1>a2) swap(a1,a2);
                if(b1>b2) swap(b1,b2);
                ans=sum(a2+1,b2+1)+sum(a1,b1)-sum(a1,b2+1)-sum(a2+1,b1);
                printf("%d\n",ans);
            }
            else if(s[0]=='A')
            {
                scanf("%d%d%d",&a1,&b1,&n1);
                //a1++;b1++;
                add(a1+1,b1+1,n1);
            }
            else if(s[0]=='D')
            {
                scanf("%d%d%d",&a1,&b1,&n1);
                //a1++;b1++;
                t=sum(a1+1,b1+1)+sum(a1,b1)-sum(a1+1,b1)-sum(a1,b1+1);
                if(t>n1) t=n1;
                add(a1+1,b1+1,-t);
            }
            else
            {
                scanf("%d%d%d%d%d",&a1,&b1,&a2,&b2,&n1);
                //a1++;b1++;a2++;b2++;
                t=sum(a1+1,b1+1)+sum(a1,b1)-sum(a1+1,b1)-sum(a1,b1+1);
                if(t>n1) t=n1;
                add(a1+1,b1+1,-t);
                add(a2+1,b2+1,t);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值