更多 CSP 认证考试题目题解可以前往:CSP-CCF 认证考试真题题解
原题链接: 202309-2 坐标变换(其二)
时间限制: 2.0s
内存限制: 512.0MB
问题描述
对于平面直角坐标系上的坐标 ( x , y ) (x, y) (x,y),小 P 定义了如下两种操作:
- 拉伸 k k k 倍:横坐标 x x x 变为 k x kx kx,纵坐标 y y y 变为 k y ky ky;
- 旋转 θ \theta θ:将坐标 ( x , y ) (x, y) (x,y) 绕坐标原点 ( 0 , 0 ) (0, 0) (0,0) 逆时针旋转 θ \theta θ 弧度( 0 ≤ θ < 2 π 0 \le \theta < 2 \pi 0≤θ<2π)。易知旋转后的横坐标为 x cos θ − y sin θ x \cos \theta - y \sin \theta xcosθ−ysinθ,纵坐标为 x sin θ + y cos θ x \sin \theta + y\cos \theta xsinθ+ycosθ。
设定好了包含 n n n 个操作的序列 ( t 1 , t 2 , ⋯ , t n ) (t_1, t_2, \cdots, t_n) (t1,t2,⋯,tn) 后,小 P 又定义了如下查询:
- i j x y:坐标
(
x
,
y
)
(x, y)
(x,y) 经过操作
t
i
,
⋯
,
t
j
t_i, \cdots, t_j
ti,⋯,tj(
1
≤
i
≤
j
≤
n
1 \le i \le j \le n
1≤i≤j≤n)后的新坐标。
对于给定的操作序列,试计算 m m m 个查询的结果。
输入格式
从标准输入读入数据。
输入共 n + m + 1 n+m+1 n+m+1 行。
输入的第一行包含空格分隔的两个正整数 n n n 和 m m m,分别表示操作和查询个数。
接下来 n n n 行依次输入 n n n 个操作,每行包含空格分隔的一个整数(操作类型)和一个实数( k k k 或 θ \theta θ),形如 1 k 1 \ k 1 k(表示拉伸 k k k 倍)或 2 θ 2 \ \theta 2 θ(表示旋转 θ \theta θ)。
接下来 m m m 行依次输入 m m m 个查询,每行包含空格分隔的四个整数 i i i、 j j j、 x x x 和 y y y,含义如前文所述。
输出格式
输出到标准输出中。
输出共 m m m 行,每行包含空格分隔的两个实数,表示对应查询的结果。
样例输入
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
样例输出
-1858706.758 -83259.993
-1261428.46 201113.678
-75099.123 -738950.159
-119179.897 -789457.532
114151.88 -366009.892
样例说明
第五个查询仅对输入坐标使用了操作八:拉伸 0.716 0.716 0.716 倍。
横坐标: 159430 × 0.716 = 114151.88 159430 \times 0.716 = 114151.88 159430×0.716=114151.88
纵坐标: − 511187 × 0.716 = − 366009.892 -511187 \times 0.716 = -366009.892 −511187×0.716=−366009.892
由于具体计算方式不同,程序输出结果可能与真实值有微小差异,样例输出仅保留了三位小数。
评测用例规模与约定
80 % 80\% 80% 的测试数据满足: n , m ≤ 1000 n, m \le 1000 n,m≤1000;
全部的测试数据满足:
- n , m ≤ 100000 n, m \le 100000 n,m≤100000;
- 输入的坐标均为整数且绝对值不超过 1000000 1000000 1000000;
- 单个拉伸操作的系数 k ∈ [ 0.5 , 2 ] k \in [ 0.5, 2 ] k∈[0.5,2];
- 任意操作区间 t i , ⋯ , t j t_i, \cdots, t_j ti,⋯,tj( 1 ≤ i ≤ j ≤ n 1 \le i \le j \le n 1≤i≤j≤n)内拉伸系数 k k k 的乘积在 [ 0.001 , 1000 ] [ 0.001, 1000 ] [0.001,1000] 范围内。
评分方式
如果你输出的浮点数与参考结果相比,满足绝对误差不大于 0.1 0.1 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() 调用三角函数。
题解
由于这两个操作相互独立,考虑这两个操作是否可以统一形式,统一后的形式如下:
- 拉伸 k k k 倍,并旋转 0 0 0;
- 拉伸 1 1 1 倍,并旋转 θ \theta θ。
对拉伸和旋转分别考虑使用前缀积和前缀和:
- 拉伸:记
a[i]
为 k 1 k 2 ⋯ k i k_1k_2\cdots k_i k1k2⋯ki,即前 i i i 个操作拉伸的倍数之积,则从 i i i 到 j j j 的拉伸倍数为 k i k i + 1 ⋯ k j = k 1 k 2 ⋯ k j k 1 k 2 ⋯ k i − 1 = a [ j ] a [ i − 1 ] k_ik_{i+1}\cdots k_j=\dfrac{k_1k_2\cdots k_j}{k_1k_2\cdots k_{i-1}}=\dfrac{a[j]}{a[i-1]} kiki+1⋯kj=k1k2⋯ki−1k1k2⋯kj=a[i−1]a[j]。 - 旋转:记
b[i]
为 θ 1 + θ 2 + ⋯ + θ i \theta_1+\theta_2+\cdots+\theta_i θ1+θ2+⋯+θi,即前 i i i 个操作旋转的弧度之和,则从 i i i 到 j j j 的旋转弧度为 θ i + θ i + 1 + ⋯ + θ j = ( θ 1 + θ 2 + ⋯ + θ j ) − ( θ 1 + θ 2 + ⋯ + θ i − 1 ) = b [ j ] − b [ i − 1 ] \theta_i+\theta_{i+1}+\cdots+\theta_j=(\theta_1+\theta_2+\cdots+\theta_j)-(\theta_1+\theta_2+\cdots+\theta_{i-1})=b[j]-b[i-1] θi+θi+1+⋯+θj=(θ1+θ2+⋯+θj)−(θ1+θ2+⋯+θi−1)=b[j]−b[i−1]。
对于每一次询问,现将 ( x , y ) (x,y) (x,y) 进行拉伸操作变为 ( x ′ , y ′ ) (x',y') (x′,y′),其中 x ′ = k x x'=kx x′=kx, y ′ = k y y'=ky y′=ky, k = a [ j ] a [ i − 1 ] k=\dfrac{a[j]}{a[i-1]} k=a[i−1]a[j];
然后再将 ( x ′ , y ′ ) (x',y') (x′,y′) 进行旋转操作变为 ( x ′ ′ , y ′ ′ ) (x'',y'') (x′′,y′′) 得到答案,其中 x ′ ′ = x ′ cos θ − y ′ sin θ x''=x'\cos\theta-y'\sin\theta x′′=x′cosθ−y′sinθ, y ′ ′ = x ′ sin θ + y ′ cos θ y''=x'\sin\theta+y'\cos\theta y′′=x′sinθ+y′cosθ, θ = b [ j ] − b [ i − 1 ] \theta=b[j]-b[i-1] θ=b[j]−b[i−1]。
时间复杂度: O ( n + m ) \mathcal{O}(n+m) O(n+m)。
参考代码(1.546s,4.453MB)
/*
Created by Pujx on 2024/2/5.
*/
#pragma GCC optimize(2, 3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
//#define int long long
//#define double long double
using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
#define inf (int)0x3f3f3f3f3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define yn(x) cout << (x ? "yes" : "no") << endl
#define Yn(x) cout << (x ? "Yes" : "No") << endl
#define YN(x) cout << (x ? "YES" : "NO") << endl
#define mem(x, i) memset(x, i, sizeof(x))
#define cinarr(a, n) for (int i = 1; i <= n; i++) cin >> a[i]
#define cinstl(a) for (auto& x : a) cin >> x;
#define coutarr(a, n) for (int i = 1; i <= n; i++) cout << a[i] << " \n"[i == n]
#define coutstl(a) for (const auto& x : a) cout << x << ' '; cout << endl
#define all(x) (x).begin(), (x).end()
#define md(x) (((x) % mod + mod) % mod)
#define ls (s << 1)
#define rs (s << 1 | 1)
#define ft first
#define se second
#define pii pair<int, int>
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) void(0)
#endif
const int N = 2e5 + 5;
//const int M = 1e5 + 5;
const int mod = 998244353;
//const int mod = 1e9 + 7;
//template <typename T> T ksm(T a, i64 b) { T ans = 1; for (; b; a = 1ll * a * a, b >>= 1) if (b & 1) ans = 1ll * ans * a; return ans; }
//template <typename T> T ksm(T a, i64 b, T m = mod) { T ans = 1; for (; b; a = 1ll * a * a % m, b >>= 1) if (b & 1) ans = 1ll * ans * a % m; return ans; }
double a[N], b[N];
int n, m, t, k, q;
void work() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> t;
if (t == 1) cin >> a[i];
else cin >> b[i], a[i] = 1;
}
a[0] = 1;
for (int i = 1; i <= n; i++) a[i] *= a[i - 1], b[i] += b[i - 1];
while (m--) {
int i, j; double x, y;
cin >> i >> j >> x >> y;
double k = a[j] / a[i - 1], theta = b[j] - b[i - 1];
x *= k, y *= k;
cout << fixed << setprecision(10) << x * cos(theta) - y * sin(theta) << ' ' << x * sin(theta) + y * cos(theta) << endl;
}
}
signed main() {
#ifdef LOCAL
freopen("C:\\Users\\admin\\CLionProjects\\Practice\\data.in", "r", stdin);
freopen("C:\\Users\\admin\\CLionProjects\\Practice\\data.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int Case = 1;
//cin >> Case;
while (Case--) work();
return 0;
}
/*
_____ _ _ _ __ __
| _ \ | | | | | | \ \ / /
| |_| | | | | | | | \ \/ /
| ___/ | | | | _ | | } {
| | | |_| | | |_| | / /\ \
|_| \_____/ \_____/ /_/ \_\
*/
关于代码的亿点点说明:
- 代码的主体部分位于
void work()
函数中,另外会有部分变量申明、结构体定义、函数定义在上方。#pragma ...
是用来开启 O2、O3 等优化加快代码速度。- 中间一大堆
#define ...
是我习惯上的一些宏定义,用来加快代码编写的速度。"debug.h"
头文件是我用于调试输出的代码,没有这个头文件也可以正常运行(前提是没定义DEBUG
宏),在程序中如果看到dbg(...)
是我中途调试的输出的语句,可能没删干净,但是没有提交上去没有任何影响。ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
这三句话是用于解除流同步,加快输入cin
输出cout
速度(这个输入输出流的速度很慢)。在小数据量无所谓,但是在比较大的读入时建议加这句话,避免读入输出超时。如果记不下来可以换用scanf
和printf
,但使用了这句话后,cin
和scanf
、cout
和printf
不能混用。- 将
main
函数和work
函数分开写纯属个人习惯,主要是为了多组数据。