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. webapp前端开发软键盘与position:fixed为我们带来的不便

    前提:我们考虑兼容的环境为android和ios两种智能手机 兼容环境测试结果显示android的表现明显好于ios,ios手机在软键盘呼起收起时存在着很严重的兼容性问题 场景展示: 页面正常状态 软 ...

  2. Failed to load c++ bson extension, using pure JS version

    Failed to load c++ bson extension, using pure JS version npm install mongodbnpm install bson npm ins ...

  3. 使用queryperf对DNS服务器作压力测试

    一.querperf简介 当我们把DNS服务器配置好后,我们肯定会想测试一下DNS服务器的性能如何,上线后如果请求数够多服务器还能否响应?于是,我们可以使用软件模拟环境,对DNS服务器作评估性的测试. ...

  4. 我眼中的C#3.0 摘自于网络:http://www.cnblogs.com/joinger/articles/1297237.html

    每次有新技术发布时,我们总能感受到两种截然不同的情绪: 一种是恐惧和抵抗,伴随着这种情绪的还有诸如"C# 2.0用的挺好的,为什么要在C# 3.0搞到那么复杂?"或者"我 ...

  5. webwork <ww:if> 标签的使用

    如果在前台(JSP)取出后台的对象的属性,这个属性在后台是属于String 类型的,但若这个属性的值为数字,取出在前台就会默认为整形的值,所以在<ww:if> 判断里面不能加引号:< ...

  6. ssh登录nat模式的VMware虚拟机

    有时候本地PC是固定IP上网方式且无多余IP,而我们又希望使用putty登陆VMware中的虚拟机且虚拟机可以上外网,那么这时候就可以使用端口映射. 1.本地环境简述 本地PC IP:192.168. ...

  7. JAVA-面向对象--封装

    1. 如果一个类中没有定义构造函数,会自动加上一个空参的默认构造函数 如果定义了一个构造函数,默认的空参构造函数就没有了. 2. this 当成员变量与局部变量重名的时候,可以使用this来区分.th ...

  8. Objective-C相关Category的收集

    Objective-C相关Category的收集 Categories是给你得不到源码的classes增加功能的一种方法.这个页面收集一些相关的Category,并且持续更新,你可以订阅关注.作者是F ...

  9. android 设计模式学习

    1:单例模式 //对于创建开销较大的类可使用此方法,保证全局一个实例,在程序运行过程中该类不会因新建额外对象产生开销.示例代码如下:public class Singleton { private s ...

  10. git使用之如何将github库下载到本地与如何将代码上传github

    git使用之如何将github库下载到本地与如何将代码上传github ---------------------------------------------------------------- ...