CF-1009-F. Counting Necessary Nodes · znzryb
## 思路讲解
那个公式的意思其实就是只有0,2,4,8,。。才能有边长为2的正方形。
0,4,8,12,。。。才能有边长为4的正方形。
图中的1,2,3,4对应加粗斜体的dfs部分。

```cpp
ROF(i,log2(b-a),0){
ll b_=b/pow2[i]*pow2[i],a_=ceil(a*1.0/pow2[i])*pow2[i];
ll d_=d/pow2[i]*pow2[i],c_=ceil(c*1.0/pow2[i])*pow2[i];
if(b_<=a) continue;
if(a_>=b) continue;
if(d_<=c) continue;
if(c_>=d) continue;
if(a_+pow2[i]>b_) continue;
if(c_+pow2[i]>d_) continue;
ans+=abs(a_-b_)/pow2[i]*(abs(c_-d_)/pow2[i]);
#ifdef LOCAL
debug(a_)
debug(b_)
debug(c_)
debug(d_)
debug(pow2[i])
debug(ans)
cerr<<"\n";
#endif
dfs(a,a_,c,d);
dfs(b_,b,c,d);
dfs(a_,b_,c,c_);
dfs(a_,b_,d_,d);
return;
}
```
## AC代码
[https://codeforces.com/contest/2074/submission/319087712](https://codeforces.com/contest/2074/submission/319087712)
::: 源代码
```cpp
// Problem: F. Counting Necessary Nodes
// Contest: Codeforces - Codeforces Round 1009 (Div. 3)
// URL: https://codeforces.com/contest/2074/problem/F
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define getFro(vec) (vec.empty()?0:vec.front())
#define getBac(vec) (vec.empty()?0:vec.front())
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define DEBUG(variable) \
do { \
std::cerr << #variable << ":"; \
for (const auto& elem : variable) { \
std::cerr << elem << " "; \
} \
std::cerr << "\n"; \
} while (0)
#define uniVec(var) \
do { \
sort(var.begin(),var.end());\
var.resize(unique(var.begin(),var.end())-var.begin());\
} while (0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
typedef pair<ll,bool> plb;
constexpr ll MAXN=static_cast<ll>(1e6)+10,INF=static_cast<ll>(1e18)+3;
constexpr ll mod=static_cast<ll>(1e9)+7;
constexpr double eps=1e-8;
ll N,M,K,T,A[MAXN];
/*
那个公式的意思其实就是只有0,2,4,8,。。才能有边长为2的正方形。
0,4,8,12,。。。才能有边长为4的正方形
去分是不大可能的,也不大可能是数学方法。
我认为其实就是
*/
ll ans=0;
ll pow2[40];
void dfs(ll a,ll b,ll c,ll d){
if(a>=b || c>=d) return;
// #ifdef LOCAL
// debug(a)debug(b)debug(c)debug(d)
// #endif
if(b-a>d-c){
swap(a,c);
swap(b,d);
}
#ifdef LOCAL
debug(a)debug(b)debug(c)debug(d)
#endif
ROF(i,log2(b-a),0){
ll b_=b/pow2[i]*pow2[i],a_=ceil(a*1.0/pow2[i])*pow2[i];
ll d_=d/pow2[i]*pow2[i],c_=ceil(c*1.0/pow2[i])*pow2[i];
if(b_<=a) continue;
if(a_>=b) continue;
if(d_<=c) continue;
if(c_>=d) continue;
if(a_+pow2[i]>b_) continue;
if(c_+pow2[i]>d_) continue;
ans+=abs(a_-b_)/pow2[i]*(abs(c_-d_)/pow2[i]);
#ifdef LOCAL
debug(a_)
debug(b_)
debug(c_)
debug(d_)
debug(pow2[i])
debug(ans)
cerr<<"\n";
#endif
dfs(a,a_,c,d);
dfs(b_,b,c,d);
dfs(a_,b_,c,c_);
dfs(a_,b_,d_,d);
return;
}
}
inline void solve(){
ll a,b,c,d;
cin>>a>>b>>c>>d;
ans=0;
dfs(a,b,c,d);
cout<<ans<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
pow2[0]=1;
FOR(i,1,35){
pow2[i]=2*pow2[i-1];
}
while(T--){
solve();
#ifdef LOCAL
cerr <<"-------------"<< '\n';
#endif
}
// solve();
return 0;
}
/*
AC
https://codeforces.com/contest/2074/submission/319087712
*/
```
:::
## 心路历程(WA,TLE,MLE……)
直接模拟4叉树,TLE
[https://codeforces.com/contest/2074/submission/319043232](https://codeforces.com/contest/2074/submission/319043232)
::: 源代码
```cpp
// Problem: F. Counting Necessary Nodes
// Contest: Codeforces - Codeforces Round 1009 (Div. 3)
// URL: https://codeforces.com/contest/2074/problem/F
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define getFro(vec) (vec.empty()?0:vec.front())
#define getBac(vec) (vec.empty()?0:vec.front())
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define DEBUG(variable) \
do { \
std::cerr << #variable << ":"; \
for (const auto& elem : variable) { \
std::cerr << elem << " "; \
} \
std::cerr << "\n"; \
} while (0)
#define uniVec(var) \
do { \
sort(var.begin(),var.end());\
var.resize(unique(var.begin(),var.end())-var.begin());\
} while (0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
typedef pair<ll,bool> plb;
constexpr ll MAXN=static_cast<ll>(1e6)+10,INF=static_cast<ll>(1e18)+3;
constexpr ll mod=static_cast<ll>(1e9)+7;
constexpr double eps=1e-8;
ll N,M,K,T,A[MAXN];
/*
那个公式的意思其实就是只有0,2,4,8,。。才能有边长为2的正方形。
0,4,8,12,。。。才能有边长为4的正方形
去分是不大可能的,也不大可能是数学方法。
我认为其实就是
*/
ll ans=0;
ll pow2[40];
void dfs(ll a,ll b,ll c,ll d,ll a1,ll b1,ll c1,ll d1){ // 模拟4叉树的这个过程就行
if(a>=b || c>=d) return; // 出现了不合法的表达形式,退出
if(b<=a1 || a>=b1) return; // 不可能情况退出
if(c>=d1 || d<=c1) return;
ll midAb=(a1+b1)/2,midCd=(c1+d1)/2;
if(a==a1 && b==b1 && c==c1 && d==d1){
++ans;
#ifdef LOCAL
debug(a)debug(b)debug(c)debug(d);
cerr<<"\n";
#endif
return;
}
// if(a>= midAb && c>=midCd){
// if(b<=b1 && d<=d1){
// dfs(a,b,c,d,midAb,b1,midCd,d1);
// }else if(b>b1 && d<=d1){
// dfs(a,b1,c,d,midAb,b1,midCd,d1);
// }
//
// }else if(a>=midAb && d<=midCd){
// dfs(a,b,c,d,midAb,b1,c1,midCd);
// }else if(b<=midAb && c>=midCd){
// dfs(a,b,c,d,a1,midAb,midCd,d1);
// }else if(b<=midAb && d<=midCd){
// dfs(a,b,c,d,a1,midAb,c1,midCd);
// }
dfs(a,min(b,midAb),c,min(d,midCd),a1,midAb,c1,midCd);
dfs(max(midAb,a),b,c,min(d,midCd),midAb,b1,c1,midCd);
dfs(a,min(b,midAb),max(midCd,c),d,a1,midAb,midCd,d1);
dfs(max(midAb,a),b,max(midCd,c),d,midAb,b1,midCd,d1);
}
inline void solve(){
ll a,b,c,d;
cin>>a>>b>>c>>d;
ans=0;
ll idx=ceil(log2(max(b,d)));
dfs(a,b,c,d,0,pow2[idx],0,pow2[idx]); // 1048576 为2的20次方
cout<<ans<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
pow2[0]=1;
FOR(i,1,35){
pow2[i]=2*pow2[i-1];
}
while(T--){
solve();
#ifdef LOCAL
cerr <<"-------------"<< '\n';
#endif
}
// solve();
return 0;
}
/*
*/
```
:::