poj 2991 线段树区间更新

来源:互联网 发布:php简历模板 编辑:程序博客网 时间:2024/04/28 02:01

Crane
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 3897 Accepted: 1059 Special Judge

Description

ACM has bought a new crane (crane -- jeřáb) . The crane consists of n segments of various lengths, connected by flexible joints. The end of the i-th segment is joined to the beginning of the i + 1-th one, for 1 ≤ i < n. The beginning of the first segment is fixed at point with coordinates (0, 0) and its end at point with coordinates (0, w), where w is the length of the first segment. All of the segments lie always in one plane, and the joints allow arbitrary rotation in that plane. After series of unpleasant accidents, it was decided that software that controls the crane must contain a piece of code that constantly checks the position of the end of crane, and stops the crane if a collision should happen. 

Your task is to write a part of this software that determines the position of the end of the n-th segment after each command. The state of the crane is determined by the angles between consecutive segments. Initially, all of the angles are straight, i.e., 180o. The operator issues commands that change the angle in exactly one joint. 

Input

The input consists of several instances, separated by single empty lines. 

The first line of each instance consists of two integers 1 ≤ n ≤10 000 and c 0 separated by a single space -- the number of segments of the crane and the number of commands. The second line consists of n integers l1,..., ln (1 li 100) separated by single spaces. The length of the i-th segment of the crane is li. The following c lines specify the commands of the operator. Each line describing the command consists of two integers s and a (1 ≤ s < n, 0 ≤ a ≤ 359) separated by a single space -- the order to change the angle between the s-th and the s + 1-th segment to a degrees (the angle is measured counterclockwise from the s-th to the s + 1-th segment).

Output

The output for each instance consists of c lines. The i-th of the lines consists of two rational numbers x and y separated by a single space -- the coordinates of the end of the n-th segment after the i-th command, rounded to two digits after the decimal point. 

The outputs for each two consecutive instances must be separated by a single empty line.

Sample Input

2 110 51 903 25 5 51 2702 90

Sample Output

5.00 10.00-10.00 5.00-5.00 10.00

Source

CTU Open 2005

给出n个线段和c个命令。最开始每个线段竖直叠加往上放置,所有线段连成一条直线。每个命令给出s和a,将第s条和第s+1条线段调整为角度a。每条命令后输出第n条线段末端点的坐标,第一条线段起点为(0,0)

首先要解决的是怎么表示坐标,要表示x,y坐标,自然要将每条线段往水平方向和竖直方向分解,也就是把每条表示成向量vx和vy,最开始vx=0,vy=len。而末端点的坐标就是所有向量之和。既然是求n个向量和,尝试用线段树解决问题。

然后就是旋转的问题,将第s和s+1条线段调整为角度a,假设旋转ang度,那么其实是第s+1到n条都线段都跟着旋转ang度,其中对于向量vx和vy逆时针旋转ang度, vx'=vxcos(ang)-vysin(ang), vy' = vxsin(ang)+vycos(ang),所以对于每条命令,更新区间[s+1,n]。同时为了确定每次需要转多少度,用ang[i+1]表示第i+1和i条线段之间的角度,初始时都为180度。



#include <iostream>#include <cstdio>#include <cmath>#include <cstring>using namespace std;#define maxn 10000+5#define Pi  acos(-1.0)struct node{    int l, r;    double vx,vy;    int lazy;};node tree[4*maxn];int a[maxn];int ang[maxn];void build(int rt, int ll, int rr){    tree[rt].l = ll, tree[rt].r = rr;    tree[rt].vx = 0, tree[rt].lazy = 0;    if(ll==rr){        tree[rt].vy = a[ll];        return;    }    int m = ll+(rr-ll)/2;    int lc = 2*rt, rc = 2*rt+1;    build(lc, ll, m);    build(rc, m+1, rr);    tree[rt].vx = tree[lc].vx+tree[rc].vx;    tree[rt].vy = tree[lc].vy+tree[rc].vy;}void rotate(double &x,  double &y, int ang){    double tx = x, ty = y;    double a = (double)ang/180*Pi;    x = tx*cos(a)-ty*sin(a);    y = tx*sin(a)+ty*cos(a);}void seg_modify(int rt, int ll, int rr, int a){    if(ll > rr) return;    int l = tree[rt].l, r = tree[rt].r;    int lazy = tree[rt].lazy;    int lc = 2*rt, rc = 2*rt+1;    if(ll == l && rr == r){        tree[rt].lazy += a;        rotate(tree[rt].vx, tree[rt].vy, a);        return;    }    else if(lazy){        tree[lc].lazy+= lazy; //注意这里是+=,每次旋转的角度叠加,而不是直接变为多少度        tree[rc].lazy+= lazy;        tree[rt].lazy = 0;        rotate(tree[lc].vx, tree[lc].vy, lazy);        rotate(tree[rc].vx, tree[rc].vy, lazy);    }    int m = l+(r-l)/2;    if(rr <= m)        seg_modify(lc, ll, rr, a);    else if(ll >= m+1)        seg_modify(rc, ll, rr, a);    else{        seg_modify(lc, ll, m, a);        seg_modify(rc, m+1, rr, a);    }    tree[rt].vx = tree[lc].vx+tree[rc].vx;    tree[rt].vy = tree[lc].vy+tree[rc].vy;}int main(){    int n, c;    while(scanf("%d%d", &n, &c)!=EOF){        for(int i = 1; i <= n; i++)            scanf("%d", a+i);        build(1, 1, n);       for(int i = 0; i<= n+1; i++) ang[i] = 180;        int s, a;        for(int i = 0; i < c; i++){            scanf("%d%d", &s, &a);            seg_modify(1, s+1, n, a-ang[s+1]);            ang[s+1]=a;            printf("%.2lf %.2lf\n\n", tree[1].vx, tree[1].vy);        }        printf("\n");    }    return 0;}




0 0
原创粉丝点击