线段树,伤不起!!!
从30号回家的路上开始构思。。。想差不多了,然后回家写,写完过不去,和zxl童鞋交流,发现我题意理解错了!!!
我理解的那个角度是和Y正半轴的夹角,哪知道是俩线段之间的夹角,纠结。。
1号出去玩了,没做。
2号晚上做了,改差不多了,生成的几百组数据和标程都没问题,我要崩溃了。。
今早找到数据,错了好多,不知道哪里错了。。
zxl童鞋帮忙调错。。调出来了。。
我的代码是如果ang为0就不传递了,因为我觉得传递也没意义因为ang为0了啊。。囧。。不过这个是错的,如果为0,不旋转可以,但是向量还是需要平移的。
因为左子树影响右子树了,所以计算的时候得平移下。。。
每个节点记录当前线段的初始坐标和终点坐标,还有这条线段和Y轴的夹角,网上好多都是记录的是逆时针角度,我记录的是顺时针的。。还调了半天。。。不过都一样啦,就算角度的时候还有旋转的时候变下。
更新线段的时候,需要知道偏移角度,所以需要知道之前的这个线段和下个线段的夹角。。
其他就没啥了,注意中间更新左子树影响右子树,需要向量平移。
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <iostream>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define FOR(i,s,t) for(int i=(s); i<(t); i++)
#define BUG puts("here!!!")
#define STOP system("pause")
#define file_r(x) freopen(x, "r", stdin)
#define file_w(x) freopen(x, "w", stdout)
using namespace std;
const int MAX = 10010;
const double pi = acos(-1.0);
const double eps = 1e-8;
struct point {
double x, y;
point(){};
point(double xx, double yy):x(xx),y(yy){};
void P(double xx, double yy)
{
x = xx; y = yy;
}
};
point operator-(point a,point b)
{
return point(a.x - b.x, a.y - b.y);
}
point operator+(point a,point b)
{
return point(a.x + b.x, a.y + b.y);
}
struct Tnode{ // 一维线段树
int l,r,ang;
point pl, pr;
int len() { return r - l;}
int mid() { return MID(l,r);}
bool in(int ll,int rr) { return l >= ll && r <= rr; }
void lr(int ll,int rr){ l = ll; r = rr;}
};
Tnode node[MAX<<4];
point p[MAX];
void Build(int t,int l,int r)
{
node[t].lr(l,r);
node[t].ang = 0;
if( node[t].len() == 1 )
{
node[t].pl = p[l];
node[t].pr = p[r];
return ;
}
int mid = MID(l,r);
Build(L(t),l,mid);
Build(R(t),mid,r);
node[t].pl = node[L(t)].pl;
node[t].pr = node[R(t)].pr;
}
point Rotate(int ang, point a, point b)//角度转换,模板是逆时针旋转ang
{
double angle = -ang*pi/180.0;
b.x -= a.x; b.y -= a.y;
point c;
c.x = b.x * cos(angle) - b.y * sin(angle) + a.x;
c.y = b.x * sin(angle) + b.y * cos(angle) + a.y;
return c;
}
void get_point(int t,int ang)
{
node[t].pr = Rotate(ang, node[t].pl, node[t].pr);
}
void Updata_ang(int t)
{
node[L(t)].ang += node[t].ang;
node[L(t)].ang %= 360;
node[L(t)].pr = node[L(t)].pr - node[L(t)].pl + node[t].pl;
node[L(t)].pl = node[t].pl;
get_point(L(t), node[t].ang);
node[R(t)].ang += node[t].ang;
node[R(t)].ang %= 360;
node[R(t)].pr = node[R(t)].pr - node[R(t)].pl + node[L(t)].pr;
node[R(t)].pl = node[L(t)].pr;
get_point(R(t), node[t].ang);
node[t].ang = 0;
}
void Query(int t,int l,int r,int &ang)
{
if( node[t].in(l,r) && node[t].len() == 1 )
{
ang = node[t].ang;
return ;
}
if( node[t].len() == 1 ) return ;
Updata_ang(t);
int mid = node[t].mid();
if( l < mid ) Query(L(t), l, r, ang);
node[R(t)].pr = node[R(t)].pr - node[R(t)].pl + node[L(t)].pr;
node[R(t)].pl = node[L(t)].pr;
if( r > mid ) Query(R(t), l, r, ang);
node[t].pr = node[R(t)].pr;
}
void Updata(int t,int l,int r,int ang)
{
if( node[t].in(l,r) )
{
node[t].ang += ang;
get_point(t, ang);
return ;
}
if( node[t].len() == 1 ) return ;
Updata_ang(t);
int mid = node[t].mid();
if( l < mid ) Updata(L(t),l,r,ang);
node[R(t)].pr = node[R(t)].pr - node[R(t)].pl + node[L(t)].pr;
node[R(t)].pl = node[L(t)].pr;
if( r > mid ) Updata(R(t),l,r,ang);
node[t].pr = node[R(t)].pr;
}
int main()
{
int n, m, ind, a, len;
bool f = false;
while( ~scanf("%d%d", &n, &m) )
{
if( f ) puts("");
f = true;
int sumlen = 0;
p[0].P(0, 0);
FOR(i, 1, n+1)
{
scanf("%d", &len);
sumlen += len;
p[i].P(0, sumlen);
}
Build(1, 0, n);
while( m-- )
{
scanf("%d%d", &ind, &a);
int anga, angb;
Query(1, ind-1, ind, anga);
Query(1, ind, ind+1, angb);
a = ( a - anga + angb + 720 ) % 360;
a = ( 180 - a + 360 ) % 360;
Updata(1, ind, n, a);
printf("%.2lf %.2lf\n", node[1].pr.x + eps, node[1].pr.y + eps);
}
}
return 0;
}