UFOs

Time limit: 2.0 second
Memory limit: 64 MB
Vasya is a ufologist and his duties include observing Unidentified Flying Objects (UFOs) in the part of space bounded by a cube N × N × N. The cube is divided into cubic sectors 1 × 1 × 1. During the observation, the following events may happen:

  • several new UFOs emerge in a certain sector;
  • several UFOs disappear in a certain sector;
  • Vasya's boss may ask him how many UFOs there are in a part of space consisting of several sectors.

At the moment when Vasya starts his observations there are no UFOs in the whole space.

Input

The first line contains an integer N (1 ≤ N ≤ 128). The coordinates of sectors are integers from 0 to N–1.
Then there are entries describing events, one entry per line. Each entry starts with a number M.

  • If M is 1, then this number is followed by four integers x (0 ≤ x < N), y (0 ≤ y < N), z (0 ≤ z < N), K (–20000 ≤ K ≤ 20000), which are coordinates of a sector and the change in the number of UFOs in this sector. The number of UFOs in a sector cannot become negative.
  • If M is 2, then this number is followed by six integers x1y1z1x2y2z2 (0 ≤ x1 ≤ x2 <N, 0 ≤ y1 ≤ y2 < N, 0 ≤ z1 ≤ z2 < N), which mean that Vasya must compute the total number of UFOs in sectors (xyz) belonging to the volume: x1 ≤ x ≤ x2y1 ≤ y ≤ y2z1 ≤ z ≤z2.
  • If M is 3, it means that Vasya is tired and goes to sleep. This entry is always the last one.

The number of entries does not exceed 100002.

Output

For each query, output in a separate line the required number of UFOs.

Sample

input output
2
2 1 1 1 1 1 1
1 0 0 0 1
1 0 1 0 3
2 0 0 0 0 0 0
2 0 0 0 0 1 0
1 0 1 0 -2
2 0 0 0 1 1 1
3
0
1
4
2

分析:三维树状数组,求和时类似于容斥;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t;
ll a[][][];
void add(int x,int y,int z,int w)
{
for(int i=x;i<=n;i+=(i&(-i)))
for(int j=y;j<=n;j+=(j&(-j)))
for(int k=z;k<=n;k+=(k&(-k)))
a[i][j][k]+=w;
}
ll get(int x,int y,int z)
{
ll ans=;
for(int i=x;i;i-=(i&(-i)))
for(int j=y;j;j-=(j&(-j)))
for(int k=z;k;k-=(k&(-k)))
ans+=a[i][j][k];
return ans;
}
int main()
{
int i,j;
scanf("%d",&n);
while(~scanf("%d",&m)&&m!=)
{
if(m==)
{
int b[];
rep(i,,)scanf("%d",&b[i]);
add(++b[],++b[],++b[],b[]);
}
else
{
int b[];
rep(i,,)scanf("%d",&b[i]),b[i]++;
printf("%lld\n",get(b[],b[],b[])
-get(b[]-,b[],b[])-get(b[],b[]-,b[])-get(b[],b[],b[]-)
+get(b[]-,b[]-,b[])+get(b[],b[]-,b[]-)+get(b[]-,b[],b[]-)
-get(b[]-,b[]-,b[]-));
}
}
//system("Pause");
return ;
}

ural1470 UFOs的更多相关文章

  1. 1470. UFOs(三维树状数组)

    1470 最简单的三维树状数组 #include <iostream> #include<cstdio> #include<cstring> #include< ...

  2. USACO . Your Ride Is Here

    Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...

  3. UserControl和CustomControl基础【PluraSight】

    UserControl UserControl实际上就是ContentControl,xaml里<UserControl></UserControl>tag之间包含的实际就是后 ...

  4. R简易入门(二)

    本文内容来源:https://www.dataquest.io/mission/128/working-with-data-frames 本文摘要:简单介绍一下用R处理数据   原始数据展示(这是一份 ...

  5. CDH-5.4.3离线安装

    使用CM离线安装CDH-5.4.3,如下: cdh5.4.3安装 配置/etc/hosts vim /etc/hosts 192.168.10.1 s1 192.168.10.2 s2 192.168 ...

  6. (17)Questioning the universe

    https://www.ted.com/talks/stephen_hawking_asks_big_questions_about_the_universe/transcript00:13There ...

  7. Rails 5 Test Prescriptions 第8章 Integration Testing with Capybara and Cucumber

    Capybara:  A complete reference is available atrubydoc.info. 集成测试就是把局部的程序组合起来测试. 端到端测试是一个特殊的集成测试,覆盖了 ...

  8. USACO_1.1_Your_Ride_Is_Here_(字符串+水题)

    描述 http://train.usaco.org/usacoprob2?a=y0SKxY0Kc2q&S=ride 给出两个由大写字母组成,长度不大于$6$的字符串. 将字符串中的各字母的字典 ...

  9. 【字符串】Your Ride Is Here

    题目描述 It is a well-known fact that behind every good comet is a UFO. These UFOs often come to collect ...

随机推荐

  1. Mac 下格式化U盘

    diskutil list 查看U盘盘符: lapommedeMacBook-Pro:~ lapomme$ diskutil list /dev/disk0 (internal, physical): ...

  2. Html wmode 标签参数详解

    原文出处:http://blog.sina.com.cn/s/blog_4532d8b50101g2sw.html 在网页中嵌入swf文件时,经常会用到wmode这个参数,而嵌入的swf出现的一些问题 ...

  3. 用caffe给图像的混乱程度打分

    Caffe应该是目前深度学习领域应用最广泛的几大框架之一了,尤其是视觉领域.绝大多数用Caffe的人,应该用的都是基于分类的网络,但有的时候也许会有基于回归的视觉应用的需要,查了一下Caffe官网,还 ...

  4. Google Gson的使用方法

    用法1:从网络获取到json字符串之后,假如该字符串为data, Gson gson = new Gson(); HomeBean json = gson.fromJson(data, HomeBea ...

  5. perl中调用cgi

    来源: http://www.cnblogs.com/itech/archive/2012/09/23/2698856.html 参考:http://www.willmaster.com/librar ...

  6. Inno Setup入门(十九)——Inno Setup类参考(5)

    : Install Setup 2013-02-02 11:29 377人阅读 评论(0) 收藏 举报 单选按钮 单选按钮在安装中也很常见,例如同一个程序可以选择安装不同的性质的功能,例如选择32位或 ...

  7. on使用详解

    on()是bind(),live(),delegate()的替代品,1.7及1.7以后使用on() bind() 绑定元素 live() 为元素附加事件,匹配选择器的当前及未来的元素(比如由脚本创建的 ...

  8. Java中的一些术语的解释

    一  API(Application Programming Interface,应用程序编程接口) 简单来说,就是其他人开发出来一块程序,你想用,他会告诉你调用哪个函数,给这个函数传什么参数,然后又 ...

  9. 【jsp】读取WebRoot下的图像文件

    package readPic; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream ...

  10. JPA 系列教程2-单表操作

    JPA Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据.他的出现主要是为了简化现有的持久化开发工作和整合ORM技术,结束现在Hibe ...