C. Star sky 二维前缀和
2 seconds
256 megabytes
standard input
standard output
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 ≤ si ≤ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 ≤ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right — (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
The first line contains three integers n, q, c (1 ≤ n, q ≤ 105, 1 ≤ c ≤ 10) — the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si(1 ≤ xi, yi ≤ 100, 0 ≤ si ≤ c ≤ 10) — the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 ≤ ti ≤ 109, 1 ≤ x1i < x2i ≤ 100, 1 ≤ y1i < y2i ≤ 100) — the moment of the i-th view and the coordinates of the viewed rectangle.
For each view print the total brightness of the viewed stars.
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
3
0
3
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
3
3
5
0
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
typedef long long LL;
#define MAXN 109
#define N 100
/*
坐标的最大值比较小 开前缀数组求和
*/
LL a[MAXN][MAXN][];
LL n, q, c;
int main()
{
cin >> n >> q >> c;
int t1, t2, t3, t4;
for (LL i = ; i < n; i++)
{
cin >> t1 >> t2 >> t3;
a[t1][t2][t3]++;
}
for(int i =;i<=;i++)
for(int j=;j<=;j++)
for (int k = ; k < c + ; k++)
{
a[i][j][k] += a[i - ][j][k] + a[i][j - ][k] - a[i - ][j - ][k];
}
int T;
int x1, y1, x2, y2;
while (q--)
{
cin >> T >> x1 >> y1 >> x2 >> y2;
LL cnt = , sum = ;
for (int i = ; i < c + ; i++)
{
cnt = a[x2][y2][i] - a[x2][y1 - ][i] - a[x1 - ][y2][i] + a[x1 - ][y1 - ][i];
sum += cnt*((i + T) % (c + ));
}
cout << sum << endl;
}
}
错误的二分代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
typedef long long LL;
#define MAXN 100005
#define N 100
/*
二分查找区域内符合范围的点,取模运算求和
...
*/
struct star
{
LL x, y, s;
bool operator<(const star& rhs)
{
if (x == rhs.x)
return y < rhs.y;
else
return x < rhs.x;
}
}a[MAXN];
LL n, q, c;
LL t, X1, Y1, X2, Y2;
LL sum = ;
void solve(LL l, LL r)
{
if (l > r)
return;
if (l == r)
{
if (a[l].x >= X1&&a[l].x >= Y1&&a[l].x <= X2&&a[l].y <= Y2)
sum += (a[l].s + t) % (c + );
return;
}
LL mid = (l + r) / ;
//cout << mid << endl;
if (a[mid].x > X2)
solve(l, mid - );
else if (a[mid].x < X1)
solve(mid + , r);
else
{
if (a[mid].x >= X1&&a[mid].x >= Y1&&a[mid].x <= X2&&a[mid].y <= Y2)
sum += (a[mid].s + t) % (c + );
solve(l, mid - );
solve(mid + , r);
}
}
int main()
{
scanf("%lld%lld%lld", &n, &q, &c);
for (LL i = ; i < n; i++)
scanf("%lld%lld%lld", &a[i].x, &a[i].y, &a[i].s);
sort(a, a + n);
while (q--)
{
sum = ;
scanf("%lld%lld%lld%lld%lld", &t, &X1, &Y1, &X2, &Y2);
solve(, n-);
printf("%lld\n", sum);
}
return ;
}
C. Star sky 二维前缀和的更多相关文章
- Codeforces 835C - Star sky - [二维前缀和]
题目链接:http://codeforces.com/problemset/problem/835/C 题意: 在天空上划定一个直角坐标系,有 $n$ 颗星星,每颗星星都有坐标 $(x_i,y_i)$ ...
- Star sky 二维前缀和
C. Star sky time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- openjudge1768 最大子矩阵[二维前缀和or递推|DP]
总时间限制: 1000ms 内存限制: 65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如,如下4 * 4的 ...
- COGS1752 [BOI2007]摩基亚Mokia(CDQ分治 + 二维前缀和 + 线段树)
题目这么说的: 摩尔瓦多的移动电话公司摩基亚(Mokia)设计出了一种新的用户定位系统.和其他的定位系统一样,它能够迅速回答任何形如“用户C的位置在哪?”的问题,精确到毫米.但其真正高科技之处在于,它 ...
- poj-3739. Special Squares(二维前缀和)
题目链接: I. Special Squares There are some points and lines parellel to x-axis or y-axis on the plane. ...
- Good Bye 2015 C. New Year and Domino 二维前缀
C. New Year and Domino They say "years are like dominoes, tumbling one after the other". ...
- 计蒜客模拟赛D1T1 蒜头君打地鼠:矩阵旋转+二维前缀和
题目链接:https://nanti.jisuanke.com/t/16445 题意: 给你一个n*n大小的01矩阵,和一个k*k大小的锤子,锤子只能斜着砸,问只砸一次最多能砸到多少个1. 题解: 将 ...
- 二维前缀和模板题:P2004 领地选择
思路:就是使用二维前缀和的模板: 先放模板: #include<iostream> using namespace std; #define ll long long ; ll a[max ...
- 二维前缀和好题hdu6514
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; ]; )* ...
随机推荐
- centos 7 配置php
对于我们的目的而言,安装 Apache 只需要在 CentOS 命令终端敲入这条命令就行了: $ sudo yum install httpd $ sudo systemctl enable http ...
- MVC之模型绑定
1.前言 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方 ...
- 第八届蓝桥杯省赛C/C++ A组第4题 方格分割
参考了http://blog.csdn.net/y1196645376/article/details/69718192,这个大哥的思路很巧妙. 思路: dfs. 实现: #include <i ...
- 从React看weight开发
从当前云发展的势头来看几乎所有互联网应用都趋向大一统的趋势,一个node下面加一堆应用,同时我们项目也趋向把复杂的大应用拆分成多个小应用,通过各种复杂的Api来协作,通信,达到同样的效果. 可以看出, ...
- Javascript中的那些bug
1. x(比如document) is not defined 不止要检查是不是没有声明变量就使用了,还要检查是不是对象的方法调用写错了!比如: alert( document.getElementB ...
- 【转】jvm类加载
类加载机制 JVM把class文件加载的内存,并对数据进行校验.转换解析和初始化,最终形成JVM可以直接使用的Java类型的过程就是加载机制. 类从被加载到虚拟机内存中开始,到卸载出内存为止,它的生命 ...
- js中时钟表盘
1.js时钟表盘 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- mongo 3.4分片集群系列之八:分片管理
这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...
- servlet——web应用中路径问题
target.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html ...
- BackboneJS and UnderscoreJS
介绍 来自API(backbone能做什么?) 当我们开发含有大量Javascript的web应用程序时,首先你需要做的事情之一便是停止向DOM对象附加数据. 通过复杂多变的jQuery选择符和回调 ...