南京网络赛B-The writing on the wall
- 30.43%
- 2000ms
- 262144K
Feeling hungry, a cute hamster decides to order some take-away food (like fried chicken for only 3030 Yuan).
However, his owner CXY thinks that take-away food is unhealthy and expensive. So she demands her hamster to fulfill a mission before ordering the take-away food. Then she brings the hamster to a wall.
The wall is covered by square ceramic tiles, which can be regarded as a n * mn∗m grid. CXY wants her hamster to calculate the number of rectangles composed of these tiles.
For example, the following 3 * 33∗3 wall contains 3636 rectangles:
Such problem is quite easy for little hamster to solve, and he quickly manages to get the answer.
Seeing this, the evil girl CXY picks up a brush and paint some tiles into black, claiming that only those rectangles which don't contain any black tiles are valid and the poor hamster should only calculate the number of the valid rectangles. Now the hamster feels the problem is too difficult for him to solve, so he decides to turn to your help. Please help this little hamster solve the problem so that he can enjoy his favorite fried chicken.
Input
There are multiple test cases in the input data.
The first line contains a integer TT : number of test cases. T \le 5T≤5.
For each test case, the first line contains 33 integers n , m , kn,m,k , denoting that the wall is a n \times mn×m grid, and the number of the black tiles is kk.
For the next kk lines, each line contains 22 integers: x\ yx y ,denoting a black tile is on the xx-th row and yy-th column. It's guaranteed that all the positions of the black tiles are distinct.
For all the test cases,
1 \le n \le 10^5,1\le m \le 1001≤n≤105,1≤m≤100,
0 \le k \le 10^5 , 1 \le x \le n, 1 \le y \le m0≤k≤105,1≤x≤n,1≤y≤m.
It's guaranteed that at most 22 test cases satisfy that n \ge 20000n≥20000.
Output
For each test case, print "Case #xx: ansans" (without quotes) in a single line, where xx is the test case number and ansans is the answer for this test case.
Hint
The second test case looks as follows:
样例输入复制
2
3 3 0
3 3 1
2 2
样例输出复制
Case #1: 36
Case #2: 20
题目来源
感觉这道题和暑假牛客网多校赛有道题很像 求数独子矩阵的 按那个方法敲了
T了 本来先用vector存的 然后排序 觉得这里可能会T 改成了优先队列
但是还是T了 可能有时候logn还是比较大吧 题解的算法是nmm 和 nmlogn比可能还是会小一点
实际上题解的方式和牛客网上这道题的思路是一样的 只不过少了处理相同字母这一部分 要更简单一点
AC代码:
相当于每次从一个矩阵的最右下角开始加一个一列的矩阵,加一个两列的矩阵,加一个三列的矩阵...........
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<set>
//#include<bits/stdc++.h>
#define inf 0x7f7f7f7f7f7f7f7f
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
int t, n, m, k;
int up[110], wall[maxn][110];
void init()
{
memset(wall, 0, sizeof(wall));
memset(up, 0, sizeof(up));
}
int main()
{
cin>>t;
for(int cas = 1; cas <= t; cas++){
scanf("%d%d%d", &n, &m, &k);
init();
for(int i = 0; i < k; i++){
int x, y;
scanf("%d%d", &x, &y);
wall[x][y] = 1;
}
LL ans = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(wall[i][j]){
up[j] = i;
}
}
for(int j = 1; j <= m; j++){
LL minn = inf;
for(int k = j; k > 0; k--){
minn = min(minn, (LL)(i - up[k]));
ans += minn;
}
}
}
printf("Case #%d: %lld\n", cas, ans);
}
return 0;
}
TLE代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<set>
//#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int maxn = 1e5;
int t, n, m, k;
int len[maxn], L[maxn][105], U[maxn][105];
//vector <LL> blackcol[105], blackrow[maxn];
priority_queue <int, vector<int>, greater<int> > blackcol[105], blackrow[maxn];
void init()
{
for(int i = 1; i <= n; i++){
while(!blackrow[i].empty()){
blackrow[i].pop();
}
blackrow[i].push(0);
//blackrow[i].clear();
//blackrow[i].push_back(0);
}
for(int i = 1; i <= m; i++){
while(!blackcol[i].empty()){
blackcol[i].pop();
}
blackcol[i].push(0);
//blackcol[i].clear();
//blackcol[i].push_back(0);
}
memset(L, 0, sizeof(L));
memset(U, 0, sizeof(U));
}
int main()
{
cin>>t;
for(int cas = 1; cas <= t; cas++){
scanf("%d%d%d", &n, &m, &k);
init();
for(int i = 0; i < k; i++){
int x, y;
scanf("%d%d", &x, &y);
blackcol[y].push(x);
blackrow[x].push(y);
//blackcol[y].push_back(x);
//blackrow[x].push_back(y);
}
/*for(int i = 1; i <= n; i++){
sort(blackrow[i].begin(), blackrow[i].end());
}
for(int i = 1; i <= m; i++){
sort(blackcol[i].begin(), blackcol[i].end());
}*/
for(int i = 1; i <= n; i++){
int now = blackrow[i].top();
blackrow[i].pop();
for(int j = 1; j <= m; j++){
if(!blackrow[i].empty()){
if(j == blackrow[i].top()){
now = blackrow[i].top();
blackrow[i].pop();
}
}
L[i][j] = min(L[i][j - 1] + 1, j - now);
}
}
for(int j = 1; j <= m; j++){
int now = blackcol[j].top();
blackcol[j].pop();
for(int i = 1; i <= n; i++){
if(!blackcol[j].empty()){
if(i == blackcol[j].top()){
now = blackcol[j].top();
blackcol[j].pop();
}
}
U[i][j] = min(U[i - 1][j] + 1, i - now);
}
}
LL ans = 0;
for(int j = 1; j <= m; j++){
memset(len, 0, sizeof(len));
for(int i = 1; i <= n; i++){
for(int k = 0; k < L[i][j]; k++){
len[k] = min(len[k] + 1, U[i][j - k]);
if(k)len[k] = min(len[k], len[k - 1]);
ans += len[k];
}
for(int k = L[i][j]; k < m; k++)len[k] = 0;
}
}
printf("Case #%d: %lld\n", cas, ans);
}
return 0;
}
南京网络赛B-The writing on the wall的更多相关文章
- 2018ICPC南京网络赛
2018ICPC南京网络赛 A. An Olympian Math Problem 题目描述:求\(\sum_{i=1}^{n} i\times i! \%n\) solution \[(n-1) \ ...
- HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)
Divide Groups Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)
Count The Pairs Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...
- HDU 4758 Walk Through Squares (2013南京网络赛1011题,AC自动机+DP)
Walk Through Squares Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Oth ...
- 2019ICPC南京网络赛A题 The beautiful values of the palace(三维偏序)
2019ICPC南京网络赛A题 The beautiful values of the palace https://nanti.jisuanke.com/t/41298 Here is a squa ...
- 2019 南京网络赛A
南京网络赛自闭现场 https://nanti.jisuanke.com/t/41298 二维偏序经典题型 二维前缀和!!! #include<bits/stdc++.h> using n ...
- 计蒜客 2018南京网络赛 I Skr ( 回文树 )
题目链接 题意 : 给出一个由数字组成的字符串.然后要你找出其所有本质不同的回文子串.然后将这些回文子串转化为整数后相加.问你最后的结果是多少.答案模 1e9+7 分析 : 应该可以算是回文树挺裸的题 ...
- The writing on the wall 南京网络赛2018B题
样例输入复制 2 3 3 0 3 3 1 2 2 样例输出复制 Case #1: 36 Case #2: 20 题目来源 ACM-ICPC 2018 南京赛区网络预赛 题意: 就是求图中去掉涂黑的方格 ...
- 南京网络赛G-Lpl and Energy【线段树】
During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Drag ...
随机推荐
- k-means算法及matlab实现
K-means算法很简单,它属于无监督学习算法中的聚类算法中的一种方法吧,利用欧式距离进行聚合啦. 解决的问题如图所示哈:有一堆没有标签的训练样本,并且它们可以潜在地分为K类,我们怎么把它们划分呢? ...
- 单精度浮点数(float)加法计算出错
场景: 一个float型的变量赋值1170601,加上19000000,结果出现错误. 原因: float占用4个字节(32位)存储空间,包括符号位1位,阶码位8位,尾数23位.浮点数精度与它的尾数有 ...
- php -- realpath($path) 函数
PHP realpath路径函数会检测$path指向的目标文件(或文件夹)是否真实存在,相当于调用了file_exists($path). 1.如果目标文件存在且不是符号连接(linux下俗称“软链接 ...
- JS中document对象详解
转自:http://www.cnblogs.com/andycai/archive/2010/06/29/1767351.html 对象属性 document.title //设置文档标题等价于HTM ...
- python 脚本检测python 版本
通过sys 模块的sys_info可以返回当前python 的版本信息, 其返回值是一个元组, 比如(2, 6, 6, 'final', 0); 表示当前版本为2.6.6 , 我们可以利用这个变量的值 ...
- MyBatis-使用mybatis-generator-core.jar生成POJO和Mapper文件
Demo: http://pan.baidu.com/s/1pLeyVv9 1.pom.xml <dependencies> <!-- 用于生成日志 --> <depen ...
- jQuery-处理元素内容、表单元素
处理元素内容 1.text方法 使用说明: 1)不传参数 得到jQuery对象内所有元素及其后代元素的文本内容 2)传入用于设置匹配元素的文本内容 3)传入function 使用函数来设置jQuery ...
- xml & < 需要转义
写了个request2XML的方法,每当数据中有'<'.'&'符号时,封装的XML就无法解析.发现了XML里的CDATA属性,问题迎刃而解!在XML文档中的所有文本都会被解析器解析. 只 ...
- HEVC有损优化二
1 . HEVC有很些设置对速度的提升有很大的帮助,比如m_bUseEarlyCU,m_useEarlySkipDetection等. 把它们设置成true,会有意想不到的效果. 比如对于不同分辨率 ...
- php 用命令行导出和导入MySQL数据库
命令行导出数据库:1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录如我输入的命令行:cd C:\Program Files\MySQL\MySQL Server 4.1\ ...