比赛 |
20150422 |
评测结果 |
AAAAAAAAAA |
题目名称 |
马拉松 |
最终得分 |
100 |
用户昵称 |
清羽 |
运行时间 |
2.644 s |
代码语言 |
C++ |
内存使用 |
7.15 MiB |
提交时间 |
2015-04-22 10:25:17 |
显示代码纯文本
#include<cstdio>
#include<iostream>
#include<cstring>
#include<ctime>
#include<algorithm>
using namespace std;
const int maxn=100050;
const int INF=2147483647;
struct SEG{
int left,right;
int lchild,rchild;
int mxt,sum;//mxt表示跳跃该区间任一个点所取得的最大值
//结点的权值表示上个点到其的距离
void print(){
printf("left=%d,right%d,mxt=%d,sum=%d\n",left,right,mxt,sum);
}
void clear(int x=0,int y=0){
left=x,right=y;
lchild=rchild=-1;
sum=0,mxt=0;
}
SEG(){
clear();
}
};
SEG tree[maxn*3];
int n,q,tot,x[maxn],y[maxn];
int bulid(int x,int y)
{
int p=tot++;
SEG& now=tree[p];
now.clear(x,y);
if(y>x){
int k=(x+y)>>1;
now.lchild=bulid(x,k);
now.rchild=bulid(k+1,y);
}
return p;
}
void update(int root)
{
if(root==-1) return;
SEG& now=tree[root];
if(now.lchild==-1) return;
SEG &lson=tree[now.lchild],&rson=tree[now.rchild];
now.sum=lson.sum+rson.sum;
now.mxt=max(lson.mxt,rson.mxt);
}
void change(int root,int x,int v,int v1)
{
if(root==-1) return;
SEG& now=tree[root];
if(now.left>x || now.right<x) return;
if(now.left==x && now.right==x){
now.sum=v;
now.mxt=v1;
return;
}
change(now.lchild,x,v,v1);
change(now.rchild,x,v,v1);
update(root);
}
int query_sum(int root,int x,int y){
if(root==-1 || x>y) return 0;
SEG& now=tree[root];
if(now.left>y || now.right<x) return 0;
if(now.left>=x && now.right<=y) return now.sum;
return query_sum(now.lchild,x,y)+query_sum(now.rchild,x,y);
}
int query_mxt(int root,int x,int y)
{
if(root==-1 || x>y) return 0;
SEG& now=tree[root];
if(now.left>y || now.right<x) return 0;
if(now.left>=x && now.right<=y) return now.mxt;
return max(query_mxt(now.lchild,x,y),query_mxt(now.rchild,x,y));
}
void init()
{
tot=0;
scanf("%d %d",&n,&q);
bulid(1,n);
for(int i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]);
for(int i=2;i<=n;i++){
int dist=abs(x[i]-x[i-1])+abs(y[i]-y[i-1]);
int dist1=dist+abs(x[i+1]-x[i])+abs(y[i+1]-y[i])-abs(x[i+1]-x[i-1])-abs(y[i+1]-y[i-1]);
if(i==n) dist1=0;
change(0,i,dist,dist1);
}
}
void work()
{
char s[20];
for(int i=0;i<q;i++){
scanf("%s",s);
if(s[0]=='Q'){
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",query_sum(0,x+1,y)-query_mxt(0,x+1,y-1));
}
if(s[0]=='U'){
int pos,x1,y1;
scanf("%d%d%d",&pos,&x1,&y1);
x[pos]=x1;
y[pos]=y1;
for(int j=pos-1;j<=pos+1;j++){
int dist=abs(x[j]-x[j-1])+abs(y[j]-y[j-1]);
int dist1=dist+abs(x[j+1]-x[j])+abs(y[j+1]-y[j])-abs(x[j+1]-x[j-1])-abs(y[j+1]-y[j-1]);
if(j==n || j==1) dist1=0;
if(j==1) dist=0;
change(0,j,dist,dist1);
}
}
}
}
int main()
{
freopen("marathona.in","r",stdin);
freopen("marathona.out","w",stdout);
init();
work();
fclose(stdin);
fclose(stdout);
return 0;
}