Codeforces Round #524 (Div. 2) C. Masha and two friends
C. Masha and two friends
题目链接:https://codeforc.es/contest/1080/problem/C
题意:
给出一个黑白相间的n*m的矩阵,现在先对一个子矩阵颜色变为白色,然后再对一个子矩阵颜色变为黑色,问最终白色格子和黑色格子有多少?
题解:
定义w(a,b)为(1,1)到(a,b)中白色的数量,通过观察找规律可以发现w(a,b)=((b+1)/2)*((a+1)/2)+(b/2)*(a/2)。
然后b(a,b)就是格子总数量减去w(a,b)。
现在对于一个子矩阵里面黑色和白色格子数量就比较容易求了。
再来分析两次变化颜色,主要时两个子矩阵交叉时,由于这里给出坐标的大小关系,所以(max(x1,x3),max(y1,y3))与(min(x2,x4),min(y2,y4))即为交叉矩阵的两个端点。
存在交叉矩阵时,黑色的数量会加上交叉矩阵中黑色的格子的数量,白色的数量会减去交叉矩阵中黑色格子的数量(结合前两次变化思考),最后直接求解即可。
代码如下:
#include <bits/stdc++.h>
using namespace std; typedef long long ll ;
int t; ll w(ll a,ll b){
return ((b+)/)*((a+)/)+(b/)*(a/);
}
ll sub_w(ll x1,ll y1,ll x2,ll y2){
return w(x2,y2)-w(x1-,y2)-w(x2,y1-)+w(x1-,y1-);
}
ll sub_b(ll x1,ll y1,ll x2,ll y2){
return (x2-x1+)*(y2-y1+)-sub_w(x1,y1,x2,y2);
}
int main(){
scanf("%d",&t);
while(t--){
ll n,m,x1,x2,x3,x4,y1,y2,y3,y4;
cin>>n>>m>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
ll sub_x1 = max(x1,x3),sub_y1=max(y1,y3),sub_x2=min(x2,x4),sub_y2=min(y2,y4);
ll num_w = w(n,m),num_b = n*m - num_w;
num_w+=sub_b(x1,y1,x2,y2);num_b-=sub_b(x1,y1,x2,y2);
num_b+=sub_w(x3,y3,x4,y4);num_w-=sub_w(x3,y3,x4,y4);
if(sub_x1<=sub_x2 && sub_y1<=sub_y2){
num_b+=sub_b(sub_x1,sub_y1,sub_x2,sub_y2);
num_w-=sub_b(sub_x1,sub_y1,sub_x2,sub_y2);
}
cout<<num_w<<" "<<num_b<<endl;
}
return ;
}
Codeforces Round #524 (Div. 2) C. Masha and two friends的更多相关文章
- Codeforces Round #524 (Div. 2) C. Masha and two friends(矩形相交)
C. Masha and two friends time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces Round #524 (Div. 2) C. Masha and two friends(思维+计算几何?)
传送门 https://www.cnblogs.com/violet-acmer/p/10146350.html 题意: 有一块 n*m 的棋盘,初始,黑白块相间排列,且左下角为白块. 给出两个区间[ ...
- Codeforces Round #524 (Div. 2) C. Masha and two friends 几何:判断矩形是否相交以及相交矩形坐标
题意 :给出一个初始的黑白相间的棋盘 有两个人 第一个人先用白色染一块矩形区域 第二个人再用黑色染一块矩形区域 问最后黑白格子各有多少个 思路:这题的关键在于求相交的矩形区间 给出一个矩形的左下和 ...
- Codeforces Round #524 (Div. 2) C. Masha and two friends 思路
题目:题目链接 思路:直接计数显然是不好处理的,但分情况讨论只要不写错这题是一定可以出的,但这样基本做完这个题就没时间做其他题了,但当时我就这么蠢的这样做了,比赛一个半小时的时候突然发现一个似乎可行的 ...
- Codeforces Round #524 (Div. 2)(前三题题解)
这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...
- Codeforces Round #524 (Div. 2) Masha and two friends矩形
题目 题意: 给一个n*m块大的黑白相间的矩形,在这个举行中操作,要先把第一个矩形(左下角坐标(x1,y2),右上角坐标(x2,y2)) 全部涂成白色,再把第二个矩形(左下角坐标(x3,y3), ...
- Codeforces Round #524 (Div. 2) F. Katya and Segments Sets(主席树)
https://codeforces.com/contest/1080/problem/F 题意 有k个区间,区间的种类有n种,有m个询问(n,m<=1e5,k<=3e5),每次询问a,b ...
- Codeforces Round #524 (Div. 2) E. Sonya and Matrix Beauty(字符串哈希,马拉车)
https://codeforces.com/contest/1080/problem/E 题意 有一个n*m(<=250)的字符矩阵,对于每个子矩阵的每一行可以任意交换字符的顺序,使得每一行每 ...
- Codeforces Round #524 (Div. 2) Solution
A. Petya and Origami Water. #include <bits/stdc++.h> using namespace std; #define ll long long ...
随机推荐
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- 关于cookie的一些学习笔记
0x00 发现自己对一些原理性的东西实在是太不了解 最近看了<cookie之困>记一下笔记 0x01 因为http是无状态的 所以需要cookie和session来保持http的会话状态和 ...
- 图表制作工具之ECharts
简介 ECharts,缩写来自Enterprise Charts,商业级数据图表,一个纯Javascript的图表库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9/10 ...
- 在编程的时候,NotePad++ 中闪烁的光标突然有竖着闪烁的编程蓝色下划线闪烁的--小技巧告诉你-费元星
当在写代码时出现的光标闪烁(横线闪烁) 在键盘上找 Insert ,按这个Insert就可以把横向闪烁光标( _ )修改成竖向闪烁光标样式( | ),横向光标会在你写代码的时候修改前面的代码,把光标移 ...
- 问题:docker pull 用户登陆tricky,Error response from daemon: unauthorized: incorrect username or password
问题描述: PS C:\WINDOWS\system32> docker pull rabbitmqUsing default tag: latest Please login prior to ...
- 【js笔记】数组那些事[0]
js中数组是一个特殊的对象,索引是它的属性,整数索引在内部被转化为字符串类型. 1 数组的创建 new关键字方法:var arr=new Array() var arr=new Array(10); ...
- ABP官方文档
https://aspnetboilerplate.com/Pages/Documents/Introduction
- Python简要标准库(5)
hashlib Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 基本的生成MD密匙的函数 import hashlib md5 = hashlib.md5() md5.up ...
- Android基本组件
①Activity和View负责与用户交互 ②Service通常位于后台,拥有独立的生命周期,为其他组件提供后台服务和监控其他组件运行状态 ③BroadcastReceiver广播消息接收器,类似事件 ...
- HDU 4729 An Easy Problem for Elfness(主席树)(2013 ACM/ICPC Asia Regional Chengdu Online)
Problem Description Pfctgeorge is totally a tall rich and handsome guy. He plans to build a huge wat ...