码迷,mamicode.com
首页 > 其他好文 > 详细

【luogu T34117 打油门】 题解

时间:2018-07-05 23:24:39      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:pac   题解   scan   std   cst   query   str   ace   print   

王强怎么这么强啊
王强太强了
二维树状数组

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int bit[2000][2000], n;
int lowbit(int x)
{
    return x&-x;
}
void add(int x, int y, int num)
{
    for(int i = x; i <= n; i += lowbit(i))
    for(int j = y; j <= n; j += lowbit(j))
    bit[i][j] += num;
}
int sum(int x, int y)
{
    int res = 0;
    for(int i = x; i > 0; i -= lowbit(i))
    for(int j = y; j > 0; j -= lowbit(j))
    res += bit[i][j];
    return res;
}
int query(int a,int b,int c,int d)
{
    return sum(c,d)-sum(a-1,d)-sum(c,b-1)+sum(a-1,b-1);
}
int main()
{
    scanf("%d",&n);
    int a,b,c,d,t;
    for(int i=1;;i++)
    {
        scanf("%d",&t);
        if(t==1)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(a+1,b+1,c);
        }
        if(t==2)
        {
            scanf("%d%d%d%d",&a,&b,&c,&d);
            printf("%d\n",query(a+1,b+1,c+1,d+1));
        }
        if(t==3)    return 0;
    }
}

【luogu T34117 打油门】 题解

标签:pac   题解   scan   std   cst   query   str   ace   print   

原文地址:https://www.cnblogs.com/MisakaAzusa/p/9270619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!