codeforces-1080C (2)

有了这两个,,我们就可以计算矩形(x , y)内了白色格子的数量了,,,
\[w(x , y) = \lceil{\frac n2}\rceil \cdot \lceil{\frac m2}\rceil + \lfloor{\frac n2}\rfloor \cdot \lfloor{\frac m2}\rfloor\]

向上取整的实现

这道题除了让我知道矩形交的处理,,,还有一个从好几个大佬的代码中我看到了几个好的求向上取整的代码,,,不像我那样傻傻的if判断(╬☉д⊙)

ll cdiv(ll a , llb) { return a / b + (a % b > 0); } //or ll cdiv(ll a , ll b) { return (a + b - 1) / b; } 代码 #include <bits/stdc++.h> using namespace std; typedef long long ll; //ceiling div ll cdiv(ll a , ll b) { return a / b + (a % b > 0); } ll ccdiv(ll a , ll b) { return (a + b - 1) / b; } ll w(ll x , ll y) { return cdiv(x , 2) * cdiv(y , 2) + (x / 2) * (y / 2); } ll wsum(ll x1 , ll y1 , ll x2 , ll y2) { return w(x2 , y2) - w(x1 - 1 , y2) - w(x2 , y1 - 1) + w(x1 - 1 , y1 - 1); } ll bsum(ll x1 , ll y1 , ll x2 , ll y2) { return (x2 - x1 + 1) * (y2 - y1 + 1) - wsum(x1 , y1 , x2 , y2); } int main() { freopen("233.txt" , "r" , stdin); ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); int t;cin >> t; while(t--) { ll n , m; cin >> n >> m; ll x1 , x2 , x3 , x4 , y1 , y2 , y3 , y4; cin >> x1 >> y1 >> x2 >> y2; cin >> x3 >> y3 >> x4 >> y4; ll w = wsum(1 , 1 , m , n); ll b = bsum(1 , 1 , m , n); //first rec w = w + bsum(x1 , y1 , x2 , y2); b = b - bsum(x1 , y1 , x2 , y2); //second rec(dont consider the itersection w = w - wsum(x3 , y3 , x4 , y4); b = b + wsum(x3 , y3 , x4 , y4); //consider the itersection if(max(x1 , x3) <= min(x2 , x4) && max(y1 , y3) <= min(y2 , y4)) { w = w - bsum(max(x1 , x3) , max(y1 , y3) , min(x2 , x4) , min(y2 , y4)); b = b + bsum(max(x1 , x3) , max(y1 , y3) , min(x2 , x4) , min(y2 , y4)); } cout << w << " " << b << endl; } } 小结

一直不怎么会的向上取整、矩形面积交等等问题算是了解了,,,至少不会在出现的时候啥都不知道,,只能从头分析,,,一个劲的堆if了(-`ェ´-╬)

cf真好玩.jpg,,,以为会第一发只能灰名,,没想到青了,,,就是深夜场太多,,,不然能天天打,,,,

这种代码不多的题锻炼锻炼思维很不错啊,,,毕竟现在纯套板子的题在各种比赛中是越来越少了,,,,

不知道这里的数学公式显示的如何,,,全是按原博客的规则写的啊QAQ
(end)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyxfjy.html