Submission #402584

# Submission time Handle Problem Language Result Execution time Memory
402584 2021-05-12T02:20:08 Z rama_pang Nicelines (RMI20_nicelines) C++17
100 / 100
7 ms 444 KB
#include "nice_lines.h"

#include <bits/stdc++.h>
using namespace std;

using lint = long long;
using ldouble = long double;
using point = pair<lint, ldouble>;

const ldouble EPS = 1e-9;
const lint INF = 2.1e8;
const lint MAXV = 1e4;
const lint BASE = 2e4 + 1;

class Line {
 public:
  ldouble a, b;
  ldouble get(ldouble x) { return a * x + b; }

  Line(ldouble a, ldouble b) : a(a), b(b) {}
  Line(ldouble x1, ldouble y1, ldouble x2, ldouble y2) {
    a = (y1 - y2) / (x1 - x2);
    b = y1 - a * x1;
  }
};

void solve(int subtask_id, int N) {
  int query_count = 0;
  map<lint, ldouble> memo;
  const auto Query = [&](lint y) {
    if (memo.count(y)) return memo[y];
    query_count++;
    return memo[y] = query(BASE, ldouble(y) / 2);
  };

  const auto IntersectionX = [&](Line l1, Line l2) {
    // a1 * x + b1 = a2 * x + b2
    // x = (b2 - b1) / (a1 - a2)
    return (l2.b - l1.b) / (l1.a - l2.a);
  };

  const auto OneLine = [&](lint x_1, lint x_2, lint x_3) {
    if (x_1 == x_2 || x_1 == x_3 || x_2 == x_3) return true;
    ldouble y_1 = Query(x_1), y_2 = Query(x_2), y_3 = Query(x_3);
    return abs((y_2 - y_1) / (x_2 - x_1) - (y_3 - y_2) / (x_3 - x_2)) < EPS;
  };

  set<lint> changing_points;
  const auto Solve = [&](const auto &self, lint lft1, lint lft2, lint rgt1, lint rgt2) {
    Line lftLine(lft1, Query(lft1), lft2, Query(lft2));
    Line rgtLine(rgt1, Query(rgt1), rgt2, Query(rgt2));
    if (abs(lftLine.a - rgtLine.a) < EPS) return;
    lint midX = round(IntersectionX(lftLine, rgtLine));
    if (OneLine(lft1, lft2, midX) && OneLine(midX, rgt1, rgt2)) {
      changing_points.emplace(midX);
    } else {
      self(self, lft1, lft2, midX - 1, midX);
      self(self, midX - 1, midX, rgt1, rgt2);
    }
  };

  Solve(Solve, 2 * (-INF), 2 * (-INF + BASE), 2 * (+INF), 2 * (+INF + BASE));
  assert(changing_points.size() == N);

  vector<int> answerA, answerB;
  for (auto y : changing_points) {
    assert(y % 2 == 0); y /= 2;

    // y = ax + b
    // x = BASE
    // b = y % BASE

    lint b = y % BASE;
    while (b < -MAXV) b += BASE;
    while (b > +MAXV) b -= BASE;

    assert((y - b) % BASE == 0);
    lint a = (y - b) / BASE;

    assert(-MAXV <= a && a <= +MAXV);
    assert(-MAXV <= b && b <= +MAXV);

    answerA.emplace_back(int(a));
    answerB.emplace_back(int(b));
  }

  return the_lines_are(answerA, answerB);
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from nicelines.cpp:3:
nicelines.cpp: In function 'void solve(int, int)':
nicelines.cpp:63:33: warning: comparison of integer expressions of different signedness: 'std::set<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   63 |   assert(changing_points.size() == N);
      |          ~~~~~~~~~~~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 328 KB Output is correct
2 Correct 7 ms 328 KB Output is correct
3 Correct 6 ms 328 KB Output is correct
4 Correct 7 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 292 KB Output is correct
2 Correct 3 ms 200 KB Output is correct
3 Correct 2 ms 200 KB Output is correct
4 Correct 2 ms 200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 328 KB Output is correct
2 Correct 7 ms 328 KB Output is correct
3 Correct 6 ms 328 KB Output is correct
4 Correct 7 ms 328 KB Output is correct
5 Correct 4 ms 292 KB Output is correct
6 Correct 3 ms 200 KB Output is correct
7 Correct 2 ms 200 KB Output is correct
8 Correct 2 ms 200 KB Output is correct
9 Correct 6 ms 328 KB Output is correct
10 Correct 5 ms 332 KB Output is correct
11 Correct 6 ms 444 KB Output is correct
12 Correct 4 ms 288 KB Output is correct