TLAPM - Editorial

I want to ask if we create the matrix with the values in the same order and then we calculate sum by moving down and then right. Is this approach right or wrong?

pre-computing function run total operation 10^3*10^3 = 10^6
and we have time limit of 0.5 mean we can run only 10^4 operation approx.
so why not we are getting tle by precomputing the matrix?

Have a look on setters code ant the way he had made the matrix , I think it could be made like that

vvll mat;

void precompute()

{

ll v = 1;

fo(i, 0, 2000)

{

    ll x = 0;

    ll y = i;

    while (x <= 2000 && y >= 0)

    {

        mat[x][y] = v++;

        x++;

        y--;

    }

}

}

void solve()

{

ll x1, x2, y1, y2, ans = 0;

cin >> x1 >> y1 >> x2 >> y2;

x1--, y1--, x2--, y2--;

fo(x, x1, x2)

{

    ans += mat[x][y1];

}

fo(y, y1 + 1, y2)

{

    ans += mat[x2][y];

}

cout << ans << ln;

}
what is wrong in this code?

https://www.codechef.com/viewsolution/46841152
why i am getting tle

Online judge does approx 10^8 operations in 1 second . So for 0.5 seconds it takes 5*10^7 ops which is more than our matrix precomputing function(10^6). That’s why we are not getting a TLE.

Can someone help me with my code

I precomputed matrix. When matrix size is a[1000][1000] answer is wrong but on a[1001][1000] it’s correct .How ?

Code–

#include<bits/stdc++.h>
#define ll unsigned long long int
#define mod 1000000007

using namespace std;

int main() {

int t;
cin>>t;

ll a[1000][1000];

a[0][0]=1;
ll d1=1,d2=1;
for(int i=0;i<1000;i++)
{
if(i>0){a[i][0] +=a[i-1][0]+d1;}

for(int j=1;j<1000;j++)
{
 a[i][j]  +=a[i][j-1]+d2;
 d2++;
}
d1++;
d2=d1;

}

while(t–)
{
int x1,y1,x2,y2;

cin>>x1>>y1>>x2>>y2;

ll val=0;
x1–; y1–;x2–;y2–;

for(int i=x1;i<=x2;i++)val +=a[i][y1];

for(int j=y1+1;j<=y2;j++)val +=a[x2][j];

cout<<val<<endl;

}

return 0;
}

Feedback for this problem-

You should have given the matrix or in the problem statement, it should be stated that how the matrix will be created. Yes, it’s quite clearly visible that elements are diagonally increasing, but how do we come to the conclusion that the rest of the values will have the same sequence?

This is not an aptitude problem, this is a programming problem, right?

If I am wrong in stating my feedback, please do correct me!

Thanks a lot…
In contest I was able to figure out logic but was creating arr size 1000

I know this question was easy, and if I had generated the matrix, it would have been much easier. I also used the same strategy, go down first, and then right. But maybe I didn’t calculated the value of grid correctly.

int val(int x, int y) {
int d = y * (y + 1) / 2;
int r = (x + 2 * (y - 1)) * (x - 1) / 2;
return r + d;
}

I don’t know why this isn’t working. This function was giving correct value for small x and y. Here y is depth and x is left-right distance. I generated this formula, and checked it many times. I am sure I calculated it correctly. Still, I get wrong answer on submission.

Please just look into this once, CodeChef: Practical coding for everyone

Even though I thought I should have used some different strategy, I just couldn’t bring myself to question what I did wrong in my calculation. Really, first problem can make or break your contest.

Your code fails in the following test cases (there are many such).

2 3 2 6
1 7 10 7
6 10 8 10
7 3 10 10
1 1 4 5
3 7 4 9
5 9 7 9
6 2 7 6
4 9 5 10
3 5 10 9

Edit: According to your algo, the way you’re reading input is wrong.
The following changes worked.

cin >> y1 >> x1 >> y2 >> x2;

