#P15535. [CEOI 2005] Electric Fence

    ID: 15452 远端评测题 3000ms 64MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>2005交互题Special JudgeCEOI(中欧)

[CEOI 2005] Electric Fence

Description

Farmer G has a large pasture-field surrounded by an electric fence. The fence consists of fenceposts and straight-line segments of wires where each segment connects two neighboring posts. The fence is obviously not self-crossing, i.e. no wire-segment crosses any other wire-segment. Farmer G had been informed that a new straight-line road will be built that might cross his field. He went to the field and noticed that the two endpoints of the road have already been marked by two posts, aa and bb. He realized that the line of the road splits the interior part of his pasture-field into several disjoint regions.

Farmer G wants to determine how many regions will form on both sides of the road. He finds that none of the fence-posts lay on the line of the road. Moreover, if a wire-segment intersects with the line of the road, then the intersection lays between the endpoints aa and bb.

Unfortunately, Farmer G has no instrument to measure the distance of two posts. He can only observe the orientation of the posts, i.e. he can walk to any post pp (recall that road endpoints are also posts) and, looking towards post qq, he can see whether a third post rr stands on his left, or on his right, or whether the three posts are collinear. Fortunately, farmer G has his laptop with him (as usual), so he can do even complex computations.

Task

You are to write a program that computes the number of disjoint regions located on the left and on the right side of the planned road as the result of splitting the pasture-field by the road.

Library

To perform queries, you are given a library lookup\tt lookup with three operations:

  • GetN\tt GetN, to be called once at the beginning, without arguments; it returns NN, the number of fence-posts. GetN\tt GetN must be called before the first call to Drift\tt Drift.
  • Drift\tt Drift, to be called with three post labels as arguments. Drift(x,y,z)\tt Drift(x,y,z) returns 11 if post zz stands on the left when looking from xx towards post yy, it returns 1-1 if zz stands on the right, and it returns 00 if the three posts are collinear. The fence-posts are labeled by the numbers from 11 to NN, the road endpoints aa and bb are labeled by N+1N+1 and by N+2N+2, respectively. The wire-segments of the fence connect fence-posts labeled by ii and (imodN)+1(i \bmod N)+1. Drift\tt Drift returns 00, too, if at least two of its arguments are equal.
  • Answer\tt Answer, to be called once in the end; it reports the solution and it properly terminates the execution of your program. Answer\tt Answer has two integer arguments. The first and second arguments must be the number of disjoint regions located on the left and right side of the road, respectively. (Drift(a,b,p)\tt Drift(a,b,p) returns 11 or 1-1 if fence-post pp stands on the left or right side of the road, respectively.)

Your program is not allowed to read or write files. Input and output is handled by the library.

Instruction for Pascal programmers: include the import statement

uses lookup;

in your source code.

Instructions for C/C++ programmers: use the directive

#include "lookup.h"

in your source code, create a project file in the task directory, add the files fence.c (fence.cpp), lookup.h and lookup.o into this project, and then compile and/or make your program. (Using Dev-C++ IDE, choose the Project/Project Options/Files menu, select the file lookup.o, unset "include in compilation" and set "include in linking").

Command line compilation:

gcc/g++ -O2 –static –o fence fence.c lookup.o -lm

Experimentation

You are provided with a toolset that contains the libraries both for WinXP and Linux. You can download it as a zip archive. Copy the appropriate library files into your task directory.

The toolset includes a test generator testgener\tt testgener to produce the file fence.in containing valid random sample input. testgener\tt testgener needs an integer input parameter, NN, the approximate number of fence-posts. If N<300N < 300 then testgener\tt testgener also creates a postscript file fence.ps that visualizes the layout of the fence (you can view it using gsview or another postscript viewer). The generated test data is considerably different for even and odd NNs; try and see it! Warning: testgener\tt testgener can not generate all possible inputs.

The solution submitted by Answer\tt Answer will be written into the file fence.out.

You can also create your own input by creating a text file fence.in. The first line must contain four integers, the coordinates of the endpoints of the road. The second line must contain NN, the number of fence-posts. Each of the following NN lines must contain a pair of integers, x yx\ y (20 000x,y20 000-20\ 000 \le x, y \le 20\ 000); the pair in line i+2i+2 defines the coordinates of the fence-post labeled by ii.

Hint

Constraints

  • For the number of fence-posts NN, we have 3N100 0003 \le N \le 100\ 000.
  • FreePascal library file names: lookup.ppu and lookup.o for WinXP and lookup.o for Linux.
  • Pascal function declarations:
    function GetN: longint;
    function Drift(x, y, z: longint): integer;
    procedure Answer(x, y: longint);
    
  • C/C++ library file names: lookup.h, and lookup.o
  • C/C++ function declarations:
    long GetN(void);
    int Drift(long x, long y, long z);
    void Answer(long left, long right);