套路题了。
根据和角公式 \(\mathrm{\sin (\alpha + \beta) = \sin \alpha \cos \beta + \cos \alpha \cos \beta, \cos (\alpha + \beta) = \cos \alpha \cos \beta - \sin \alpha \sin \beta}\)
可以考虑在复平面上维护一个复数 \(\mathrm{\sin \alpha + \cos \alpha i}\),或者矩阵维护。
求和及修改可以线段树。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define rep(i, a, b) for (int i = (a); i <= (b); i ++ )
#define rop(i, a, b) for (int i = (a); i < (b); i ++ )
#define dep(i, a, b) for (int i = (a); i >= (b); i -- )
#define dop(i, a, b) for (int i = (a); i > (b); i -- )
using namespace std;
using LL = long long;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
const int N = 300010;
const double pi = acos(-1);
const double eps = 1e-6;
int n, m;
double w[N];
struct Complex {
double x, y;
Complex(){}
Complex(double _x, double _y) { x = _x, y = _y; }
Complex operator + (const Complex& tmp)const {
return Complex(x + tmp.x, y + tmp.y);
}
Complex operator * (const Complex& tmp)const {
return Complex(x * tmp.x - y * tmp.y, x * tmp.y + y * tmp.x);
}
Complex operator - (const Complex& tmp)const {
return Complex(x - tmp.x, y - tmp.y);
}
Complex operator * (const double &tmp)const {
return Complex(x * tmp, y * tmp);
}
void clear() { x = y = 0; }
void makeI() { x = 1.0, y = 0; }
bool empty() { return x == 0 && y == 0; }
bool isI() { return x == 1 && y == 0; }
};
struct Tree {
int l, r;
Complex mul, sum;
int len() { return r - l + 1; }
}tr[N << 2];
#define ls u << 1
#define rs u << 1 | 1
void pushup(int u) {
tr[u].sum = tr[ls].sum + tr[rs].sum;
}
void push_mul(int u, Complex mul) {
if (tr[u].l == tr[u].r) {
tr[u].sum = tr[u].sum * mul;
return;
}
tr[u].mul = tr[u].mul * mul;
tr[u].sum = tr[u].sum * mul;
}
void pushdown(int u) {
if (tr[u].l == tr[u].r) return;
if (!tr[u].mul.isI()) {
push_mul(ls, tr[u].mul);
push_mul(rs, tr[u].mul);
tr[u].mul.makeI();
}
}
void build(int u, int l, int r) {
tr[u] = {l, r}, tr[u].mul.makeI();
if (l == r) {
tr[u].sum = Complex(sin(w[r]), cos(w[r]));
return;
}
int mid = l + r >> 1;
build(ls, l, mid), build(rs, mid + 1, r);
pushup(u);
}
void Multiply(int u, int l, int r, Complex k) {
if (tr[u].l >= l && tr[u].r <= r) {
push_mul(u, k); return;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) Multiply(ls, l, r, k);
if (r > mid) Multiply(rs, l, r, k);
pushup(u);
}
Complex query(int u, int l, int r) {
if (tr[u].l >= l && tr[u].r <= r) return tr[u].sum;
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1; Complex ans(0, 0);
if (l <= mid) ans = ans + query(ls, l, r);
if (r > mid) ans = ans + query(rs, l, r);
return ans;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i ++ )
scanf("%lf", &w[i]);
build(1, 1, n);
scanf("%d", &m);
while (m -- ) {
int op, l, r; double v;
scanf("%d%d%d", &op, &l, &r);
if (op == 1) {
scanf("%lf", &v);
Multiply(1, l, r, Complex(cos(v), -sin(v)));
}
else
printf("%.1lf\n", query(1, l, r).x);
}
return 0;
}
标签:tmp,NOIP2013,int,货车运输,return,Complex,alpha,const,P1967
From: https://www.cnblogs.com/LcyRegister/p/17037863.html