Advertisement
CooBin

fcity_sol1

Oct 9th, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define maxn 1000
  3.  
  4. using namespace std;
  5.  
  6. double r[maxn][maxn];
  7.  
  8. double a, b, c;
  9. double k, l;
  10.  
  11. void InitPravac(int x1, int y1, int x2, int y2) {
  12.   a = (y2 - y1);
  13.   b = (x1 - x2);
  14.   double temp = sqrt(a * a + b * b);
  15.   a /= temp;
  16.   b /= temp;
  17.   c = -(a * x1 + b * y1);
  18.  
  19.   k = (double) (y2 - y1) / (x2 - x1);
  20.   l = y1 - k * x1;
  21. }
  22.  
  23. double GetY(int x) {
  24.   return k * x + l;
  25. }
  26.  
  27. double Dist(int x, int y) {
  28.   return fabs(a * x + b * y + c);
  29. }
  30.  
  31. int Count(int x, int y1, int y2) {
  32.   int ret = 0;
  33.   for (int y = y1; y <= y2; ++y) {
  34.     ret += r[x][y] > 0.0 && Dist(x, y) <= r[x][y];
  35.   }
  36.   return ret;
  37. }
  38.  
  39. int main() {
  40.   int n;
  41.   scanf("%d", &n);
  42.   for (int i = 0; i < n; ++i) {
  43.     int x, y;
  44.     double radius;
  45.     scanf("%d%d%lf", &x, &y, &radius); --x; --y;
  46.     r[x][y] = radius;
  47.   }
  48.  
  49.   int q;
  50.   scanf("%d", &q);
  51.   for (int i = 0; i < q; ++i) {
  52.     int x1, y1, x2, y2;
  53.     scanf("%d%d%d%d", &x1, &y1, &x2, &y2); --x1; --y1; --x2; --y2;
  54.     if (x1 > x2 || x1 == x2 && y1 > y2) {
  55.       swap(x1, x2);
  56.       swap(y1, y2);
  57.     }
  58.     int ret = 0;
  59.     if (x1 == x2) {
  60.       for (int y = y1; y <= y2; ++y) {
  61.         ret += r[x1][y] > 0.0;
  62.       }
  63.     } else if (y1 == y2) {
  64.       for (int x = x1; x <= x2; ++x) {
  65.         ret += r[x][y1] > 0.0;
  66.       }
  67.     } else {
  68.       InitPravac(x1, y1, x2, y2);
  69.       if (y2 > y1) {
  70.         int prev_y = y1;
  71.         int next_y = ceil(GetY(x1 + 1) - 1e-6);
  72.         ret += Count(x1, prev_y, next_y);
  73.         for (int x = x1 + 1; x < x2; ++x) {
  74.           int curr_y = next_y;
  75.           next_y = ceil(GetY(x + 1) - 1e-6);
  76.           ret += Count(x, prev_y, next_y);
  77.           prev_y = curr_y - 1;
  78.         }
  79.         ret += Count(x2, prev_y, next_y);
  80.       } else {
  81.         int prev_y = y1;
  82.         int next_y = floor(GetY(x1 + 1) + 1e-6);
  83.         ret += Count(x1, next_y, prev_y);
  84.         for (int x = x1 + 1; x < x2; ++x) {
  85.           int curr_y = next_y;
  86.           next_y = floor(GetY(x + 1) + 1e-6);
  87.           ret += Count(x, next_y, prev_y);
  88.           prev_y = curr_y + 1;
  89.         }
  90.         ret += Count(x2, next_y, prev_y);
  91.       }
  92.     }
  93.     printf("%d\n", ret);
  94.   }
  95.   return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement