題目鏈接:
題目大意:
數據範圍:
解題思路:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long LL;
const int inf = 1 << 30;
const LL INF = 1LL << 60;
const int MaxN = 10010;
const double PI = 3.1415926535897932384626;
int n, C;
struct segtree {
int l, r;
double x, y; //當前節點所管轄區間的向量
}tree[4 * MaxN + 5];
int lazy[4 * MaxN + 5]; //當前節點所管轄區間 要旋轉的角度
int angle[MaxN + 5]; //angle[i]表示節點i和(i + 1)當前的夾角,依照題意,逆時針
double Len[MaxN + 5]; //各線段長度
//其實就是區間修改,只是爲了貼合題意,取名叫rotate
void rotate(int rt, int val) {
double Theta = (double)val * PI / 180.0; //轉化爲弧度制
//向量旋轉Theta弧度後,求新座標的公式
double x = tree[rt].x * cos(Theta) - tree[rt].y * sin(Theta);
double y = tree[rt].x * sin(Theta) + tree[rt].y * cos(Theta);
tree[rt].x = x;
tree[rt].y = y;
}
void push_up(int rt) {
tree[rt].x = tree[rt << 1].x + tree[rt << 1 | 1].x;
tree[rt].y = tree[rt << 1].y + tree[rt << 1 | 1].y;
}
void push_down(int rt) {
if(lazy[rt] != 0) { //保存的是旋轉的角度,所以有可能是負的!!!掛了不知多少發!!!
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
rotate(rt << 1, lazy[rt]);
rotate(rt << 1 | 1, lazy[rt]);
lazy[rt] = 0;
}
}
void Build(int rt, int l, int r) { //建樹
tree[rt].l = l; tree[rt].r = r;
lazy[rt] = 0;
if(l == r) {
tree[rt].x = 0.0;
tree[rt].y = Len[l];
return;
}
int mid = (l + r) >> 1;
Build(rt << 1, l, mid);
Build(rt << 1 | 1, mid + 1, r);
push_up(rt);
}
void update(int rt, int L, int R, int val) {
if(L <= tree[rt].l && tree[rt].r <= R) {
lazy[rt] += val;
rotate(rt, val);
return;
}
push_down(rt);
int mid = (tree[rt].l + tree[rt].r) >> 1;
if(L <= mid) update(rt << 1, L, R, val);
if(R > mid) update(rt << 1 | 1, L, R, val);
push_up(rt);
}
int main()
{
while(scanf("%d %d", &n, &C) != EOF)
{
for(int i = 1; i <= n; i++) scanf("%lf", &Len[i]);
Build(1, 1, n);
for(int i = 1; i <= n; i++) angle[i] = 180; //初始爲180
for(int i = 1; i <= C; i++) {
int a, b;
scanf("%d %d", &a, &b);
update(1, a + 1, n, b - angle[a]);
angle[a] = b;
printf("%.2lf %.2lf\n", tree[1].x, tree[1].y);
//最後答案顯然就是根節點的向量
}
printf("\n");
}
return 0;
}