首页 > 其他 > 详细

POJ 1195 Mobile Phones

时间:2014-01-27 17:16:32      阅读:249      评论:0      收藏:0      [点我收藏+]

    树状数组,开始的时候wa了,后来看看,原来是概率论没学好,以为求(L,B) - (R,T) 矩阵内的和只要用sum(R+1,T+1) - sum(L,B) 就行了,。傻x了。。

    必须 sum(R,T) - sum(L,T) - sum(R,B) + sum(L,B) ;  (R,T 已经自加1)   诫之。

 

    代码:

bubuko.com,布布扣
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 1027

int c[N][N];
int n;

int lowbit(int x)
{
    return x&(-x);
}

void modify(int x,int y,int val)
{
    for(int i=x;i<=n;i+=lowbit(i))
    {
        for(int j=y;j<=n;j+=lowbit(j))
        {
            c[i][j] += val;
        }
    }
}

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 += c[i][j];
        }
    }
    return res;
}

int main()
{
    int op,L,B,R,T,A,X,Y;
    while(scanf("%d",&op)!=EOF)
    {
        if(op == 3)
            break;
        else if(op == 0)
        {
            memset(c,0,sizeof(c));
            scanf("%d",&n);
        }
        else if(op == 1)
        {
            scanf("%d%d%d",&X,&Y,&A);
            X++;Y++;
            modify(X,Y,A);
        }
        else if(op == 2)
        {
            scanf("%d%d%d%d",&L,&B,&R,&T);
            R++;T++;
            printf("%d\n",sum(R,T)-sum(L,T)-sum(R,B)+sum(L,B));
        }
    }
    return 0;
}
View Code

POJ 1195 Mobile Phones

原文:http://www.cnblogs.com/whatbeg/p/3534696.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!