new file: P16055.cpp

This commit is contained in:
hanyixuanten committed 2026-10-14 19:48:25 +08:00
1 parent b600661bcc
commit c0f5d1b60d
3 files changed
+278

No files matched your search

+54
View File
@@ -0,0 +1,54 @@
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, double>> o;
double st1[200005][21], st2[200005][21];
int main() {
scanf("%d%d", &n, &m);
o.push_back({0, 0});
for (int i = 1; i <= n; ++i) {
int op;
double k;
scanf("%d%lf", &op, &k);
o.push_back({op, k});
if (op == 1) {
st1[i][0] = k;
st2[i][0] = 0;
} else {
st1[i][0] = cos(k);
st2[i][0] = sin(k);
}
}
for (int i = 1; i <= 20; i++) {
for (int j = 1; j + (1 << i) - 1 <= n; j++) {
double tmp11 = st1[j][i - 1], tmp12 = st2[j][i - 1];
double tmp21 = st1[j + (1 << (i - 1))][i - 1],
tmp22 = st2[j + (1 << (i - 1))][i - 1];
st1[j][i] = tmp11 * tmp21 - tmp12 * tmp22;
st2[j][i] = tmp11 * tmp22 + tmp12 * tmp21;
}
}
while (m--) {
int l, r, x, y;
scanf("%d%d%d%d", &l, &r, &x, &y);
double nx = x, ny = y;
r++;
for (int i = 20; i >= 0; i--) {
if (l + (1 << i) <= r) {
double tmpx = st1[l][i] * nx - st2[l][i] * ny,
tmpy = st2[l][i] * nx + st1[l][i] * ny;
nx = tmpx, ny = tmpy;
l += 1 << i;
}
}
printf("%f %f\n", nx, ny);
}
return 0;
}
/*
k1 -k2
k2 k1
*/
+100
View File
@@ -0,0 +1,100 @@
# P16055 [CSPro 31] 坐标变换(其二)
## 题目背景
洛谷的测试数据仅供民间交流使用,非官方测试数据。官方评测链接:<https://www.cspro.org/>。
## 题目描述
对于平面直角坐标系上的坐标 $(x, y)$,小 P 定义了如下两种操作:
1. 拉伸 $k$ 倍:横坐标 $x$ 变为 $kx$,纵坐标 $y$ 变为 $ky$;
2. 旋转 $\theta$:将坐标 $(x, y)$ 绕坐标原点 $(0, 0)$ **逆时针**旋转 $\theta$ 弧度($0 \le \theta < 2\pi$)。易知旋转后的横坐标为 $x \cos \theta - y \sin \theta$,纵坐标为 $x \sin \theta + y \cos \theta$。
设定好了包含 $n$ 个操作的序列 $(t_1, t_2, \cdots, t_n)$ 后,小 P 又定义了如下查询:
- `i j x y`:坐标 $(x, y)$ 经过操作 $t_i, \cdots, t_j$($1 \le i \le j \le n$)后的新坐标。
对于给定的操作序列,试计算 $m$ 个查询的结果。
## 输入格式
从标准输入读入数据。
输入共 $n + m + 1$ 行。
输入的第一行包含空格分隔的两个正整数 $n$ 和 $m$,分别表示操作和查询个数。
接下来 $n$ 行依次输入 $n$ 个操作,每行包含空格分隔的一个整数(操作类型)和一个实数($k$ 或 $\theta$),形如 `1 k`(表示拉伸 $k$ 倍)或 `2 θ`(表示旋转 $\theta$)。
接下来 $m$ 行依次输入 $m$ 个查询,每行包含空格分隔的四个整数 $i、j、x$ 和 $y$,含义如前文所述。
## 输出格式
输出到标准输出。
输出共 $m$ 行,每行包含空格分隔的两个实数,表示对应查询的结果。
## 输入输出样例 #1
### 输入 #1
```
10 5
2 0.59
2 4.956
1 0.997
1 1.364
1 1.242
1 0.82
2 2.824
1 0.716
2 0.178
2 4.094
1 6 -953188 -946637
1 9 969538 848081
4 7 -114758 522223
1 9 -535079 601597
8 8 159430 -511187
```
### 输出 #1
```
-1858706.758 -83259.993
-1261428.46 201113.678
-75099.123 -738950.159
-119179.897 -789457.532
114151.88 -366009.892
```
## 说明/提示
### 样例解释
第五个查询仅对输入坐标使用了操作八:拉伸 $0.716$ 倍。
横坐标:$159430 \times 0.716 = 114151.88$
纵坐标:$-511187 \times 0.716 = -366009.892$
由于具体计算方式不同,程序输出结果可能与真实值有微小差异,样例输出仅保留了三位小数。
### 子任务
- $80\%$ 的测试数据满足:$n, m \le 1000$;
- 全部的测试数据满足:
- $n, m \le 10^5$;
- 输入的坐标均为整数且绝对值不超过 $10^6$;
- 单个拉伸操作的系数 $k \in [0.5, 2]$;
- 任意操作区间 $t_i, \cdots, t_j$($1 \le i \le j \le n$)内拉伸系数 $k$ 的乘积在 $[0.001, 1000]$ 范围内。
### 评分方式
如果你输出的浮点数与参考结果相比,满足绝对误差不大于 $0.1$,则该测试点满分,否则不得分。
### 提示
- C/C++:建议使用 `double` 类型存储浮点数,并使用 `scanf("%lf", &x);` 进行输入,`printf("%f", x);` 输出,也可以使用 `cin` 和 `cout` 输入输出浮点数;`#include <math.h>` 后可使用三角函数 `cos()` 和 `sin()`。
- Python:直接使用 `print(x)` 即可输出浮点数 `x`;`from math import cos, sin` 后可使用相应三角函数。
- Java:建议使用 `double` 类型存储浮点数,可以使用 `System.out.print(x);` 进行输出;可使用 `Math.cos()` 和 `Math.sin()` 调用三角函数。
+124
View File
@@ -0,0 +1,124 @@
# P16055 [CSPro 31] 坐标变换(其二) 题解
这篇题解主要针对不懂得矩阵乘法的人。主要思想:
视每次操作为:
$$
newx = x * k1 - y * k2\\
newy = x * k2 + y * k1\\
x = newx, y = newy
$$
其中:
- 操作为 $1$ 时,$k1 = k, k2 = 0$。
- 操作为 $2$ 时,$k1 = \cos(\theta), k2 = \sin(\theta)$。
## 80 pts 暴力
注意到 $80%$ 的测试点满足 $n, m \le 1000$,此时最大执行次数为 $1000 \times 1000 = 10^6$,可以获得此部分分。
::::info[[80 pts 代码](https://www.luogu.com.cn/record/303033043)]
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, double>> o;
double k1[100005], k2[100005];
int main() {
scanf("%d%d", &n, &m);
o.push_back({0, 0});
for (int i = 1; i <= n; ++i) {
int op;
double k;
scanf("%d%lf", &op, &k);
o.push_back({op, k});
if (op == 2) {
k1[i] = cos(k);
k2[i] = sin(k);
} else {
k1[i] = k;
k2[i] = 0;
}
}
for (int i = 1; i <= m; ++i) {
int l, r, x, y;
scanf("%d%d%d%d", &l, &r, &x, &y);
double nx = x, ny = y;
for (int j = l; j <= r; ++j) {
if (o[j].first == 1)
nx *= o[j].second, ny *= o[j].second;
else {
double tmpx = nx * k1[j] - ny * k2[j],
tmpy = nx * k2[j] + ny * k1[j];
nx = tmpx, ny = tmpy;
}
}
printf("%f %f\n", nx, ny);
}
return 0;
}
```
::::
## 100 pts st 表
注意到连续一段的操作可以由各部分操作叠加,可使用 st 表解决。
::::info[[100pts 代码](https://www.luogu.com.cn/record/303042049)]
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, double>> o;
double st1[200005][21], st2[200005][21];
int main() {
scanf("%d%d", &n, &m);
o.push_back({0, 0});
for (int i = 1; i <= n; ++i) {
int op;
double k;
scanf("%d%lf", &op, &k);
o.push_back({op, k});
if (op == 1) {
st1[i][0] = k;
st2[i][0] = 0;
} else {
st1[i][0] = cos(k);
st2[i][0] = sin(k);
}
}
for (int i = 1; i <= 20; i++) {
for (int j = 1; j + (1 << i) - 1 <= n; j++) {
double tmp11 = st1[j][i - 1], tmp12 = st2[j][i - 1];
double tmp21 = st1[j + (1 << (i - 1))][i - 1],
tmp22 = st2[j + (1 << (i - 1))][i - 1];
st1[j][i] = tmp11 * tmp21 - tmp12 * tmp22;
st2[j][i] = tmp11 * tmp22 + tmp12 * tmp21;
}
}
while (m--) {
int l, r, x, y;
scanf("%d%d%d%d", &l, &r, &x, &y);
double nx = x, ny = y;
r++;
for (int i = 20; i >= 0; i--) {
if (l + (1 << i) <= r) {
double tmpx = st1[l][i] * nx - st2[l][i] * ny,
tmpy = st2[l][i] * nx + st1[l][i] * ny;
nx = tmpx, ny = tmpy;
l += 1 << i;
}
}
printf("%f %f\n", nx, ny);
}
return 0;
}
```
::::