# 【BZOJ-3730】震波 动态点分治 + 树状数组

## 3730: 震波

Time Limit: 15 Sec  Memory Limit: 256 MB
Submit: 626  Solved: 149
[Submit][Status][Discuss]

## Description

0 x k 表示发生了一次地震，震中城市为x，影响范围为k，所有与x距离不超过k的城市都将受到影响，该次地震造成的经济损失为所有受影响城市的价值和。
1 x y 表示第x个城市的价值变成了y。

## Sample Input

8 1
1 10 100 1000 10000 100000 1000000 10000000
1 2
1 3
2 4
2 5
3 6
3 7
3 8
0 3 1

11100101

## HINT

1<=N,M<=100000
1<=u,v,x<=N
1<=value[i],y<=10000
0<=k<=N-1

## Code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
{
int x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN 100010
int N,M,val[MAXN],lastans;

namespace Tree{
struct EdgeNode{
int next,to;
}edge[MAXN<<1];

int deep[MAXN],dist[MAXN],father[18][MAXN];
inline void DFS(int now,int last)
{
for (int i=1; i<=17; i++)
if (deep[now]>=(1<<i))
father[i][now]=father[i-1][father[i-1][now]];
else
break;
if (edge[i].to!=last) {
deep[edge[i].to]=deep[now]+1;
dist[edge[i].to]=dist[now]+1;
father[0][edge[i].to]=now;
DFS(edge[i].to,now);
}
}

inline int LCA(int x,int y)
{
if (deep[x]<deep[y]) swap(x,y);
int dd=deep[x]-deep[y];
for (int i=0; i<=17; i++)
if (dd&(1<<i)) x=father[i][x];
for (int i=17; i>=0; i--)
if (father[i][x]!=father[i][y])
x=father[i][x],y=father[i][y];
return x==y? x:father[0][x];
}

inline int Dist(int x,int y)
{
int z=LCA(x,y);
return deep[x]+deep[y]-deep[z]-deep[z];
}
}using namespace Tree;

namespace BIT{
typedef vector<int> vec;
struct BIT{
vec tree; int n;
inline void init(int size) {tree.resize(size+2); n=size+1;}
inline int lowbit(int x) {return x&-x;}
inline void Modify(int x,int d) {if (x<=0) return; for (int i=x; i<=n; i+=lowbit(i)) tree[i]+=d;}
inline int Query(int x) {int re=0; if (x>n) x=n; for (int i=x; i>0; i-=lowbit(i)) re+=tree[i]; return re;}
}f[MAXN],g[MAXN];
}using namespace BIT;

namespace TreeDivide{

int size[MAXN],mx[MAXN],root,Sz,par[MAXN];
bool visit[MAXN];

inline void Getroot(int now,int last)
{
size[now]=1,mx[now]=0;
if (edge[i].to!=last && !visit[edge[i].to]) {
Getroot(edge[i].to,now);
size[now]+=size[edge[i].to];
mx[now]=max(mx[now],size[edge[i].to]);
}
mx[now]=max(mx[now],Sz-size[now]);
if (mx[now]<mx[root]) root=now;
}

inline void DFS(int now,int last,int dep)
{
f[root].Modify(dep+1,val[now]);
g[par[root]].Modify(dep+1,val[now]);
if (edge[i].to!=last && !visit[edge[i].to]) {
DFS(edge[i].to,now,dep+1);
}
}

inline void Divide(int now)
{
//      printf("Divide = %d\n",now);

visit[now]=1;

g[now].Modify(1,val[now]);

if (!visit[edge[i].to]) {
root=0;
Sz=size[edge[i].to];
Getroot(edge[i].to,now);
par[root]=now;
f[root].init(Sz),g[root].init(Sz);
DFS(edge[i].to,now,1);
Divide(root);
}
}

inline void Modify(int x,int y)
{
for (int i=x; i; i=par[i]) {
int dep=Tree::Dist(x,i)+1;
g[i].Modify(dep,y-val[x]);
if (par[i])
dep=Tree::Dist(par[i],x)+1,f[i].Modify(dep,y-val[x]);
}
val[x]=y;
}

inline int Query(int x,int k)
{
int ans=0;
for (int i=x; i; i=par[i]) {
int dep=k-Tree::Dist(x,i)+1;
ans+=g[i].Query(dep);
if (par[i])
dep=k-Tree::Dist(x,par[i])+1,ans-=f[i].Query(dep);
}
return ans;
}
}using namespace TreeDivide;

int main()
{
for (int i=1; i<=N; i++) val[i]=read();

Tree::DFS(1,0);

Sz=mx[root=0]=N;
Getroot(1,0);
f[root].init(Sz),g[root].init(Sz);
Divide(root);

while (M--) {
x^=lastans,y^=lastans;
if (opt==1) {
TreeDivide::Modify(x,y);
} else {
printf("%d\n",lastans=TreeDivide::Query(x,y));
}
}
return 0;
}



——It's a lonely path. Don't make it any lonelier than it has to be.
posted @ 2017-01-26 23:33  DaD3zZ  阅读(630)  评论(0编辑  收藏  举报