Nyoj 298 Phase Change point (Matrix high-speed power)

Source: Internet
Author: User
Tags acos cos in degrees value of pi

Change time limit for points: -Ms | Memory Limit:65535KB Difficulty:5
Descriptive narrative

There are no more than 10,000 points on a plane. The coordinates are known. Now it's possible to do the following for all points:

Translation a certain distance (M), the relative x-axis upside-down (x), relative to the y-axis flip (y), the coordinates are reduced or enlarged by a certain multiple (S), all points to the coordinates of the origin counter-clockwise rotation of a certain angle (R).

The number of operations is not more than 1 million times, the coordinates of all points are finally obtained.

Tip: Assuming that the value of pi is used in the program, it can be obtained with ACOS (-1.0).

Input
There's just one set of test data
The first line of the test data is two integer n,m, representing the number of points and the number of operations (n<=10000,m<=1000000)
The subsequent row has n logarithm pairs, and the first number of each pair represents the x-coordinate of a point, and the second number represents the y-coordinate. The absolute value of the initial coordinate size of these points is no more than 100.
The subsequent m lines, each representing an operation, are a character at the beginning of the line:
The first character, assuming M, represents a panning operation. The line will be followed by two digits x, Y. Indicates that all points are shifted by vector (x, y);
The first character is assumed to be X. It means that all points are flipped up and down relative to the x-axis;
The first character is assumed to be y, which means that all points are flipped left and right relative to the y-axis;
The first character is assumed to be s. Then it will be followed by a number p, which indicates that the coordinates are enlarged p times;
The first character is assumed to be r, then it is followed by a number a, which means that all points relative to the origin of coordinates counterclockwise rotate a certain angle a (in degrees)
Output
output two numbers per line. Represents the coordinates of a point (rounding the result to 1 digits after the decimal point, and outputting one decimal place)
The output order of the points should be consistent with the input order
Example input
2 51.0 2.0 2.0 3.0XYM 2.0 3.0S 2.0R 180
Example output
-2.0-2.00.0 0.0

Analysis: The hypothesis is modeled according to the description of the topic. Must be timed out. At this point we need to find a high-speed transformation method.


This allows you to calculate the last rectangle formed after the M-transform, and then multiply the coordinates of the point by the rectangle to find out the answer.

#include <cstdio> #include <cstring> #include <cmath> #define PI acos ( -1.0) struct Matrix {double mat[3]    [3];        Matrix () {memset (mat, 0, sizeof (MAT));    for (int i = 0; i < 3; i++) mat[i][i] = 1;        } Matrix Multi (Matrix A, Matrix B) {matrix res;                for (int i = 0, i < 3; i++) {for (int j = 0; J < 3; J + +) {Res.mat[i][j] = 0;                for (int k = 0; k < 3; k++) {res.mat[i][j] = Res.mat[i][j] + a.mat[i][k] * B.mat[k][j];    }}} return res;        } Matrix translation (Matrix A, double p, double q) {//pan up P units, shift Q units to the right matrix res;        RES.MAT[0][2] = p;        RES.MAT[1][2] = q;    Return Multi (res, A);        } Matrix scale (Matrix A, double p) {//zoom P times Matrix Res;        Res.mat[0][0] = res.mat[1][1] = p;    Return Multi (res, A);        } Matrix Turn_ud (Matrix A) {//axes flip The matrix res up or down; res.mat[1][1] =-1;    Return Multi (res, A);        } Matrix TURN_LR (Matrix A) {//axes flip the matrix res around;        Res.mat[0][0] =-1;    Return Multi (res, A);        } Matrix Rotate (Matrix A, double angle) {//rotate counterclockwise around Origin angle angle double rad = angle/180.0 * PI;        Matrix Res; Res.mat[0][0] = cos (RAD);        RES.MAT[0][1] =-sin (RAD); Res.mat[1][0] = sin (rad);        RES.MAT[1][1] = cos (RAD);    Return Multi (res, A); }};struct point {double X, y;}    P[10005];int Main () {int n, m;    Char op[5];    Double x, y;    scanf ("%d%d", &n, &m);    for (int i = 0; i < n; i++) scanf ("%lf%lf", &p[i].x, &AMP;P[I].Y);    Matrix A;        for (int i = 0; i < m; i++) {scanf ("%s", op);        if (op[0] = = ' X ') A = A.turn_ud (a);        else if (op[0] = = ' Y ') A = A.TURN_LR (a);            else if (op[0] = = ' M ') {scanf ("%lf%lf", &x, &y);        A = A.translation (A, x, y); } else if (op[0] = = ' S ') {scanf ("%lf", &x);        A = A.scale (A, x);            } else if (op[0] = = ' R ') {scanf ("%lf", &x);        A = A.rotate (A, x);        }} for (int i = 0; i < n; i++) {double xx = a.mat[0][0] * p[i].x + a.mat[0][1] * p[i].y + a.mat[0][2];        Double yy = a.mat[1][0] * p[i].x + a.mat[1][1] * p[i].y + a.mat[1][2];    printf ("%.1lf%.1lf\n", XX, yy); } return 0;}

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Nyoj 298 Phase Change point (Matrix high-speed power)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.