BZOJ 4999: This Problem Is Too Simple! DFS序+LCA+树状数组+离线
Description
Input
Output
#include<bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin) , freopen(s".out","w",stdout)
#define maxn 300000
using namespace std;
struct OPT
{
int type;
int a,b,c,tag;
OPT(int type=0,int a=0,int b=0,int c=0,int tag=0):type(type),a(a),b(b),c(c),tag(tag){}
}opt[maxn];
vector<OPT>G[maxn];
struct BIT
{
#define N 300000
int C[maxn];
int lowbit(int t)
{
return t & (-t);
}
void update(int x,int delta)
{
while(x<N)
{
C[x]+=delta;
x+=lowbit(x);
}
}
int query(int x)
{
int tmp=0;
while(x>0)
{
tmp+=C[x];
x-=lowbit(x);
}
return tmp;
}
}tree;
char str[10];
int n,Q,edges,tot,tim,mkk=0;
int col[maxn],Arr[maxn],hd[maxn],to[maxn<<1],nex[maxn<<1],dfn[maxn],ln[maxn],st[maxn],ed[maxn];
int siz[maxn],hson[maxn],dep[maxn],top[maxn],fa[maxn],answer[maxn];
void add(int u,int v)
{
nex[++edges]=hd[u],hd[u]=edges,to[edges]=v;
}
void dfs1(int u,int ff)
{
ln[++tim]=u,dfn[u]=st[u]=tim,siz[u]=1,dep[u]=dep[ff]+1,fa[u]=ff;
for(int i=hd[u];i;i=nex[i])
{
int v=to[i];
if(v==ff) continue;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[v]>siz[hson[u]]) hson[u]=v;
}
ed[u]=tim;
}
void dfs2(int u,int tp)
{
top[u]=tp;
if(hson[u]) dfs2(hson[u],tp);
for(int i=hd[u];i;i=nex[i])
{
int v=to[i];
if(v==fa[u]||v==hson[u]) continue;
dfs2(v,v);
}
}
int LCA(int x,int y)
{
while(top[x]^top[y]) dep[top[x]] > dep[top[y]] ? x = fa[top[x]] : y = fa[top[y]];
return dep[x]<dep[y]?x:y;
}
void solve(int cur)
{
for(int i=0,sz=G[cur].size();i<sz;++i)
{
OPT cn=G[cur][i];
switch(cn.type)
{
case -1:
{
int u=cn.a;
tree.update(st[u], -1);
tree.update(ed[u]+1, +1);
break;
}
case 1 :
{
int u=cn.a;
tree.update(st[u], 1);
tree.update(ed[u]+1, -1);
break;
}
case 2 :
{
int u=cn.a;
int v=cn.b;
int c=cn.c;
int g=cn.tag;
int lca = LCA(u,v);
answer[g]=tree.query(dfn[u])+tree.query(dfn[v])-tree.query(dfn[lca])-tree.query(dfn[fa[lca]]);
break;
}
}
}
for(int i=0,sz=G[cur].size();i<sz;++i)
{
OPT cn=G[cur][i];
if(cn.type==-1) tree.update(st[cn.a], +1), tree.update(ed[cn.a]+1,-1);
if(cn.type==1) tree.update(st[cn.a], -1), tree.update(ed[cn.a]+1, +1);
}
}
int main()
{
// setIO("input");
scanf("%d%d",&n,&Q);
for(int i=1;i<=n;++i) scanf("%d",&col[i]),Arr[++tot]=col[i];
for(int i=1;i<n;++i)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y), add(y,x);
}
dfs1(1,0);
dfs2(1,1);
for(int i=1;i<=Q;++i)
{
scanf("%s",str);
switch(str[0])
{
case 'C' :
{
opt[i].type=1;
scanf("%d%d",&opt[i].a,&opt[i].b);
Arr[++tot]=opt[i].b;
break;
}
case 'Q' :
{
opt[i].type=2;
scanf("%d%d%d",&opt[i].a,&opt[i].b,&opt[i].c);
Arr[++tot]=opt[i].c;
break;
}
}
}
sort(Arr+1,Arr+1+tot);
for(int i=1;i<=Q;++i)
{
if(opt[i].type==1) opt[i].b=lower_bound(Arr+1,Arr+1+tot,opt[i].b)-Arr;
if(opt[i].type==2) opt[i].c=lower_bound(Arr+1,Arr+1+tot,opt[i].c)-Arr;
}
for(int i=1;i<=n;++i)
{
col[i]=lower_bound(Arr+1,Arr+1+tot,col[i])-Arr;
G[col[i]].push_back(OPT(1,i,0,0,0));
}
for(int i=1;i<=Q;++i)
{
if(opt[i].type==2)
{
OPT P=opt[i];
P.tag=++mkk;
G[opt[i].c].push_back(P);
}
else
{
int u=opt[i].a,v=opt[i].b;
G[col[u]].push_back(OPT(-1,u,0,0,0));
G[v].push_back(OPT(1,u,0,0,0));
col[u]=v;
}
}
for(int i=1;i<maxn;++i)
{
if(G[i].size()) solve(i);
}
for(int i=1;i<=mkk;++i)
{
printf("%d\n",answer[i]);
}
return 0;
}
BZOJ 4999: This Problem Is Too Simple! DFS序+LCA+树状数组+离线的更多相关文章
- POJ 2763 Housewife Wind(DFS序+LCA+树状数组)
Housewife Wind Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 11419 Accepted: 3140 D ...
- HDU 6203 ping ping ping(dfs序+LCA+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=6203 题意: n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V 无法连 ...
- HDU 3966 dfs序+LCA+树状数组
题目意思很明白: 给你一棵有n个节点的树,对树有下列操作: I c1 c2 k 意思是把从c1节点到c2节点路径上的点权值加上k D c1 c2 k 意思是把从c1节点到c2节点路径上的点权值减去k ...
- BZOJ 2819: Nim dfs序维护树状数组,倍增
1.随机选两个堆v,u,询问若在v到u间的路径上的石子堆中玩Nim游戏,是否有必胜策略,如果有,vfleaking将会考虑将这些石子堆作为初始局面之一,用来坑玩家.2.把堆v中的石子数变为k. 分析: ...
- BZOJ 1103: [POI2007]大都市meg(dfs序,树状数组)
本来还想链剖的,结果才发现能直接树状数组的= = 记录遍历到达点与退出点的时间,然后一开始每个到达时间+1,退出时间-1,置为公路就-1,+1,询问直接点1到该点到达时间求和就行了- - CODE: ...
- 2018.06.30 BZOJ4765: 普通计算姬(dfs序+分块+树状数组)
4765: 普通计算姬 Time Limit: 30 Sec Memory Limit: 256 MB Description "奋战三星期,造台计算机".小G响应号召,花了三小时 ...
- 【Tyvj2133&BZOJ1146】网络管理Network(树套树,DFS序,树状数组,主席树,树上差分)
题意:有一棵N个点的树,每个点有一个点权a[i],要求在线实现以下操作: 1:将X号点的点权修改为Y 2:查询X到Y的路径上第K大的点权 n,q<=80000 a[i]<=10^8 思路: ...
- 【POJ3321】Apple Tree(DFS序,树状数组)
题意:给一棵n个节点的树,每个节点开始有一个苹果,m次操作 1.将某个结点的苹果数异或 1 2.查询一棵子树内的苹果数 n,m<=100000 思路:最近一段时间在思考树上统计问题的算法 发 ...
- 【BZOJ1103】大都市meg(DFS序,树状数组)
题意:有一颗树,1号点为根,保证编号小的点深度较小,初始状态每条边都没有被标记,要求实现两个操作在线: A:将连接x,y的边标记 W:查询从1到x的路径上有多少条边未被标记 n<=2*10^5 ...
随机推荐
- [luogu2209][USACO13]燃油经济性Fuel Economy_贪心
燃油经济性Fuel Economy 题目大意:FJ想要去旅行.他的车总容量为G,每行驶一个单位就消耗一个单位的油.FJ要行驶D个单位的距离.期间存在n个加油站,每个加油站有一个价格,表示在这个燃油站买 ...
- 洛谷 P1491 集合位置
P1491 集合位置 题目描述 每次有大的活动,大家都要在一起“聚一聚”,不管是去好乐迪,还是避风塘,或者汤姆熊,大家都要玩的痛快.还记得心语和花儿在跳舞机上的激情与释放,还记得草草的投篮技艺是如此的 ...
- MVC.Net:添加第三方类库的bundles引用
以jQuery Mobile为例.当我们用nuget添加jQuery Mobile的package后,需要在项目中引用jQuery Mobile.传统的做法是在html页面上添加 <link h ...
- 关于Openstack的浅层次认知
Openstack浅析 英文好的应该直接跳到官方文档去看相关的介绍,以下是具体介绍的连接,包含Openstack的具体架构: http://docs.openstack.org/kilo/instal ...
- jvm的运行模式 client和 server两种
jvm的运行模式 client和 server两种 学习了:https://www.cnblogs.com/fsjohnhuang/p/4270505.html 在jdk 9的情况下,好像没有clie ...
- Hibernate学习笔记(八) — 懒载入与抓取策略
懒载入(Load On Demand)是一种独特而又强大的数据获取方法,它可以在用户滚动页面的时候自己主动获取很多其它的数据,而新得到的数据不会影响原有数据的显示,同一时候最大程度上降低server端 ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理解法
一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-p ...
- Office 365 的公共网站的一些限制及解决的办法
当前的SharePoint Online版本是基于SharePoint 2013的,但是很多功能确被阉割掉了. 下面主要列出Office 365公共网站被限制的功能,以及可绕过的解决方案: 内容类型 ...
- 关于isset的一点说明
作者:zhanhailiang 日期:2014-10-08 今天遇到一个非常奇怪的bug,測试例如以下: <? php $a = 'abc'; var_dump(isset($a['code'] ...
- android測试工具MonkeyRunner--google官网翻译
近期在复习之前的笔记,在回想MonkeyRunner时看了看google官网的内容,写得不错.就翻译出来分享下.事实上google官网真是一个学习的好地方. 基础知识 MonkeyRunner工具提供 ...