# bzoj1095

## 1095: [ZJOI2007]Hide 捉迷藏

Time Limit: 40 Sec  Memory Limit: 162 MB
Submit: 1602  Solved: 650
[Submit][Status][Discuss]

8

1 2

2 3

3 4

3 5

3 6

6 7

6 8

7

G

C 1

G

C 2

G

C 1

G

4

3

3

4

## HINT

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ls id<<1,l,mid
#define rs id<<1|1,mid+1,r
#define Maxn 100010
using namespace std;

int x=0;
char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return x;
}
struct edge{
int to,next;
}p[Maxn<<1];
int tot;
p[tot].to=v;
}
int dfn[Maxn*3],pos[Maxn*3],c[Maxn*3];
int tmpdfn;
void dfs(int u,int fa){
dfn[++tmpdfn]=-1;
dfn[++tmpdfn]=u;
pos[u]=tmpdfn; //u的时间戳
int v=p[i].to;
if(v==fa) continue;
dfs(v,u);
}
dfn[++tmpdfn]=-2;
}
const int inf=-0x3f3f3f3f;
struct seg{
int lk,rk,la,lb,ra,rb,d;
seg(){}
seg(int _lk,int _rk,int _la,int _lb,int _ra,int _rb,int _d){
lk=_lk,rk=_rk,la=_la,lb=_lb,ra=_ra,rb=_rb,d=_d;
}
seg operator+(seg a){
int LK=lk+max(a.lk-rk,0),RK=a.rk+max(rk-a.lk,0);
int LA=la,LB=lb,RA=a.ra,RB=a.rb; //la前缀和,lb前缀逆差
//a1+b2+abs(b1-a2)
if(a.lb!=inf) LA=max(LA,lk+rk+a.lb);
if(a.la!=inf) LA=max(LA,lk-rk+a.la);
if(ra!=inf) RA=max(RA,ra+a.rk-a.lk);
if(rb!=inf) RA=max(RA,rb+a.rk+a.lk);
//b1+b2-a1-a2
if(a.lb!=inf) LB=max(LB,rk-lk+a.lb);
//a1+a2-b1-b2
if(rb!=inf) RB=max(RB,rb+a.lk-a.rk);
int D=max(d,a.d);
if(ra!=inf&&a.lb!=inf) D=max(D,ra+a.lb);
if(rb!=inf&&a.la!=inf) D=max(D,rb+a.la);
return seg(LK,RK,LA,LB,RA,RB,D);
}
}tr[Maxn*12];
void pushup(int id){
tr[id]=tr[id<<1]+tr[id<<1|1];
}
void set(int id,int l){
tr[id].lk=dfn[l]==-2;
tr[id].rk=dfn[l]==-1;
tr[id].d=inf;
if(dfn[l]>0&&c[l]) tr[id].la=tr[id].lb=tr[id].ra=tr[id].rb=0;
else tr[id].la=tr[id].lb=tr[id].ra=tr[id].rb=inf;
}
void build(int id,int l,int r){
if(l==r){
set(id,l);
return;
}
int mid=l+r>>1;
build(ls);
build(rs);
pushup(id);
}
void update(int id,int l,int r,int a){
if(l==r){
set(id,l);
return;
}
int mid=l+r>>1;
if(a<=mid) update(ls,a);
else update(rs,a);
pushup(id);
}
int main()
{
int n,m,u,v;
tot=0;
for(int i=1;i<n;i++){
}
tmpdfn=0;
dfs(1,0);
for(int i=1;i<=n;i++) c[pos[i]]=1;
build(1,1,tmpdfn);
int num=n;
char s[2];
while(m--){
scanf("%s",s);
if(s[0]=='G'){
if(num==0) puts("-1");
else if(num==1) puts("0");
else printf("%d\n",tr[1].d);
}
else{
if(c[pos[u]]==1) num--;
else num++;
c[pos[u]]^=1;
update(1,1,tmpdfn,pos[u]);
}
}
return 0;
}

﻿﻿

• 评论

• 上一篇
• 下一篇