#include "mosaic.h"
#include <bits/stdc++.h>
#define int long long
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
int q,n;
vector<vector<int>> lft;
vector<vector<int>> top;
vector<int> dia;
vector<int> diapref;
vector<int> diaSpref;
vector<int> diaSprefINV;
// X cest le top
// Y cest la gauche
// [le x][le y]
int todia(int x, int y){
return (n-x-1)+y;
}
int prefL(int l, int r, int t ,int b){
if(l>r||t>b) return 0;
int sm=lft[r][b];
if(l>0) sm-=lft[l-1][b];
if(t>0) sm-=lft[r][t-1];
if(l>0&&t>0) sm+=lft[l-1][t-1];
return sm;
}
int prefT(int l, int r, int t ,int b){
if(l>r||t>b) return 0;
int sm=top[r][b];
if(l>0) sm-=top[l-1][b];
if(t>0) sm-=top[r][t-1];
if(l>0&&t>0) sm+=top[l-1][t-1];
return sm;
}
int SprefINV(int l, int r){
int sm=diaSprefINV[l];
if(r<sz(dia)-1) sm-=diaSprefINV[r+1];
int p=diaSprefINV[r];
if(l>0) p-=diaSprefINV[l-1];
sm-=p*(sz(dia)-(r+1));
return sm;
}
int Spref(int l, int r){
int sm=diaSpref[r];
if(l>0) sm-=diaSpref[l-1];
int p=diapref[r];
if(l>0) p-=diapref[l-1];
sm-=p*l;
return sm;
}
std::vector<long long> mosaic(std::vector<signed> X, std::vector<signed> Y, std::vector<signed> T, std::vector<signed> B,std::vector<signed> L, std::vector<signed> R) {
q = sz(T); n=sz(Y);
lft.resize(3,vector<int>(n,0));
top.resize(n,vector<int>(3,0));
dia.resize(n+n-1,0);
diaSprefINV.resize(n+n+4,0);
diaSpref.resize(n+n+4,0);
diapref.resize(n+n+4,0);
for (int i = 0; i < n; i++)
{
lft[0][i]=Y[i];
top[i][0]=X[i];
}
if(n>1){
lft[1][0]=X[1];
top[0][1]=Y[1];
if(n>2){
lft[2][0]=X[2];
top[0][2]=Y[2];
}
}
for (int i = 1; i < n&&n>1; i++)
{
top[i][1]=(top[i-1][1]==0&&top[i][0]==0);
lft[1][i]=(lft[1][i-1]==0&&lft[0][i]==0);
}
for (int i = 1; i < n&&n>2; i++)
{
top[i][2]=(top[i-1][2]==0&&top[i][1]==0);
lft[2][i]=(lft[2][i-1]==0&&lft[1][i]==0);
dia[todia(i,2)]=top[i][2];
dia[todia(2,i)]=lft[2][i];
}
for (int i = 0; i < n; i++){
for (int j = 0; j < min(n,3LL); j++)
{
if(j>0) top[i][j]+=top[i][j-1];
if(i>0) top[i][j]+=top[i-1][j];
if(i>0&&j>0) top[i][j]-=top[i-1][j-1];
if(j>0) lft[j][i]+=lft[j-1][i];
if(i>0) lft[j][i]+=lft[j][i-1];
if(i>0&&j>0) lft[j][i]-=lft[j-1][i-1];
}
}
for (int i = 0; i < sz(dia); i++)
{
diapref[i]=dia[i];
diaSpref[i]=dia[i]*(i+1);
if(i>0) diapref[i]+=diapref[i-1];
if(i>0) diaSpref[i]+=diaSpref[i-1];
// --
int j=(sz(dia)-(i+1));
diaSprefINV[j]=dia[j]*(sz(dia)-(j));
if(j<sz(dia)-1) diaSprefINV[j]+=diaSprefINV[j+1];
}
std::vector<long long> C(q, 0);
for (int i = 0; i < q; i++)
{
int _l=L[i],r=R[i],_t=T[i],b=B[i];
int l=max(3LL,_l);
int t=max(3LL,_t);
int sm=prefT(_l,r,_t,min(2LL,b));
sm+=prefL(_l,min(2LL,r),_t,b);
sm-=prefL(_l,min(2LL,r),_t,min(2LL,b));
if(l>r||t>b) { C[i]=sm; continue; }
int lng=min(r-l,b-t);
sm+=SprefINV(todia(l,b)-lng,todia(l,b));
sm+=Spref(todia(r,t),todia(r,t)+lng);
if((r-l)==(b-t)) sm-=dia[todia(l,t)]*lng;
else{
int dp=diapref[todia(l,b)-lng]-diapref[todia(r,t)+lng];
sm+=(dp)*lng;
}
C[i]=sm;
}
return C;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
508 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
508 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Incorrect |
1 ms |
504 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
40236 KB |
Output is correct |
2 |
Correct |
99 ms |
39420 KB |
Output is correct |
3 |
Correct |
99 ms |
39424 KB |
Output is correct |
4 |
Correct |
110 ms |
41772 KB |
Output is correct |
5 |
Correct |
85 ms |
25964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
508 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Incorrect |
1 ms |
504 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
50 ms |
8264 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
118 ms |
42572 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
40236 KB |
Output is correct |
2 |
Correct |
99 ms |
39420 KB |
Output is correct |
3 |
Correct |
99 ms |
39424 KB |
Output is correct |
4 |
Correct |
110 ms |
41772 KB |
Output is correct |
5 |
Correct |
85 ms |
25964 KB |
Output is correct |
6 |
Incorrect |
118 ms |
42572 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
508 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Incorrect |
1 ms |
504 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |