Submission #570531

# Submission time Handle Problem Language Result Execution time Memory
570531 2022-05-30T11:46:28 Z Sam_a17 Chessboard (IZhO18_chessboard) C++14
100 / 100
1783 ms 4564 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;

#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif

#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define blt __builtin_popcount

#define pb push_back
#define popf pop_front
#define popb pop_back

void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}

template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'

// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};

// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();

  if (str != "") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  }
}
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
vector<pair<pair< long long,  long long>, pair< long long,  long long>>> points;
long long ansi = 1e17;

vector<long long> get_divisors(long long n) {
  vector<long long> answ{1};
  for(long long i = 2; i * i <= n; i++) {
    if(n % i == 0) {
      answ.push_back(i);
      if(n / i != i) {
        answ.push_back(n / i);
      }
    }
  }
  return answ;
}

long long n, k;

void check(long long curr) {
  long long first = 0, second = 0;
  for(auto i: points) {
    long long x1 = i.first.first - 1, y1 = i.first.second - 1;
    long long x2 = i.second.first - 1, y2 = i.second.second - 1;

    // y1 -> y2

    long long block1 = y1 / curr, block2 = y2 / curr;

    if(block1 == block2) {
      long long toadd = y2 - y1 + 1;

      long long block11 = x1 / curr, block22 = x2 / curr;

      if(block11 == block22) {
        long long bazm = x2 - x1 + 1;
        if(block11 % 2 == block1 % 2) {
          first += bazm * toadd;
        } else {
          second += bazm * toadd;
        }
      }
      else {

        long long start = x1;
        while(start / curr == block11) {
          if(block11 % 2 == block1 % 2) {
            first += toadd;
          } else {
            second += toadd;
          }
          start++;
        }

        start = x2;

        while(start / curr == block22) {
          if(block22 % 2 == block1 % 2) {
            first += toadd;
          } else {
            second += toadd;
          }
          start--;
        }

        for(long long j = block11 + 1; j < block22; j++) {
          if(j % 2 == block1 % 2) {
            first += toadd * curr;
          } else {
            second += toadd * curr;
          }
        }
      }

    }
    else {
      long long block11 = x1 / curr, block22 = x2 / curr;

      if(block11 == block22) {
        long long toadd = x2 - x1 + 1;
        long long start = y1;

        while(start / curr == block1) {
          if(block11 % 2 == block1 % 2) {
            first += toadd;
          } else {
            second += toadd;
          }
          start++;
        }

        start = y2;
        while(start / curr == block2) {
          if(block11 % 2 == block2 % 2) {
            first += toadd;
          } else {
            second += toadd;
          }
          start--;
        }

        for(long long j = block1 + 1; j < block2; j++) {
          if(j % 2 == block11 % 2) {
            first += toadd * curr;
          } else {
            second += toadd * curr;
          }
        }
      }
      else {
        long long start = y1;
        long long needFirst = 0, needSecond = 0;
        while(start / curr == block1) {
          if(block11 % 2 == block1 % 2) {
            needFirst++;
          } else {
            needSecond++;
          }
          start++;
        }

        start = y2;
        while(start / curr == block2) {
          if(block11 % 2 == block2 % 2) {
            needFirst++;
          } else {
            needSecond++;
          }
          start--;
        }

        long long bazm = (curr - x1 % curr);
        long long bazm2 = (x2 % curr + 1);

        if(block11 % 2 == block22 % 2) {
          first += needFirst * (bazm + bazm2);
          second += needSecond * (bazm + bazm2);
        } else {
          first += needFirst * bazm;
          second += needSecond * bazm;
          first += needSecond * bazm2;
          second += needFirst * bazm2;
        }


        for(long long j = block1 + 1; j < block2; j++) {
          if(j % 2 == block11 % 2) {
            first += curr * bazm;
            needFirst += curr;
          } else {
            second += curr * bazm;
            needSecond += curr;
          }

          if(j % 2 == block22 % 2) {
            first += curr * bazm2;
          } else {
            second += curr * bazm2;
          }
        }

        //
        for(long long j = block11 + 1; j < block22; j++) {
          if(block11 % 2 == j % 2) {
            first += needFirst * curr;
            second += needSecond * curr;
          } else {
            second += needFirst * curr;
            first += needSecond * curr;
          }
        }
      }
    }
  }

  long long row = (n * n) / (curr * curr);
  long long fs = row / 2, ss = row / 2;
  if(row & 1) {
    fs++;
  }

  fs = fs * (curr * curr);
  ss = ss * (curr * curr);

  ansi = min(ansi, fs - first + second);
  ansi = min(ansi, ss - second + first);
}

void solve_() {

  scanf("%lld%lld", &n, &k);
  vector<long long> div = get_divisors(n);

  for(int i = 1; i <= k; i++) {
    long long x1, y1, x2, y2;
    scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2);
    points.push_back({{x1, y1}, {x2, y2}});
  }

  sort(all(points));

  for(auto i: div) {
    check(i);
  }

  printf("%lld\n", ansi);
}

int main() {
  setIO();

  auto solve = [&](int test_case)-> void {
    while(test_case--) {
      solve_();
    }
  };

  int test_cases = 1;
  solve(test_cases);

  return 0;
}

Compilation message

chessboard.cpp: In function 'void setIO(std::string)':
chessboard.cpp:63:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
chessboard.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
chessboard.cpp: In function 'void solve_()':
chessboard.cpp:258:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  258 |   scanf("%lld%lld", &n, &k);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~
chessboard.cpp:263:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  263 |     scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 2488 KB Output is correct
2 Correct 7 ms 1364 KB Output is correct
3 Correct 18 ms 2508 KB Output is correct
4 Correct 23 ms 2520 KB Output is correct
5 Correct 24 ms 2512 KB Output is correct
6 Correct 16 ms 2436 KB Output is correct
7 Correct 4 ms 600 KB Output is correct
8 Correct 17 ms 2512 KB Output is correct
9 Correct 44 ms 4540 KB Output is correct
10 Correct 23 ms 2404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 12 ms 1492 KB Output is correct
17 Correct 37 ms 4520 KB Output is correct
18 Correct 45 ms 4496 KB Output is correct
19 Correct 91 ms 4528 KB Output is correct
20 Correct 111 ms 4564 KB Output is correct
21 Correct 41 ms 4548 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 26 ms 2512 KB Output is correct
24 Correct 41 ms 4556 KB Output is correct
25 Correct 5 ms 856 KB Output is correct
26 Correct 27 ms 2512 KB Output is correct
27 Correct 35 ms 4556 KB Output is correct
28 Correct 44 ms 4492 KB Output is correct
29 Correct 14 ms 2472 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 2488 KB Output is correct
2 Correct 7 ms 1364 KB Output is correct
3 Correct 18 ms 2508 KB Output is correct
4 Correct 23 ms 2520 KB Output is correct
5 Correct 24 ms 2512 KB Output is correct
6 Correct 16 ms 2436 KB Output is correct
7 Correct 4 ms 600 KB Output is correct
8 Correct 17 ms 2512 KB Output is correct
9 Correct 44 ms 4540 KB Output is correct
10 Correct 23 ms 2404 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 12 ms 1492 KB Output is correct
27 Correct 37 ms 4520 KB Output is correct
28 Correct 45 ms 4496 KB Output is correct
29 Correct 91 ms 4528 KB Output is correct
30 Correct 111 ms 4564 KB Output is correct
31 Correct 41 ms 4548 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 26 ms 2512 KB Output is correct
34 Correct 41 ms 4556 KB Output is correct
35 Correct 5 ms 856 KB Output is correct
36 Correct 27 ms 2512 KB Output is correct
37 Correct 35 ms 4556 KB Output is correct
38 Correct 44 ms 4492 KB Output is correct
39 Correct 14 ms 2472 KB Output is correct
40 Correct 2 ms 468 KB Output is correct
41 Correct 88 ms 4556 KB Output is correct
42 Correct 51 ms 4492 KB Output is correct
43 Correct 61 ms 4532 KB Output is correct
44 Correct 46 ms 4524 KB Output is correct
45 Correct 43 ms 4540 KB Output is correct
46 Correct 102 ms 4552 KB Output is correct
47 Correct 38 ms 4496 KB Output is correct
48 Correct 53 ms 4508 KB Output is correct
49 Correct 42 ms 4524 KB Output is correct
50 Correct 354 ms 4544 KB Output is correct
51 Correct 378 ms 4564 KB Output is correct
52 Correct 352 ms 4476 KB Output is correct
53 Correct 380 ms 4556 KB Output is correct
54 Correct 351 ms 4552 KB Output is correct
55 Correct 399 ms 4508 KB Output is correct
56 Correct 338 ms 4472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 27 ms 2488 KB Output is correct
10 Correct 7 ms 1364 KB Output is correct
11 Correct 18 ms 2508 KB Output is correct
12 Correct 23 ms 2520 KB Output is correct
13 Correct 24 ms 2512 KB Output is correct
14 Correct 16 ms 2436 KB Output is correct
15 Correct 4 ms 600 KB Output is correct
16 Correct 17 ms 2512 KB Output is correct
17 Correct 44 ms 4540 KB Output is correct
18 Correct 23 ms 2404 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 12 ms 1492 KB Output is correct
35 Correct 37 ms 4520 KB Output is correct
36 Correct 45 ms 4496 KB Output is correct
37 Correct 91 ms 4528 KB Output is correct
38 Correct 111 ms 4564 KB Output is correct
39 Correct 41 ms 4548 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 26 ms 2512 KB Output is correct
42 Correct 41 ms 4556 KB Output is correct
43 Correct 5 ms 856 KB Output is correct
44 Correct 27 ms 2512 KB Output is correct
45 Correct 35 ms 4556 KB Output is correct
46 Correct 44 ms 4492 KB Output is correct
47 Correct 14 ms 2472 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 88 ms 4556 KB Output is correct
50 Correct 51 ms 4492 KB Output is correct
51 Correct 61 ms 4532 KB Output is correct
52 Correct 46 ms 4524 KB Output is correct
53 Correct 43 ms 4540 KB Output is correct
54 Correct 102 ms 4552 KB Output is correct
55 Correct 38 ms 4496 KB Output is correct
56 Correct 53 ms 4508 KB Output is correct
57 Correct 42 ms 4524 KB Output is correct
58 Correct 354 ms 4544 KB Output is correct
59 Correct 378 ms 4564 KB Output is correct
60 Correct 352 ms 4476 KB Output is correct
61 Correct 380 ms 4556 KB Output is correct
62 Correct 351 ms 4552 KB Output is correct
63 Correct 399 ms 4508 KB Output is correct
64 Correct 338 ms 4472 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 0 ms 212 KB Output is correct
67 Correct 1727 ms 4476 KB Output is correct
68 Correct 1572 ms 4480 KB Output is correct
69 Correct 1300 ms 4548 KB Output is correct
70 Correct 1271 ms 4456 KB Output is correct
71 Correct 1465 ms 4532 KB Output is correct
72 Correct 1496 ms 4512 KB Output is correct
73 Correct 1550 ms 4552 KB Output is correct
74 Correct 1641 ms 4528 KB Output is correct
75 Correct 1577 ms 4556 KB Output is correct
76 Correct 1563 ms 4564 KB Output is correct
77 Correct 233 ms 4444 KB Output is correct
78 Correct 81 ms 4536 KB Output is correct
79 Correct 169 ms 4548 KB Output is correct
80 Correct 177 ms 4564 KB Output is correct
81 Correct 177 ms 4544 KB Output is correct
82 Correct 124 ms 4476 KB Output is correct
83 Correct 82 ms 4464 KB Output is correct
84 Correct 1024 ms 4556 KB Output is correct
85 Correct 1783 ms 4556 KB Output is correct
86 Correct 22 ms 212 KB Output is correct
87 Correct 7 ms 324 KB Output is correct
88 Correct 1735 ms 4556 KB Output is correct
89 Correct 506 ms 1492 KB Output is correct
90 Correct 5 ms 212 KB Output is correct