답안 #1108649

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1108649 2024-11-04T17:48:01 Z Ludissey 모자이크 (IOI24_mosaic) C++17
100 / 100
135 ms 45672 KB
#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=diapref[r];
  if(l>0) p-=diapref[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+1);
    else{
      int dp=diapref[todia(l,b)-lng-1]-diapref[todia(r,t)+lng];
      sm+=(dp)*(lng+1);
    }
    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 504 KB Output is correct
4 Correct 0 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 388 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 500 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 504 KB Output is correct
4 Correct 0 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 388 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 500 KB Output is correct
11 Correct 1 ms 504 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 107 ms 41268 KB Output is correct
2 Correct 102 ms 39468 KB Output is correct
3 Correct 113 ms 39468 KB Output is correct
4 Correct 105 ms 39420 KB Output is correct
5 Correct 79 ms 23868 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 504 KB Output is correct
4 Correct 0 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 388 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 500 KB Output is correct
11 Correct 1 ms 504 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 76 ms 12688 KB Output is correct
19 Correct 63 ms 12620 KB Output is correct
20 Correct 63 ms 12360 KB Output is correct
21 Correct 64 ms 12300 KB Output is correct
22 Correct 63 ms 12360 KB Output is correct
23 Correct 66 ms 11848 KB Output is correct
24 Correct 79 ms 12104 KB Output is correct
25 Correct 63 ms 12104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 8776 KB Output is correct
2 Correct 126 ms 40204 KB Output is correct
3 Correct 113 ms 44588 KB Output is correct
4 Correct 112 ms 44588 KB Output is correct
5 Correct 111 ms 44844 KB Output is correct
6 Correct 115 ms 45420 KB Output is correct
7 Correct 113 ms 45368 KB Output is correct
8 Correct 113 ms 45356 KB Output is correct
9 Correct 93 ms 28648 KB Output is correct
10 Correct 93 ms 28484 KB Output is correct
11 Correct 88 ms 28748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 112 ms 39416 KB Output is correct
2 Correct 121 ms 45200 KB Output is correct
3 Correct 113 ms 43820 KB Output is correct
4 Correct 135 ms 43820 KB Output is correct
5 Correct 109 ms 45672 KB Output is correct
6 Correct 85 ms 28984 KB Output is correct
7 Correct 78 ms 21896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 107 ms 41268 KB Output is correct
2 Correct 102 ms 39468 KB Output is correct
3 Correct 113 ms 39468 KB Output is correct
4 Correct 105 ms 39420 KB Output is correct
5 Correct 79 ms 23868 KB Output is correct
6 Correct 112 ms 39416 KB Output is correct
7 Correct 121 ms 45200 KB Output is correct
8 Correct 113 ms 43820 KB Output is correct
9 Correct 135 ms 43820 KB Output is correct
10 Correct 109 ms 45672 KB Output is correct
11 Correct 85 ms 28984 KB Output is correct
12 Correct 78 ms 21896 KB Output is correct
13 Correct 125 ms 45356 KB Output is correct
14 Correct 110 ms 45356 KB Output is correct
15 Correct 109 ms 45604 KB Output is correct
16 Correct 110 ms 43828 KB Output is correct
17 Correct 111 ms 44844 KB Output is correct
18 Correct 113 ms 45356 KB Output is correct
19 Correct 83 ms 25320 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 504 KB Output is correct
5 Correct 0 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 388 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 500 KB Output is correct
12 Correct 1 ms 504 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 107 ms 41268 KB Output is correct
20 Correct 102 ms 39468 KB Output is correct
21 Correct 113 ms 39468 KB Output is correct
22 Correct 105 ms 39420 KB Output is correct
23 Correct 79 ms 23868 KB Output is correct
24 Correct 76 ms 12688 KB Output is correct
25 Correct 63 ms 12620 KB Output is correct
26 Correct 63 ms 12360 KB Output is correct
27 Correct 64 ms 12300 KB Output is correct
28 Correct 63 ms 12360 KB Output is correct
29 Correct 66 ms 11848 KB Output is correct
30 Correct 79 ms 12104 KB Output is correct
31 Correct 63 ms 12104 KB Output is correct
32 Correct 55 ms 8776 KB Output is correct
33 Correct 126 ms 40204 KB Output is correct
34 Correct 113 ms 44588 KB Output is correct
35 Correct 112 ms 44588 KB Output is correct
36 Correct 111 ms 44844 KB Output is correct
37 Correct 115 ms 45420 KB Output is correct
38 Correct 113 ms 45368 KB Output is correct
39 Correct 113 ms 45356 KB Output is correct
40 Correct 93 ms 28648 KB Output is correct
41 Correct 93 ms 28484 KB Output is correct
42 Correct 88 ms 28748 KB Output is correct
43 Correct 112 ms 39416 KB Output is correct
44 Correct 121 ms 45200 KB Output is correct
45 Correct 113 ms 43820 KB Output is correct
46 Correct 135 ms 43820 KB Output is correct
47 Correct 109 ms 45672 KB Output is correct
48 Correct 85 ms 28984 KB Output is correct
49 Correct 78 ms 21896 KB Output is correct
50 Correct 125 ms 45356 KB Output is correct
51 Correct 110 ms 45356 KB Output is correct
52 Correct 109 ms 45604 KB Output is correct
53 Correct 110 ms 43828 KB Output is correct
54 Correct 111 ms 44844 KB Output is correct
55 Correct 113 ms 45356 KB Output is correct
56 Correct 83 ms 25320 KB Output is correct
57 Correct 118 ms 45416 KB Output is correct
58 Correct 115 ms 44780 KB Output is correct
59 Correct 126 ms 45376 KB Output is correct
60 Correct 116 ms 44580 KB Output is correct
61 Correct 113 ms 44648 KB Output is correct
62 Correct 118 ms 44908 KB Output is correct
63 Correct 90 ms 28728 KB Output is correct
64 Correct 81 ms 20916 KB Output is correct