12508 - Triangles in the Grid

题目链接

题意:给定一个nm格子的矩阵,然后给定A,B。问能找到几个面积在A到B之间的三角形。

思路:枚举每一个子矩阵,然后求[0,A]的个数减去[0,B]的个数就是答案,然后对于每一个子矩阵个数非常好求为(nr+1)∗(mc+1)。

关键在于怎么求每一个子矩阵的符合个数。

想了好久,參考别人题解才想出来。分3种情况讨论:

1、一个点在矩形顶点。另外两点相应在顶点的另外两边上。

2、两个点在顶点上。另外一点在对边上。

3、三个点都在顶点上

然后分别去计算求和。详细的过程比較麻烦,在纸上多画画一边就能得到一个o(N)的方法。大概是枚举一个在竖直边上的位置,横的位置利用公式运算一步求解,这样时间复杂度是能够接受的

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int t;
long long n, m, a, b; long long solve(long long limit) {
long long ans = 0;
for (long long r = 1; r <= n; r++) {
for (long long c = 1; c <= m; c++) {
long long count = 0;
long long up, down;
if (r * c <= limit) count += 2 * (r - 1 + c - 1);
for (long long x = 0; x <= r; x++) {
up = min(c, (x * c + limit) / r);
long long tmp = x * c - limit;
if (tmp <= 0) down = 0;
else down = (tmp - 1) / r + 1;
if (down <= up) count += 2 * (up - down + 1);
}
for (long long x = 1; x < r; x++) {
long long s = (r * c - x);
if (s <= limit) count += 4 * (c - 1);
else count += 4 * ((c - 1) - min((s - limit) / x + ((s - limit) % x != 0), c - 1));
}
ans += count * (n - r + 1) * (m - c + 1);
}
}
return ans;
} int main() {
scanf("%d", &t);
while (t--) {
scanf("%lld%lld%lld%lld", &n, &m, &a, &b);
a <<= 1; b <<= 1; if (a == 0) a = 1;
printf("%lld\n", solve(b) - solve(a - 1));
}
return 0;
}

UVA 12508 - Triangles in the Grid(计数问题)的更多相关文章

  1. uva 12508 - Triangles in the Grid(几何+计数)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/35244875 题目链接:uva 12508 ...

  2. UVA 12651 Triangles

    You will be given N points on a circle. You must write a program to determine how many distinctequil ...

  3. UVA 12075 - Counting Triangles(容斥原理计数)

    题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对 ...

  4. uva 10671 - Grid Speed(dp)

    题目链接:uva 10671 - Grid Speed 题目大意:给出N,表示在一个N*N的网格中,每段路长L,如今给出h,v的限制速度,以及起始位置sx,sy,终止位置ex,ey,时间范围st,et ...

  5. Cheerleaders UVA - 11806 计数问题

    In most professional sporting events, cheerleaders play a major role in entertaining the spectators. ...

  6. uva 11605 - Lights inside a 3d Grid(概率)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=2652" style=""& ...

  7. UVA 12382 Grid of Lamps 贪心

    题目链接: C - Grid of Lamps Time Limit:1000MSMemory Limit: 0KB 问题描述 We have a grid of lamps. Some of the ...

  8. UVA 11916 Emoogle Grid(同余模)

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. uva 11529 - Strange Tax Calculation(计数问题)

    题目链接:uva 11529 - Strange Tax Calculation 题目大意:给出若干个点,保证随意三点不共线.随意选三个点作为三角行,其它点若又在该三角形内,则算是该三角形内部的点.问 ...

随机推荐

  1. git的学习笔记整理

    Git学习较好的网址:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001373 ...

  2. Gson解析List的一点小问题

    这阵子在使用gson解析时遇到了点小麻烦,因为一直用的fastjson,最近一个项目里使用的gson,需要解析list集合,查资料都是使用TypeToken解决,相对比较麻烦,下面为大伙推荐一种简便的 ...

  3. Access-Control-Allow-Origin 如何设置多个值呢

    需求就是多个网站请求同一个api服务器和这里一样https://segmentfault.com/q/10... 我不是做后端的,但是我们后端不知道怎么设置,在web.config里设置了一下 < ...

  4. JavaScript 获取移动设备的型号

    https://joyqi.com/javascript/how-to-detect-mobile-devices-model-using-javascript.html?utm_source=too ...

  5. 欢迎访问微先锋vXianFeng官方博客

    欢迎访问微先锋vXianFeng官方博客,专注微商城.P2P理财.山寨矿机平台研究与开发!

  6. Win10+TensorFlow-gpu pip方式安装,anaconda方式安装

    中文官网安装教程:https://www.tensorflow.org/install/install_windows#determine_how_to_install_tensorflow 1.安装 ...

  7. 题解 P1198 【[JSOI2008]最大数】

    说起来这还是蒟蒻AC的第一道省选线段树呢. 这道题和其他线段树最大的不同就是在于本题数组一直在增大. 寻常的线段树蒟蒻习惯用如下的结构体储存,然而对于此题就不行了: struct node{ int ...

  8. 【BZOJ 1047】[HAOI2007]理想的正方形

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 二维的ST表. 每个大的正方形可以由4个小的正方形组成. 然后区域内的最大值最小值.也可以由4个小的张方形部分全部覆盖到. [代码] ...

  9. OpenGL之抗锯齿 以及 线宽的设置

    转自原文 OpenGL之抗锯齿 以及 线宽的设置 抗锯齿 1.线的抗锯齿 glEnable(GL_LINE_SMOOTH); //启用 glHint(GL_LINE_SMOOTH,GL_NICEST) ...

  10. AFNetworking框架的使用

    #import "ViewController.h" #import "AFNetworking.h" @interface ViewController () ...