UFOs(三维树状数组)

Description

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

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

解题思路:

貌似树状数组不太有用三维的,不过这题用三维还是非常方便的。之前做了道Mobile phones,跟这道没什么区别。三维需要的注意的就是如何减去重复的空间,有一个空间重复了2次,还有三个空间重复了1次,讲也不太能讲的清楚,自己拿笔把立方体画出来并进行分割就行。代码写的比较挫。。。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 130;
int c[maxn][maxn][maxn], n;
int lowbit(int a)
{
    return a & (-a);
}
void Update(int x,int y,int z,int a)
{
    for(int i = x; i <= n; i += lowbit(i))
        for(int j = y; j <= n; j += lowbit(j))
            for(int k = z; k <= n; k += lowbit(k))
            c[i][j][k] += a;
}
int Sum(int x,int y,int z)
{
    int sum = 0;
    for(int i = x; i > 0; i -= lowbit(i))
        for(int j = y; j > 0; j -= lowbit(j))
            for(int k = z; k > 0; k -= lowbit(k))
            sum += c[i][j][k];
    return sum;
}
int main()
{
    int m, x, y, z, a, x1, y1, z1, x2, y2, z2;
    memset(c, 0, sizeof(c));
    scanf("%d",&n);
    while(scanf("%d", &m))
    {
        if(m == 3)
            break;
        if(m == 1)
        {
            scanf("%d%d%d%d", &x, &y, &z, &a);
            Update(x + 1, y + 1, z + 1, a);
        }
        if(m == 2)
        {
            scanf("%d%d%d%d%d%d", &x1, &y1, &z1, &x2, &y2, &z2);
            int a = Sum(x2 + 1, y2 + 1, z2 + 1);
            int b = Sum(x1, y2 + 1, z2 + 1);
            int c = Sum(x2 + 1, y1, z2 + 1);
            int d = Sum(x2 + 1, y2 + 1, z1);
            int e = Sum(x1, y1, z1);
            int f = Sum(x1, y2 + 1, z1);
            int g = Sum(x1, y1, z2 + 1);
            int h = Sum(x2 + 1, y1, z1);
            printf("%d\n", a - b - c - d - e + f + g + h);
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为你提供一个基于 Python Pygame 模块的简单游戏示例,名为“打飞碟游戏”: ``` import pygame import random # 初始化 Pygame pygame.init() # 设置窗口大小和标题 WIDTH = 600 HEIGHT = 400 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("打飞碟游戏") # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) # 加载图片 BACKGROUND_IMAGE = pygame.image.load("background.jpg").convert() UFO_IMAGE = pygame.image.load("ufo.png").convert_alpha() # 设置游戏参数 FPS = 60 UFO_SPEED = 5 UFO_SIZE = 50 SCORE = 0 FONT = pygame.font.SysFont("Arial", 24) # 定义飞碟类 class UFO: def __init__(self): self.x = random.randint(0, WIDTH-UFO_SIZE) self.y = -UFO_SIZE self.speed = UFO_SPEED def update(self): self.y += self.speed def draw(self, surface): surface.blit(UFO_IMAGE, (self.x, self.y)) # 初始化飞碟列表 ufos = [] # 设置时钟 clock = pygame.time.Clock() # 游戏循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: # 检查点击是否命中飞碟 for ufo in ufos: if ufo.x <= event.pos[0] <= ufo.x+UFO_SIZE and ufo.y <= event.pos[1] <= ufo.y+UFO_SIZE: SCORE += 10 ufos.remove(ufo) # 更新游戏状态 for ufo in ufos: ufo.update() if ufo.y > HEIGHT: ufos.remove(ufo) # 生成新飞碟 if random.randint(1, 60) == 1: ufos.append(UFO()) # 绘制游戏画面 screen.blit(BACKGROUND_IMAGE, (0, 0)) for ufo in ufos: ufo.draw(screen) score_text = FONT.render(f"得分:{SCORE}", True, RED) screen.blit(score_text, (10, 10)) # 刷新画面 pygame.display.flip() # 控制帧率 clock.tick(FPS) # 退出 Pygame pygame.quit() ``` 这个游戏创建一个 Pygame 窗口,背景是一张图片,玩家需要点击飞碟来得分。每隔一段时间会有新的飞碟出现,玩家需要在飞碟离开屏幕前尽可能地击中它们。当玩家关闭窗口时,游戏结束,显示得分。你可以根据自己的需求进行修改和扩展,例如增加难度、音效等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值