列表

详情


NC24670. Machine Learning

描述

"You know? AI is ruling the world!" Well, there seems no wrong with it as well if you change the word "ruling" into "ruining".

Anyway, to make Ramen's program better, he wants to add machine learning technique into his project. He is working on a picture generation project, and the first step of the process is to calculate the sum of some rectangle of a very special matrix. 

The matrix itself is easy to describe. Think about the first quadrant of a two-dimensional coordinate system, the value at the point (i,j) is:


For example, here is a part of the matrix:


Then, Ramen needs the sum of some rectangles. We are using (x1,y1)-(x2,y2) to describe a rectangle, means that the lower left corner's coordinate is (x1, y1) while the upper right corner is (x2, y2). For example, under some situation, he needs the sum of the rectangle below:


It's the rectangle (0,0)-(1,1). The sum of it is 3.

Ramen has written a very fast program to calculate the sum of a rectangle. But after downloading the training data set, he realizes that there is a severe problem: in his program, the origin is (0,0), but all of the training data's origin is (1,1).  He has no idea how to modify his program to suit the training data, can you help him to do it?

输入描述

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

说明:

Sample 1 is described in the statement. Because the origin is (1,1), the rectangle's coordinate is now (1,1)-(2,2).
For sample 2, the required rectangle is:
\begin{matrix}<br />0&0&0\\<br />3&3&3\\<br />2&2&3<br />\end{matrix}
It's obvious the sum of them is 3x4+2x2=16.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

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;
    }
}

上一题