Robot Arm (Codeforces 618E)
Tags: Others, geometry

Roger is a robot. He has an arm that is a series of n segments connected to each other. The endpoints of the i-th segment are initially located at points (i - 1, 0) and (i, 0). The endpoint at (i - 1, 0) is colored red and the endpoint at (i, 0) is colored blue for all segments. Thus, the blue endpoint of the i-th segment is touching the red endpoint of the (i + 1)-th segment for all valid i.Roger can move his arm in two different ways:   He can choose some segment and some value. This is denoted as choosing the segment number i and picking some positive l. This change happens as follows: the red endpoint of segment number i and segments from 1 to i - 1 are all fixed in place. Imagine a ray from the red endpoint to the blue endpoint. The blue endpoint and segments i + 1 through n are translated l units in the direction of this ray.   In this picture, the red point labeled A and segments before A stay in place, while the blue point labeled B and segments after B gets translated. He can choose a segment and rotate it. This is denoted as choosing the segment number i, and an angle a. The red endpoint of the i-th segment will stay fixed in place. The blue endpoint of that segment and segments i + 1 to n will rotate clockwise by an angle of a degrees around the red endpoint.   In this picture, the red point labeled A and segments before A stay in place, while the blue point labeled B and segments after B get rotated around point A. Roger will move his arm m times. These transformations are a bit complicated, and Roger easily loses track of where the blue endpoint of the last segment is. Help him compute the coordinates of the blue endpoint of the last segment after applying each operation. Note that these operations are cumulative, and Roger's arm may intersect itself arbitrarily during the moves.

Input: The first line of the input will contain two integers n and m (1 ≤ n, m ≤ 300 000) — the number of segments and the number of operations to perform.Each of the next m lines contains three integers xi, yi and zi describing a move. If xi = 1, this line describes a move of type 1, where yi denotes the segment number and zi denotes the increase in the length. If xi = 2, this describes a move of type 2, where yi denotes the segment number, and zi denotes the angle in degrees. (1 ≤ xi ≤ 2, 1 ≤ yi ≤ n, 1 ≤ zi ≤ 359)

Output: Print m lines. The i-th line should contain two real values, denoting the coordinates of the blue endpoint of the last segment after applying operations 1, ..., i. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 4.Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if  for all coordinates.

Note: The following pictures shows the state of the arm after each operation. The coordinates of point F are printed after applying each operation. For simplicity, we only show the blue endpoints of a segment (with the exception for the red endpoint of the first segment). For instance, the point labeled B is the blue endpoint for segment 1 and also the red endpoint for segment 2.Initial state:    Extend segment 1 by 3.    Rotate segment 3 by 90 degrees clockwise.    Rotate segment 5 by 48 degrees clockwise.    Extend segment 4 by 1.   

Sample Inputs:
5 4
1 1 3
2 3 90
2 5 48
1 4 1

Sample Outputs:
8.0000000000 0.0000000000
5.0000000000 -3.0000000000
4.2568551745 -2.6691306064
4.2568551745 -3.6691306064

Solution (Python):
from cmath import rect
import sys
import math
from functools import reduce

class SegmentTree:

    def __init__(self, L, function=lambda x, y: x + y):
        self.function = function
        N = self.size = len(L)
        M = 1 << N.bit_length()
        self.margin = 2 * M - N
        self.L = [None for i in range(self.margin)] + L
        for i in range(M - 1, 0, -1):
            (x, y) = (self.L[i << 1], self.L[i << 1 | 1])
            self.L[i] = None if x is None or y is None else function(x, y)

    def modify(self, pos, value):
        p = pos + self.margin
        self.L[p] = value
        while p > 1:
            (x, y) = (self.L[p], self.L[p ^ 1])
            if p & 1:
                (x, y) = (y, x)
            self.L[p >> 1] = None if x is None or y is None else self.function(x, y)
            p >>= 1

    def query(self, left, right):
        (l, r) = (left + self.margin, right + self.margin)
        stack = []
        void = True
        while l < r:
            if l & 1:
                if void:
                    result = self.L[l]
                    void = False
                else:
                    result = self.function(result, self.L[l])
                l += 1
            if r & 1:
                r -= 1
                stack.append(self.L[r])
            l >>= 1
            r >>= 1
        init = stack.pop() if void else result
        return reduce(self.function, reversed(stack), init)

def degrect(r, phi):
    return rect(r, math.radians(phi))

def vsum(u, v):
    return (u[0] + v[0] * degrect(1, u[1]), (u[1] + v[1]) % 360)