My solution runtime 0.0 second and space complexity O(x+y)

#include
using namespace std;

int main() {
int t;
cin>>t;
while(t–)
{
int a,b,x,y;
cin>>a>>b>>x>>y;
int arr[x+y];
for(int i=a+b-1;i<=x+y-1;i++)
arr[i]=((i+1)*(i))/2;
int sum=0;
for(int i=a;i<=x;i++)
sum+=(arr[i+b-1]-b+1);
for(int j=b+1;j<=y;j++)
sum+=(arr[x+j-1]-j+1);
cout<<sum<<endl;

}
return 0;
}

https://www.codechef.com/viewsolution/46846761

To understand what is wrong with your code, you should try printing your matrix created from i = 990 to i = 1000. You need to pre - compute the values upto 2000, and not just 1000.
There is also another small error, if x1 == x2 and y1 == y2, the answer will be 2*arr[x1][y1], because we have to include both the starting point as well as the end point of the path.
Link to submission after making changes to your code : CodeChef: Practical coding for everyone
Hope I helped!

Thank you.

I just checked that x denoted the row, not column. Why, why, and I wasted so much time man.

Still buddy, thanks a lot.

I hope you will understand by this.

I used three different approaches for this quesion in python:
All three solutions are accepted:

(i)

for _ in range(int(input())):
    x1,y1,x2,y2 = map(int,input().split())
    x1-=1
    y1-=1
    x2-=1
    y2-=1
    summ = 1
    cnt = 2
    for i in range(x1):
        summ += cnt
        cnt += 1
    cnt -= 1
    for i in range(y1):
        summ += cnt
        cnt += 1
    cnt += 1
    ans = summ
    for i in range(x1+1,x2+1):
        summ += cnt
        ans += summ
        cnt += 1
    cnt -=1
    for i in range(y1+1,y2+1):
        summ += cnt
        ans += summ
        cnt += 1
    print(ans)

(ii)

arr = [[i+1 for i in range(1000)] for j in range(1000)]
for i in range(1,1000):
    arr[0][i] = arr[0][i-1] + (i)
for i in range(1,1000):
    for j in range(1000):
        arr[i][j] = arr[i-1][j] + (i+j+1)
for _ in range(int(input())):
    x1,y1,x2,y2 = map(int,input().split())
    x1-=1
    y1-=1
    x2-=1
    y2-=1
    summ = 0
    for i in range(x1,x2+1):
        summ += arr[i][y1]
    for i in range(y1+1,y2+1):
        summ += arr[x2][i]
    print(summ)

(iii)

dic = {n:((n*(n+1))//2) for n in range(10001)}
def find(x,y):
    global dic
    ans = 0
    ans += dic[x]
    ans += (dic[x+y-2] - dic[x-1])
    return ans
for _ in range(int(input())):
    x1,y1,x2,y2 = map(int,input().split())
    ans= 0
    for i in range(x1,x2+1):
        ans += find(i,y1)
    for i in range(y1+1,y2+1):
        ans += find(x2,i)
    print(ans)

Okkk got it

Why am I getting WA? I have pre-computed the array

`#include
using namespace std;

int main() {
int arr[2000][2000], i, j;
arr[0][0] = 1;

for(i=1; i<2000; i++){
    arr[i][0] = arr[i-1][0] + i;
}
for(j=1; j<2000; j++){
    arr[0][j] = arr[1][j-1] + 1;
    for(i=1; i<2000; i++){
        arr[i][j] = arr[i-1][j] + i + j; 
    }
}

int t, x1, y1, x2, y2, sum;
cin >> t;
while(t--){
    sum = 0;
    cin >> x1 >> y1 >> x2 >> y2;
    for(j=y1-1; j<y2; j++){
        sum += arr[x1-1][j];
    }
    for(i=x1; i<x2; i++){
        sum += arr[i][y2-1];
    }
    cout << sum << '\n';
}

return 0;

}
`

@yogendrasingh7 explained it already check it out