CCF CSP题解:坐标变换(其二)(202309-2)

链接和思路

OJ链接:传送门

对于平面直角坐标系上的坐标 ( x , y ) (x,y) (x,y),定义如下两种操作:

  1. 拉伸 k k k倍:横坐标 x x x变为 k x kx kx, 纵坐标 y y y 变为 k y ky ky;
  2. 旋转 θ \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θ 。

本题要求将平面坐标 ( x , y ) (x, y) (x,y),经过 n n n个操作 ( t 1 , t 2 , ⋯   , t n ) (t_1, t_2, \cdots, t_n) (t1,t2,⋯,tn)后,对于给定的操作序列,计算 m m m个如下查询:

  • 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)后的新坐标。

在考场上,笔者发现此题为区间查询问题,因而首先想到使用树状数组。但是树状数组的建树和查询成本都太高,而此题不涉及到对区间值的修改,因而只需要记录 k k k的前缀和和 θ \theta θ的前缀积即可。前缀和向量的建立只需要线性代价,而每次区间查询只需要常数级别的代价。

具体而言,由于拉伸和旋转2种行为相互独立,我们只需分别求出经过 n n n个操作 ( t 1 , t 2 , ⋯   , t n ) (t_1, t_2, \cdots, t_n) (t1,t2,⋯,tn)后,总共旋转的角度和拉伸的倍数。我们仅需维护2个向量:

  1. 拉伸前缀积向量 k = { k 0 , k 1 , k 2 , ⋯   , k n } \mathbf k = \{k_0,k_1, k_2,\cdots,k_n\} k={k0,k1,k2,⋯,kn},其中 k 0 = 1 k_0=1 k0=1, k i k_i ki为前 i i i次操作的总拉伸的倍数,即前缀积;
  2. 旋转前缀和向量 θ = { θ 0 , θ 1 , θ 2 , ⋯   , θ n } \mathbf{\theta} = \{\theta_0,\theta_1, \theta_2,\cdots,\theta_n\} θ={θ0,θ1,θ2,⋯,θn},其中 θ 0 = 0 \theta_0=0 θ0=0, θ i \theta_i θi为前 i i i次操作的总旋转角度,即前缀和。

被查询坐标 ( 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)后,拉伸倍数为 k j / k i − 1 k_j / k_{i-1} kj/ki−1,角度为 θ j − θ i − 1 \theta_j-\theta_{i-1} θj−θi−1。

AC代码

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;

int n, m;

vector<double> xita(100005);
vector<double> k(100005, 1);


int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        int type;
        double value;
        cin >> type >> value;
        if (type == 1) {
            k[i] = k[i - 1] * value;
            xita[i] = xita[i - 1];
        } else {
            k[i] = k[i - 1];
            xita[i] = xita[i - 1] + value;
        }
    }

    for (int i = 0; i < m; ++i) {
        int l, r;
        double x, y;
        cin >> l >> r >> x >> y;

        double sum_xita = xita[r] - xita[l - 1];
        double pro_k = k[r] / k[l - 1];

        cout << fixed << setprecision(3) << (x * cos(sum_xita) - y * sin(sum_xita)) * pro_k << " "
             << (x * sin(sum_xita) + y * cos(sum_xita)) * pro_k << endl;
    }

    return 0;
}
相关推荐
菜鸟233号9 分钟前
力扣213 打家劫舍II java实现
java·数据结构·算法·leetcode
十五年专注C++开发16 分钟前
CMake基础: 在release模式下生成调试信息的方法
linux·c++·windows·cmake·跨平台构建
方便面不加香菜17 分钟前
数据结构--栈和队列
c语言·数据结构
狐5722 分钟前
2026-01-18-LeetCode刷题笔记-1895-最大的幻方
笔记·算法·leetcode
点云SLAM39 分钟前
C++(C++17/20)最佳工厂写法和SLAM应用综合示例
开发语言·c++·设计模式·c++实战·注册工厂模式·c++大工程系统
Q741_14741 分钟前
C++ 队列 宽度优先搜索 BFS 力扣 662. 二叉树最大宽度 每日一题
c++·算法·leetcode·bfs·宽度优先
Pluchon42 分钟前
硅基计划4.0 算法 动态规划进阶
java·数据结构·算法·动态规划
csdn_aspnet1 小时前
C++跨平台开发:工程难题与解决方案深度解析
c++
余衫马1 小时前
在Win10下编译 Poppler
c++·windows·qt·pdf·poppler
王老师青少年编程1 小时前
2024年3月GESP真题及题解(C++七级): 俄罗斯方块
c++·题解·真题·gesp·csp·俄罗斯方块·七级