Codeforces Round #478 (Div. 2) E. Hag's Khashba

本文介绍了一位才华横溢的艺术家,他在父亲的压力下学习机械工程,却秘密地用一块巨大的木头雕刻成鹅。面对父亲的质问,他巧妙地将作品解释为力学项目,并邀请父亲进行互动实验,通过旋转木雕并稳定下来,展示其力学原理。
摘要由CSDN通过智能技术生成
E. Hag's Khashba
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering.

Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance.

Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with nn vertices.

Hag brought two pins and pinned the polygon with them in the 11-st and 22-nd vertices to the wall. His dad has qq queries to Hag of two types.

  • 1ftt: pull a pin from the vertex ff, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex tt.
  • 2vv: answer what are the coordinates of the vertex vv.

Please help Hag to answer his father's queries.

You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again.

Input

The first line contains two integers nn and qq (3n100003≤n≤10000, 1q2000001≤q≤200000) — the number of vertices in the polygon and the number of queries.

The next nn lines describe the wooden polygon, the ii-th line contains two integers xixi and yiyi (|xi|,|yi|108|xi|,|yi|≤108) — the coordinates of the ii-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct.

The next qq lines describe the queries, one per line. Each query starts with its type 11 or 22. Each query of the first type continues with two integers ff and tt (1f,tn1≤f,t≤n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex ff contains a pin. Each query of the second type continues with a single integer vv (1vn1≤v≤n) — the vertex the coordinates of which Hag should tell his father.

It is guaranteed that there is at least one query of the second type.

Output

The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10410−4.

Formally, let your answer be aa, and the jury's answer be bb. Your answer is considered correct if |ab|max(1,|b|)104|a−b|max(1,|b|)≤10−4

Examples
input
Copy
3 4
0 0
2 0
2 2
1 1 2
2 1
2 2
2 3
output
Copy
3.4142135624 -1.4142135624
2.0000000000 0.0000000000
0.5857864376 -1.4142135624
input
Copy
3 2
-1 1
0 0
1 1
1 1 2
2 1
output
Copy
1.0000000000 -1.0000000000
Note

In the first test note the initial and the final state of the wooden polygon.

Red Triangle is the initial state and the green one is the triangle after rotation around (2,0)(2,0).

In the second sample note that the polygon rotates 180180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.

思路:每个顶点相对顶点、重心的距离、顶点重心当前点构成的角都可以预处理出来。每次旋转你只需维护重心的位置和第一个顶点的位置,每次查询都可以通过第一个点、重心位置以及预处理的信息O(1)得到某个点的位置。

我用double精度不够,换成long double过的。

  1 #include <iostream>
  2 #include <fstream>
  3 #include <sstream>
  4 #include <cstdlib>
  5 #include <cstdio>
  6 #include <cmath>
  7 #include <string>
  8 #include <cstring>
  9 #include <algorithm>
 10 #include <queue>
 11 #include <stack>
 12 #include <vector>
 13 #include <set>
 14 #include <map>
 15 #include <list>
 16 #include <iomanip>
 17 #include <cctype>
 18 #include <cassert>
 19 #include <bitset>
 20 #include <ctime>
 21 
 22 using namespace std;
 23 
 24 #define pau system("pause")
 25 #define ll long long
 26 #define pii pair<int, int>
 27 #define pb push_back
 28 #define mp make_pair
 29 #define clr(a, x) memset(a, x, sizeof(a))
 30 
 31 const long double pi = acos(-1.0);
 32 const int INF = 0x3f3f3f3f;
 33 const int MOD = 1e9 + 7;
 34 const long double EPS = 1e-9;
 35 
 36 /*
 37 #include <ext/pb_ds/assoc_container.hpp>
 38 #include <ext/pb_ds/tree_policy.hpp>
 39 
 40 using namespace __gnu_pbds;
 41 tree<pli, null_type, greater<pli>, rb_tree_tag, tree_order_statistics_node_update> T;
 42 */
 43 
 44 int n, q;
 45 struct point {
 46     long double x, y;
 47     point() {}
 48     point (long double x, long double y) : x(x), y(y) {}
 49     void input() {
 50         //scanf("%lf%lf", &x, &y);
 51         cin >> x >> y;
 52     }
 53     void output() {
 54         double tx = x, ty = y;
 55         printf("%.12f %.12f\n", tx, ty);
 56     }
 57     point operator - (const point &p) const {
 58         return point(x - p.x, y - p.y);
 59     }
 60     point operator + (const point &p) const {
 61         return point(x + p.x, y + p.y);
 62     }
 63     long double operator ^ (const point &p) const {
 64         return x * p.y - y * p.x;
 65     }
 66     point operator / (const long double &k) const {
 67         return point(x / k, y / k);
 68     }
 69     point operator * (const long double &k) const {
 70         return point(x * k, y * k);
 71     }
 72     long double dis(const point &p) const {
 73         return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
 74     }
 75 } p[100015];
 76 long double d0[100015], d1[100015], th[100015];
 77 long double cal_th(point p1, point p0, point p2) {
 78     long double th1 = atan2(p1.y - p0.y, p1.x - p0.x);
 79     long double th2 = atan2(p2.y - p0.y, p2.x - p0.x);
 80     long double res = th1 - th2;
 81     if (res < 0) res += 2 * pi;
 82     return res;
 83 }
 84 multiset<int> rem;
 85 point get_pos(int index) {
 86     long double th1 = atan2(p[1].y - p[0].y, p[1].x - p[0].x);
 87     long double th2 = th1 - th[index];
 88     if (th2 < 0) th2 += 2 * pi;
 89     return p[0] + point(d0[index] * cos(th2), d0[index] * sin(th2));
 90 }
 91 void rot_and_update(int index2) {
 92     point p2 = get_pos(index2);
 93     long double th01 = atan2(p[0].y - p2.y, p[0].x - p2.x);
 94     long double th02 = 3 * pi / 2;
 95     long double th0 = th02 - th01;
 96     if (th0 < 0) th0 += 2 * pi;
 97     long double th11 = atan2(p[1].y - p2.y, p[1].x - p2.x);
 98     //p[0].output(), p[1].output(), p2.output();
 99     long double th12 = th11 + th0;
100     if (2 * pi <= th12) th12 -= 2 * pi;
101     //printf("index2 = %d\n", index2);
102     p[0] = point(p2.x, p2.y - d0[index2]);
103     p[1] = p2 + point(d1[index2] * cos(th12), d1[index2] * sin(th12));
104 }
105 void get_wp() {
106     long double s = 0;
107     p[0] = point(0, 0);
108     for (int i = 2; i < n; ++i) {
109         long double ts = (p[i] - p[1]) ^ (p[i] - p[i + 1]);
110         p[0] = p[0] + (p[1] + p[i] + p[i + 1]) / 3 * ts;
111         s += ts;
112     }
113     p[0] = p[0] / s;
114 }
115 int main() {
116     scanf("%d%d", &n, &q);
117     p[0] = point(0, 0);
118     for (int i = 1; i <= n; ++i) {
119         p[i].input();
120     }
121     get_wp();
122     for (int i = 1; i <= n; ++i) {
123         th[i] = cal_th(p[1], p[0], p[i]);
124         d0[i] = p[0].dis(p[i]);
125         d1[i] = p[1].dis(p[i]);
126     }
127     rem.insert(1), rem.insert(2);
128     while (q--) {
129         int op;
130         scanf("%d", &op);
131         if (1 == op) {
132             int f, t;
133             scanf("%d%d", &f, &t);
134             multiset<int>::iterator it = rem.lower_bound(f);
135             rem.erase(it);
136             int v = *rem.begin();
137             rot_and_update(v);
138             rem.insert(t);
139         } else {
140             int v;
141             scanf("%d", &v);
142             get_pos(v).output();
143         }
144     }
145     return 0;
146 }
147 /*
148 10 10
149 0 -100000000
150 1 -100000000
151 1566 -99999999
152 2088 -99999997
153 2610 -99999994
154 3132 -99999990
155 3654 -99999985
156 4176 -99999979
157 4698 -99999972
158 5220 -99999964
159 1 2 5
160 2 1
161 1 1 7
162 2 5
163 1 5 4
164 1 4 2
165 2 8
166 1 7 9
167 2 1
168 1 2 10
169 */
View Code

 

转载于:https://www.cnblogs.com/BIGTOM/p/8979459.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值