HDU 4456(二维树状数组+坐标转换)
题目链接:Problem - 4456
看别人叙述看的心烦,于是我自己画了一张图。
上图。
上代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = ;
const int maxm = 3e6+; //这里大小的确定应该是80000*log2(20000)*log2(20000),算出来大约是一千三百万,数据应该是水一些,然而看所有人的题解,都是四百万,呵呵呵,这其中一定有什么交易。
int p[maxn],x[maxn],y[maxn],z[maxn];
int arr[maxm];
int cnt[maxm];
int num = ;
int len = ;
int w = ;
int lowbit(int x)
{
return x&(-x);
}
void pre(int x,int y)
{
for(int i=x;i<=w;i+=lowbit(i))
{
for(int j=y;j<=w;j+=lowbit(j))
{
cnt[num++] = i*w+j;
}
}
}
void sum(int x,int y,int tt)
{
for(int i=x;i<=w;i+=lowbit(i))
{
for(int j=y;j<=w;j+=lowbit(j))
{
int id = lower_bound(cnt,cnt+len,i*w+j)-cnt;
arr[id] += tt;
}
}
}
int getsum(int x,int y)
{
x=max(x,); x=min(x,w); //边界
y=max(y,); y=min(y,w);
int summ = ;
for(int i=x;i>;i-=lowbit(i))
{
for(int j=y;j>;j-=lowbit(j))
{
int id = lower_bound(cnt,cnt+len,i*w+j)-cnt;
if(cnt[id]==(i*w+j)) summ += arr[id];
}
}
return summ;
}
int main()
{
int n,m;
while(scanf("%d",&n)!=EOF&&n)
{
scanf("%d",&m);
w = *n;
memset(cnt,,sizeof(cnt));
memset(arr,,sizeof(arr));
num = ;
for(int i=;i<=m;i++)
{
scanf("%d %d %d %d",&p[i],&x[i],&y[i],&z[i]);
int nx = x[i]-y[i]+n;
int ny = x[i]+y[i];
if(p[i]==)
pre(nx,ny);
}
sort(cnt,cnt+num);
len = unique(cnt,cnt+num)-cnt;
for(int i=;i<=m;i++)
{
int nx = x[i]-y[i]+n;
int ny = x[i]+y[i];
if(p[i]==)
{
sum(nx,ny,z[i]);
}
else
{
int x1,y1,x2,y2,x3,y3,x4,y4;
x1 = nx+z[i];y1 = ny+z[i];
x2 = nx+z[i];y2 = ny-z[i];y2--;
x3 = nx-z[i];y3 = ny+z[i];x3--;
x4 = nx-z[i];y4 = ny-z[i];x4--;y4--;
printf("%d\n",getsum(x1,y1)-getsum(x2,y2)-getsum(x3,y3)+getsum(x4,y4));
}
}
}
return ;
}
HDU 4456(二维树状数组+坐标转换)的更多相关文章
- hdu 2642 二维树状数组 单点更新区间查询 模板水题
Stars Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others) Total Subm ...
- hdu 2642二维树状数组 单点更新区间查询 模板题
二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...
- 【 HDU - 4456 】Crowd (二维树状数组、cdq分治)
BUPT2017 wintertraining(15) #5A HDU 4456 题意 给你一个n行n列的格子,一开始每个格子值都是0.有M个操作,p=1为第一种操作,给格子(x,y)增加z.p=2为 ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU 5517---Triple(二维树状数组)
题目链接 Problem Description Given the finite multi-set A of n pairs of integers, an another finite mult ...
- HDU 5517 【二维树状数组///三维偏序问题】
题目链接:[http://acm.split.hdu.edu.cn/showproblem.php?pid=5517] 题意:定义multi_set A<a , d>,B<c , d ...
- HDU 5465 Clarke and puzzle Nim游戏+二维树状数组
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5465 Clarke and puzzle Accepts: 42 Submissions: 26 ...
- HDU_4456_二维树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=4456 第一道二维树状数组就这么麻烦,题目要计算的是一个菱形范围内的和,于是可以把原来的坐标系旋转45度,就是求一 ...
- HDU1559 最大子矩阵 (二维树状数组)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others) ...
随机推荐
- 开发MOSS自定义字段类型
前段时间,由于刚好项目定制的需要,笔者就开发了几个自定义字段类型.在这抽空做个详细笔记,方便初学者学习.这方面的资料也很多,如果自身觉得不大明白可以参考下SDK和网上的相关文章.本章的目的主要是给新手 ...
- Box of Bricks
Box of Bricks Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- python的历史与优劣
历史 Python的创始人是Guido van Rossum,在发明Python语言之前Guido曾参与过一门称作ABC的语言的设计,ABC是专门为非专业程序员设计的:Guido在Python语言的设 ...
- NSArray的containsObject漏洞
1.NSArray中的containsObject的用法 NSMutableArray *array=[NSMutableArray array]; if(![array containsObject ...
- php过滤提交数据 防止sql注入攻击
规则 1:绝不要信任外部数据或输入 关于 Web 应用程序安全性,必须认识到的第一件事是不应该信任外部数据.外部数据(outside data) 包括不是由程序员在 PHP 代码中直接输入的任何数据. ...
- .project
http://blog.csdn.net/qiushuichangtian888/article/details/9299843 一个老项目导入新环境后老是提示build.properties不存在的 ...
- C语言 - pthread
pthread_create函数 原型:int pthread_create((pthread_t *thread, pthread_attr_t *attr, void *(*start ...
- IT项目各阶段管理
- C/C++ kubetu
reference sign & use predefine in c, but const in c++
- Integer比较值的时候小心使用
package integerdemo; public class IntegerDemo { public static void main(String[] args) { //-128--127 ...