NC24670. Machine Learning
描述
输入描述
The input contains multiple test cases.
The first line is an integer T(1 <= T <= 1000), which indicates the number of test cases. Then T lines follow.
Each of the next T lines contains four integers x1, x2, y1, y2 (1 <= x1 <= x2 <= 1e9, 1 <= y1 <= y2 <= 1e9), indicates the rectangle (x1,y1)-(x2,y2).
输出描述
For each test case, output Case #x: y, where x is the number of the test case (starts from 1) and y is the answer.
示例1
输入:
3 1 1 2 2 2 3 4 5 12 34 56 78
输出:
Case #1: 3 Case #2: 16 Case #3: 3039
说明:
C++14(g++5.4) 解法, 执行用时: 7ms, 内存消耗: 2852K, 提交时间: 2019-04-18 18:15:04
#include <bits/stdc++.h> using namespace std; typedef long long ll; vector<ll> q[100005]; ll deng(ll a1,ll d,ll n) { return (a1+(a1+(n-1)*d))*n/2; } ll jiao(ll y) { ll ans=0; if(y>=1) { ans+=deng(1,4,(y+3)/4); } if(y>=2) { ans+=deng(2*2,2*4,(y+2)/4); } if(y>=3) { ans+=deng(3*3,3*4,(y+1)/4); } ans=ans*2; ans+=(y/4)*6; for(int i=1;i<=y%4;i++) { ans+=i; } return ans; } ll solve(ll x,ll y) { if(x<0||y<0) return 0; ll ans=jiao(min(x,y)); int p1=min(x,y); int p2=max(x,y); //cout<<"p1="<<p1<<" "<<p2<<endl; if(p2>p1) { ans+=deng(0,0,p2/4-p1/4)*(min(x,y)+1); ans+=deng(1,0,(p2+3)/4-(p1+3)/4)*(min(x,y)+1); ans+=deng(2,0,(p2+2)/4-(p1+2)/4)*(min(x,y)+1); ans+=deng(3,0,(p2+1)/4-(p1+1)/4)*(min(x,y)+1); } //printf("%lld %lld = %lld\n",x,y,ans); return ans; } int main() {//cout<<solve(1,3)<<endl; /*for(int i=0;i<=30;i++) { printf("%2d ",i); for(int j=0;j<=30;j++) if(j<i)printf(" "); else printf("%d ",max(i,j)%4); printf("\n"); }*/ int t;scanf("%d",&t);int cas=1; while(t--) { ll x1,x2,y1,y2; scanf("%lld%lld",&x1,&y1); scanf("%lld%lld",&x2,&y2); x1--,y1--; x2--,y2--; ll ans=solve(x2,y2)-solve(x1-1,y2)-solve(x2,y1-1)+solve(x1-1,y1-1); printf("Case #%d: %lld\n",cas++,ans); } return 0; } /* 3 1 1 2 2 2 3 4 5 12 34 56 78 3 16 3039 */
C++11(clang++ 3.9) 解法, 执行用时: 7ms, 内存消耗: 484K, 提交时间: 2019-04-14 16:12:17
#include <bits/stdc++.h> using namespace std; long long solve(long long x, long long y){ if(x < 0 || y < 0) return 0; //cout << x << " " << y << ": "; if(x > y) swap(x, y); long long k = x/4, m = x % 4; long long ans = k * 10 + 24 * k * k; if(m >= 1) ans += 8 * k + 3; if(m >= 2) ans += (8 * k + 5) * 2; if(m >= 3) ans += (8 * k + 7) * 3; x++; long long p = (y/4 - x/4 - 1) * 6; for(long long i = x%4; i <= 3; i++) p += i; for(long long i = 0; i <= y%4; i++) p += i; return ans + p * x; } int main(){ //cout << solve(2, 2) << endl; long long T; cin >> T; for(long long t = 1; t <= T; t++){ long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--; x2--; y1--; y2--; long long ans = solve(x2, y2) - solve(x1-1, y2) - solve(x2, y1-1) + solve(x1-1, y1-1); cout << "Case #" << t << ": "; cout << ans << endl; } }