ACM-ICPC 2018 沈阳赛区网络预赛 J树分块
J. Ka Chang
Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero point.
Then, you need to handle QQ operations. There're two types:
1\ L\ X1 L X: Increase points by XX of all nodes whose depth equals LL ( the depth of the root is zero ). (x \leq 10^8)(x≤108)
2\ X2 X: Output sum of all points in the subtree whose root is XX.
Input
Just one case.
The first lines contain two integer, N,QN,Q. (N \leq 10^5, Q \leq 10^5)(N≤105,Q≤105).
The next n-1n−1 lines: Each line has two integer aa,bb, means that node aa is the father of node bb. It's guaranteed that the input data forms a rooted tree and node 11 is the root of it.
The next QQ lines are queries.
Output
For each query 22, you should output a number means answer.
样例输入复制
3 3
1 2
2 3
1 1 1
2 1
2 3
样例输出复制
1
0
题目来源
首先这是一个原题,你需要更新深度为x的值,每次仅仅查询子树就可以,用线段树的话,需要更新的太多的
要树分块,打上time标记,每个数据太多不能暴力更新,单独列出来
#include<bits/stdc++.h>
using namespace std;
const int N=;
vector<int>G[N];
vector<int>pos[N];
vector<int>vec;
int L[N],R[N];
int tot,n,m,lim=;
long long s[N],c[N];
void dfs(int now,int deep)
{
L[now]=++tot;//一块的左标记
pos[deep].push_back(L[now]);
for(auto X:G[now])dfs(X,deep+);
R[now]=tot;//右标记,这个内全是其子树
return;
}
void add(int x,int d)
{
for(;x<=n;x+=x&-x)c[x]+=d;
}
long long sum(int x)
{
long long ans=;
for(;x>;x-=x&-x)ans+=c[x];
return ans;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=,u,v;i<n;i++)
scanf("%d%d",&u,&v),G[u].push_back(v);
dfs(,);
for(int i=;i<=n;i++)
if(pos[i].size()>lim)vec.push_back(i);
for(int i=,op,x,y;i<m;i++)
{
scanf("%d%d",&op,&x);
if(op==)
{
scanf("%d",&y);
if(pos[x].size()<=lim)
for(auto X:pos[x])add(X,y);//小于所给块大小,直接树状数组更新
else s[x]+=y;//大于的直接扔进去等更新
}
else
{
long long ans=sum(R[x])-sum(L[x]-);//查询所有小块的值,多余的进行下面的更新
for(auto X:vec)
ans+=(upper_bound(pos[X].begin(), pos[X].end(),R[x])-lower_bound(pos[X].begin(),pos[X].end(),L[x]))*s[X];
//vec的元素不会太多,顶多1e5,直接搞进去更新
printf("%lld\n",ans);
}
}
}
4765: 普通计算姬
Time Limit: 30 Sec Memory Limit: 256 MB
Submit: 1481 Solved: 318
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
Sample Output
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
#define N 100005
typedef unsigned long long ll;
int n,m,q,lim,root,belong[N];
int L[N],R[N],tot,cnt[N];
int g[N][];
ll c[N+N],sum[N],a[N],tag[N];
vector<int>G[N];
void add(int x,int d)
{
for(; x<=n; x+=x&-x)c[x]+=d;
}
ll Sum(int x)
{
ll ans=;
for(; x>; x-=x&-x)ans+=c[x];
return ans;
}
void dfs(int x,int fa)
{
L[x]=++tot,add(L[x],a[x]),++cnt[belong[x]],sum[x]=a[x];
for(int i=; i<=lim; i++)g[x][i]=cnt[i];
int l=G[x].size();
for(int i=,X;i<l;i++)
{
X=G[x][i];
if(X==fa)continue;
dfs(X,x);
sum[x]+=sum[X];
}
R[x]=tot,--cnt[belong[x]],tag[belong[x]]+=sum[x];
}
ll query(int l,int r)
{
ll ans=;
for(int i=l;i<=min(r,belong[l]*m);i++)ans+=Sum(R[i])-Sum(L[i]-);
if(belong[l]!=belong[r])
{
for(int i=(belong[r]-)*m+;i<=r;i++)ans+=Sum(R[i])-Sum(L[i]-);
}
for(int i=belong[l]+;i<=belong[r]-;i++)ans+=tag[i];
return ans;
}
int main()
{
cin>>n>>q;
m=sqrt(n+0.5);//一块有几个
for(int i=; i<=n; i++)cin>>a[i],belong[i]=(i-)/m+;
lim=belong[n];
for(int i=,u,v; i<=n; ++i)
{
cin>>u>>v;
if(!u)root=v;
else G[u].push_back(v),G[v].push_back(u);
}
dfs(root,-);
for(int i=,op,u,v; i<q; i++)
{
cin>>op>>u>>v;
if(op==)
{
for(int i=; i<=lim; i++)tag[i]+=g[u][i]*1LL*(v-a[u]);
add(L[u],v-a[u]),a[u]=v;
}
else cout<<query(u,v)<<"\n";
}
return ;
}
最新文章
- PHP 删除目录及目录下文件
- js小程序写法优化
- cas+shiro实现不时时的去请求cas进行身份验证
- NOIP2013积木大赛
- Robot Framework--01 创建简单工程示例
- Codeforces Gym 100015A Another Rock-Paper-Scissors Problem 找规律
- HTML5 indexedDB数据库的入门学习(二)
- UVALive 4957 Fake scoreboard
- 译文:前端性能的重要性 The Importance of Frontend Performance
- JavaScript调试总结
- ggplot2 aes函数map到data笔记
- Crypto加密解密
- 使用android-ndk官方ndkbuild例子
- RTTI和反射小结
- Ajax的使用~~~整理
- 基于Redis主从复制读写分离架构的Session共享(Windows Server)
- echarts折线图Demo
- design-twitter
- 超全面的JavaWeb笔记day05<;xml&;dtd&;jaxp>;
- 通过IP获取对应所在地的地址