hdu 5480(维护前缀和+思路题)
2016-07-09 16:54
489 查看
Conturbatio
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 786 Accepted Submission(s): 358
[align=left]Problem Description[/align]
There are many rook on a chessboard, a rook can attack the row and column it belongs, including its own place.
There
are also many queries, each query gives a rectangle on the chess board,
and asks whether every grid in the rectangle will be attacked by any
rook?
[align=left]Input[/align]
The first line of the input is a integer T, meaning that there are T test cases.
Every test cases begin with four integers n,m,K,Q.
K is the number of Rook, Q is the number of queries.
Then K lines follow, each contain two integers x,y describing the coordinate of Rook.
Then Q lines follow, each contain four integers x1,y1,x2,y2 describing the left-down and right-up coordinates of query.
1≤n,m,K,Q≤100,000.
1≤x≤n,1≤y≤m.
1≤x1≤x2≤n,1≤y1≤y2≤m.
[align=left]Output[/align]
For every query output "Yes" or "No" as mentioned above.
[align=left]Sample Input[/align]
2
2 2 1 2
1 1
1 1 1 2
2 1 2 2
2 2 2 1
1 1
1 2
2 1 2 2
[align=left]Sample Output[/align]
Yes
No
Yes
Hint
Huge input, scanf recommended.
[align=left]Source[/align]
BestCoder Round #57 (div.2)
题意:在一个棋盘上有一些"车",他能够攻击到与它同一行或者同一列的棋盘上的所有的格子,现在给出K个棋子的坐标,然后有Q组询问,每一次询问(x1,y1,x2,y2)这个方格内的所有棋子是否能够全部被攻击到。
题解:维护前缀和,统计 (x1-x2) 这一段区间里面的被攻击到的行的数量,统计(y1-y2)这一段区间里面的被攻击到的列的数量,如果sum(x1~x2) == x2-x1+1 ,那么这段区间全部能够被攻击到,列也就不用考虑了,对列的考虑亦如此。
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <vector> #include <algorithm> using namespace std; const int N = 100005; int flag_x ,flag_y ; int main() { int tcase; scanf("%d",&tcase); while(tcase--){ int n,m,k,q; scanf("%d%d%d%d",&n,&m,&k,&q); memset(flag_x,0,sizeof(flag_x)); memset(flag_y,0,sizeof(flag_y)); int x,y; for(int i=1;i<=k;i++){ scanf("%d%d",&x,&y); flag_x[x] = 1; flag_y[y] = 1; } for(int i=1;i<=n;i++){ flag_x[i]+= flag_x[i-1]; } for(int i=1;i<=m;i++){ flag_y[i] += flag_y[i-1]; } while(q--){ int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); if(flag_x[x2]-flag_x[x1-1]==x2-x1+1||flag_y[y2]-flag_y[y1-1]==y2-y1+1) printf("Yes\n"); else printf("No\n"); } } return 0; }
相关文章推荐
- 使用Docker实现持续集成
- 在Linux中创建静态库.a和动态库.so
- Linux 2.4.x内核软中断机制
- Ubuntu 16.04 lts 下安装mysql + php + nginx + phpmyadmin(图形化mysql管理工具)
- centos安装mysql
- Linux手机DIY.库文件专题.知识准备
- Linux电源管理研究笔记—动态电源管理(DPM)
- Centos6.5安装配置KVM
- org.apache.commons.lang3.StringUtils类中isBlank和isEmpty方法的区别
- RPC 工具--Thrift 框架 (一)
- 嵌入式Linux编译器gcc
- 利用window.open实现post方式的参数传递
- Linux C第二课
- CentOS 6.4下PXE+Kickstart无人值守安装操作系统
- Linux的inode的理解
- 手把手搭建gitosis 绝对不坑
- Hadoop RPC
- 个人收藏的常用前端网站
- sqoop遇到的问题
- iOS 中的MVC,MVP,MVVM以及VIPER架构