这道题如果用动态开点的线段树时间非常的卡,我写的常数太大根本卡不过去。。。。
貌似比较好的打开方式是把线段树改成树状数组。。。
以下代码是TLE的。。。
#include
#include
#include
#include
#include
#define N 100003
#define inf 1000000000
using namespace std;
struct data{
int ls,rs,sum;
}tr[N*30],tr1[N*30];
int rt[N],rtc[N],tot,point[N],nxt[N*2],v[N*2],deep[N],fa[N*2][30],mi[30];
int belong[N],size[N],f[N],sum,root,vis[N],sz,n,m,ans,val[N],d[N],L[N*2],ti,pos[N*2];
int read() {
char ch; while (!((ch=getchar())>='0'&&ch<='9'));
int a=ch-'0';
while ((ch=getchar())>='0'&&ch<='9') a=a*10+ch-'0';
return a;
}
void add(int x,int y)
{
tot++; nxt[tot]=point[x]; point[x]=tot; v[tot]=y;
tot++; nxt[tot]=point[y]; point[y]=tot; v[tot]=x;
}
/*void dfs(int x,int father)
{
deep[x]=deep[father]+1;
for (int i=1;i<=17;i++) {
if (deep[x]-mi[i]<0) break;
fa[x][i]=fa[fa[x][i-1]][i-1];
}
for (int i=point[x];i;i=nxt[i]) {
if(v[i]==father) continue;
fa[v[i]][0]=x;
dfs(v[i],x);
}
}
int lca(int x,int y)
{
if (deep[x]y]) swap(x,y);
int k=deep[x]-deep[y];
for (int i=0;i<=17;i++)
if ((k>>i)&1) x=fa[x][i];
if (x==y) return x;
for (int i=17;i>=0;i--)
if(fa[x][i]!=fa[y][i])
x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
int dis(int x,int y)
{
return deep[x]+deep[y]-2*deep[lca(x,y)];
}*/
void dfs(int x, int father)
{
fa[pos[x]=++ti][0]=deep[x]=deep[father]+1;
for (int i=point[x];i;i=nxt[i])
if (v[i]^father)
dfs(v[i],x),fa[++ti][0]=deep[x];
}
int lca(int x,int y)
{
x=pos[x]; y=pos[y];
if (x>y) swap(x,y);
int len=L[y-x+1];
return min(fa[x][len],fa[y-(1<1][len]);
}
int dis(int x, int y) {
return deep[x]+deep[y]-(lca(x,y)<<1);
}
void getroot(int x,int father)
{
f[x]=0; size[x]=1;
for (int i=point[x];i;i=nxt[i]){
if (vis[v[i]]||v[i]==father) continue;
getroot(v[i],x);
size[x]+=size[v[i]];
f[x]=max(f[x],size[v[i]]);
}
f[x]=max(f[x],sum-size[x]);
if (f[x]x;
}
void divi(int x,int father)
{
belong[x]=father; vis[x]=1;
for (int i=point[x];i;i=nxt[i]){
if (vis[v[i]]) continue;
root=0; sum=size[v[i]];
getroot(v[i],x);
divi(root,x);
}
}
void insert(int &i,int l,int r,int x,int val)
{
if (!i) i=++sz; tr[i].sum+=val;
if (l==r) return;
int mid=(l+r)/2;
if (x<=mid) insert(tr[i].ls,l,mid,x,val);
else insert(tr[i].rs,mid+1,r,x,val);
}
void change(int u,int val)
{
insert(rt[u],0,n,0,val);
for(int i=u;belong[i];i=belong[i])
{
int D=dis(u,belong[i]);
insert(rt[belong[i]],0,n,D,val);
insert(rtc[i],0,n,D,val);
}
}
int qjsum(int i,int l,int r,int ll,int rr)
{
if (ll>rr) return 0;
if(ll<=l&&r<=rr) return tr[i].sum;
int mid=(l+r)/2; int ans=0;
if (ll<=mid) ans+=qjsum(tr[i].ls,l,mid,ll,rr);
if (rr>mid) ans+=qjsum(tr[i].rs,mid+1,r,ll,rr);
return ans;
}
void calc(int u,int d)
{
ans+=qjsum(rt[u],0,n,0,d);
for(int i=u;belong[i];i=belong[i])
{
int D=dis(belong[i],u);
ans+=qjsum(rt[belong[i]],0,n,0,d-D);
ans-=qjsum(rtc[i],0,n,0,d-D);
}
}
int main()
{
freopen("a.in","r",stdin);
freopen("my.out","w",stdout);
n=read(); m=read(); mi[0]=1;
for (int i=1;i<=17;i++) mi[i]=mi[i-1]*2;
for (int i=1;i<=n;i++) val[i]=read();
for (int i=1;iint x,y; x=read(); y=read();
add(x,y);
}
dfs(1,0);
for (int i=2;i<=ti;i++) L[i]=L[i>>1]+1;
for (int j=1;j<=L[ti];j++)
for (int i=1;i+(1<1<=ti;i++)
fa[i][j]=min(fa[i][j-1],fa[i+(1<1)][j-1]);
root=0; f[0]=inf; sum=n;
getroot(1,0); divi(root,0); ans=0;
// for (int i=1;i<=n;i++) cout<" "; cout<for (int i=1;i<=n;i++)
change(i,val[i]);
for (int i=1;i<=m;i++) {
//cout<int opt,x,y; opt=read(); x=read(); y=read();
//x^=ans; y^=ans;
if (opt==1) {
change(x,y-val[x]);
val[x]=y;
}
if (opt==0) {
ans=0;
calc(x,y);
printf("%d\n",ans);
}
}
}