二维树状数组的区间加减及查询 tyvj 1716 上帝造题的七分钟

具体解释见小结。http://blog.csdn.net/zmx354/article/details/31740985

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>

#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define _INF 0x3f3f3f3f
#define Mod 9999991
#define lowbit(x) (x&(-x))

using namespace std;

const int N = 2049;

int a[N][N];
int ax[N][N];
int ay[N][N];
int axy[N][N];

void Updata(int a[N][N],int x,int y,int n,int m,int data)
{
    for(; x <= n; x += lowbit(x))
    {
        for(int t = y; t <= m; t += lowbit(t))
        {
            a[x][t] += data;
        }
    }
}

int Query(int a[N][N],int x,int y)
{
    int sum = 0;

    while(x > 0)
    {
        for(int t = y; t > 0; t -= lowbit(t))
        {
            sum += a[x][t];
        }
        x -= lowbit(x);
    }

    return sum;
}

int sum(int x,int y)
{
    return Query(a,x,y)*(x+1)*(y+1) - (y+1)*Query(ax,x,y) - (x+1)*Query(ay,x,y) + Query(axy,x,y);
}

void Add(int x1,int y1,int x2,int y2,int n,int m,int data)
{
    Updata(a,x1,y1,n,m,data);
    Updata(a,x2+1,y1,n,m,-data);
    Updata(a,x1,y2+1,n,m,-data);
    Updata(a,x2+1,y2+1,n,m,data);

    Updata(ax,x1,y1,n,m,x1*data);
    Updata(ax,x2+1,y1,n,m,-(x2+1)*data);
    Updata(ax,x1,y2+1,n,m,-x1*data);
    Updata(ax,x2+1,y2+1,n,m,(x2+1)*data);

    Updata(ay,x1,y1,n,m,y1*data);
    Updata(ay,x2+1,y1,n,m,-y1*data);
    Updata(ay,x1,y2+1,n,m,-(y2+1)*data);
    Updata(ay,x2+1,y2+1,n,m,(y2+1)*data);

    Updata(axy,x1,y1,n,m,x1*y1*data);
    Updata(axy,x2+1,y1,n,m,-(x2+1)*y1*data);
    Updata(axy,x1,y2+1,n,m,-x1*(y2+1)*data);
    Updata(axy,x2+1,y2+1,n,m,(x2+1)*(y2+1)*data);
}

int main()
{
    int n,m;

    char order[10];

    int x1,x2,y1,y2,data;

    memset(a,0,sizeof(a));
    memset(ax,0,sizeof(ax));
    memset(ay,0,sizeof(ay));
    memset(axy,0,sizeof(axy));

    while(scanf("%s",order) != EOF)
    {
        if(order[0] == 'L')
        {
            scanf("%d %d %d %d %d",&x1,&y1,&x2,&y2,&data);

            Add(x1,y1,x2,y2,n,m,data);
        }
        else if(order[0] == 'k')
        {
            scanf("%d %d %d %d",&x1,&y1,&x2,&y2);

            printf("%d\n",sum(x2,y2) - sum(x1-1,y2) - sum(x2,y1-1) + sum(x1-1,y1-1));
        }
        else
        {
            scanf("%d %d",&n,&m);
        }
    }

    return 0;
}

二维树状数组的区间加减及查询 tyvj 1716 上帝造题的七分钟

时间: 2024-08-03 19:16:20

二维树状数组的区间加减及查询 tyvj 1716 上帝造题的七分钟的相关文章

二维树状数组模板(区间修改+区间查询)

二维树状数组模板(区间修改+区间查询) 例题:JOIOI上帝造题的七分钟 一共两种操作: \(L\ x_1\ y_1\ x_2\ y_2\ d\):把\((x_1,y_1)\),\((x_2,y_2)\)这个矩形内所有元素加\(d\). \(k\ x_1\ y_1\ x_2\ y_2\):查询\((x_1,y_1)\),\((x_2,y_2)\)这个矩形内所有元素的和. 代码如下: #include<bits/stdc++.h> #define RG register #define IL i

POJ2155 Matrix 【二维树状数组】+【段更新点查询】

Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17766   Accepted: 6674 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1

一维 + 二维树状数组 + 单点更新 + 区间更新 详解

树状数组详解: 假设一维数组为A[i](i=1,2,...n),则与它对应的树状数组C[i](i=1,2,...n)是这样定义的: C1 = A1 C2 = A1 + A2 C3 = A3 C4 = A1 + A2 + A3 + A4 C5 = A5 C6 = A5 + A6 ................. C8 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 ................ 如图可知: 为奇数的时候他是代表他本身,而为偶数的时候则是代表着自

【bzoj5173】[Jsoi2014]矩形并 扫描线+二维树状数组区间修改区间查询

题目描述 JYY有N个平面坐标系中的矩形.每一个矩形的底边都平行于X轴,侧边平行于Y轴.第i个矩形的左下角坐标为(Xi,Yi),底边长为Ai,侧边长为Bi.现在JYY打算从这N个矩形中,随机选出两个不同的矩形,并计算它们的并的大小.JYY想知道,交的大小的期望是多少.换句话说即求在所有可能的选择中,两个矩形交的面积的平均大小是多大. 输入 输入一行包含一个正整数N. 接下来N行,每行4个整数,分别为Xi,Yi,Ai,Bi 2 < =  N < =  2*10^5, 0 < =  Xi,

二维树状数组-POJ-2155-Matrix

Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 23707 Accepted: 8762 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1 <=

POJ 2155 Matrix【二维树状数组+YY(区间更新,单点查询)】

题目链接:http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 32950   Accepted: 11943 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th col

POJ 1195-Mobile phones(二维树状数组-区间更新区间查询)

Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 17661   Accepted: 8173 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The

POJ 1195 Mobile phones(二维树状数组)

题目链接:POJ 1195 题意: 给出一个S*S的矩阵(行.列号从1开始),每个元素初始值为0,有两种操作:一种是第X行第Y列元素值加A:另一种是查询给定范围矩阵的所有元素之和(L<=X<=R,B<=Y<=T). 分析: 查询给定范围矩阵的所有元素之和是二维区间和,可以转换为二维前缀和求值.类比一维前缀和求法,二维区间和S(L, B, R, T) = S(1, 1, R, T) - S(1 ,1, L-1, T) - S(1, 1, R, B-1) + S(1, 1, L-1,

POJ 2155 Matrix(二维树状数组,绝对具体)

Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20599   Accepted: 7673 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1