def solve(f):
    (n, m) = [int(x) for x in f.readline().split()]
    segments = [[1, 0] for i in range(n)]
    arm = SegmentTree([(1, 0) for i in range(n)], vsum)
    for line in f:
        (q, i, a) = [int(x) for x in line.split()]
        if q == 1:
            segments[i - 1][0] += a
        else:
            segments[i - 1][1] -= a
        arm.modify(i - 1, (degrect(segments[i - 1][0], segments[i - 1][1]), segments[i - 1][1]))
        query = arm.query(0, n)[0]
        sys.stdout.write(str(query.real) + ' ' + str(query.imag) + '\n')
solve(sys.stdin)

Solution (Python):
from cmath import rect
import sys
import math
from functools import reduce

class SegmentTree:

    def __init__(self, L, function=lambda x, y: x + y):
        self.function = function
        N = self.size = len(L)
        M = 1 << N.bit_length()
        self.margin = 2 * M - N
        self.L = [None for i in range(self.margin)] + L
        for i in range(M - 1, 0, -1):
            (x, y) = (self.L[i << 1], self.L[i << 1 | 1])
            self.L[i] = None if x is None or y is None else function(x, y)

    def modify(self, pos, value):
        p = pos + self.margin
        self.L[p] = value
        while p > 1:
            (x, y) = (self.L[p], self.L[p ^ 1])
            if p & 1:
                (x, y) = (y, x)
            self.L[p >> 1] = None if x is None or y is None else self.function(x, y)
            p >>= 1

    def query(self, left, right):
        (l, r) = (left + self.margin, right + self.margin)
        stack = []
        void = True
        while l < r:
            if l & 1:
                if void:
                    result = self.L[l]
                    void = False
                else:
                    result = self.function(result, self.L[l])
                l += 1
            if r & 1:
                r -= 1
                stack.append(self.L[r])
            l >>= 1
            r >>= 1
        init = stack.pop() if void else result
        return reduce(self.function, reversed(stack), init)

def degrect(r, phi):
    return rect(r, math.radians(phi))

def vsum(u, v):
    return (u[0] + v[0] * degrect(1, u[1]), (u[1] + v[1]) % 360)

def solve(f):
    (n, m) = [int(x) for x in f.readline().split()]
    segments = [[1, 0] for i in range(n)]
    arm = SegmentTree([(1, 0) for i in range(n)], vsum)
    for line in f:
        (q, i, a) = [int(x) for x in line.split()]
        if q == 1:
            segments[i - 1][0] += a
        else:
            segments[i - 1][1] -= a
        arm.modify(i - 1, (degrect(segments[i - 1][0], segments[i - 1][1]), segments[i - 1][1]))
        query = arm.query(0, n)[0]
        print(query.real, query.imag)
solve(sys.stdin)

Solution (Python):
import math
o = 524288
pi = math.acos(-1)
a = [0] * (2 * o)
x = [0.0] * (2 * o)
y = [0.0] * (2 * o)
z = [0] * (2 * o)
(n, m) = map(int, input().split())
for i in range(n):
    a[i + o] = x[i + o] = 1
for i in range(o - 1, 0, -1):
    x[i] = x[i << 1] + x[i << 1 | 1]
for _ in range(m):
    (i, j, k) = map(int, input().split())
    j += o - 1
    if i == 1:
        a[j] += k
    else:
        z[j] = (z[j] + k) % 360
    x[j] = a[j] * math.cos(z[j] * pi / 180)
    y[j] = a[j] * -math.sin(z[j] * pi / 180)
    while j > 1:
        if j % 2 == 0:
            x[j // 2] = x[j] + x[j + 1] * math.cos(z[j] * pi / 180) + y[j + 1] * math.sin(z[j] * pi / 180)
            y[j // 2] = y[j] - x[j + 1] * math.sin(z[j] * pi / 180) + y[j + 1] * math.cos(z[j] * pi / 180)
            z[j // 2] = (z[j] + z[j + 1]) % 360
        else:
            x[j // 2] = x[j - 1] + x[j] * math.cos(z[j - 1] * pi / 180) + y[j] * math.sin(z[j - 1] * pi / 180)
            y[j // 2] = y[j - 1] - x[j] * math.sin(z[j - 1] * pi / 180) + y[j] * math.cos(z[j - 1] * pi / 180)
            z[j // 2] = (z[j - 1] + z[j]) % 360
        j //= 2
    print('%.10f %.10f' % (x[1], y[1]))