codeforces 343d
题意:一棵树结构上有水,往一个节点加水,那么所有的子节点都会有水,或者排干一个节点的水,那么它的上面的节点都会没水。
用dfs序,数组记录区间内全部有水为1,区间内有没水的点就为0。
倒水:区间更新,排水:单点更新,并更新途中经过的所有点,查询:区间查询。
倒水:区间内所有的点变为有水,就是1,用lazy数组,要下传。
排水:所有节点的dfs序之间的相交只能是包含,并且只被祖先包含。那么在线段树中就将经过的点全部排去。这样祖先节点查询时就为0了,并且无关节点还是有水。不过这样有个BUG,假如开始全有水,如果一个节点排空了立即加水,那么祖先又有水了,实际上是没水,这时将祖先排水就行。
6
2 1
3 1
4 1
4 5
2 6
4
1 1
2 4
1 4
3 1就这。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#include <unordered_set>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,SSZ=*SZ,APB=,INF=0x7FFFFFFF,mod=;
lon n,bg[SZ],fn[SZ],cnt,arr[SSZ],lazy[SSZ];
lon pre[SZ];
vector<int> mp[SZ]; void release()
{ } void dfs(int x,int p)
{
bg[x]=++cnt;
pre[x]=p;
for(int i=;i<mp[x].size();++i)
{
int to=mp[x][i];
if(to!=p)
{
dfs(to,x);
}
}
fn[x]=cnt;
} void pushdown(int rt)
{
if(lazy[rt])
{
lazy[rt*]=lazy[rt*+]=;
arr[rt*]=arr[rt*+]=;
lazy[rt]=;
}
} void pushup(int rt)
{
arr[rt]=arr[rt*]*arr[rt*+];
} void update(int ll,int rr,int rt,int ql,int qr)
{
int mid=(ll+rr)/;
pushdown(rt);
if(ql<=ll&&rr<=qr)
{
arr[rt]=;
lazy[rt]=;
return;
}
if(ll>rr)return;
if(ql<=mid)update(ll,mid,rt*,ql,qr);
if(qr>mid)update(mid+,rr,rt*+,ql,qr);
pushup(rt);
} void update(int ll,int rr,int rt,int pos)
{
int mid=(ll+rr)/;
pushdown(rt);
if(ll==rr)
{
arr[rt]=;
return;
}
if(pos<=mid)update(ll,mid,rt*,pos);
else update(mid+,rr,rt*+,pos);
pushup(rt);
} int qry(int ll,int rr,int rt,int ql,int qr)
{
//cout<<" "<<ll<<" "<<rr<<endl;
int mid=(ll+rr)/;
pushdown(rt);
if(ql<=ll&&rr<=qr)
{
return arr[rt];
}
if(ll>rr)return ;
int res1=,res2=;
if(ql<=mid)res1=qry(ll,mid,rt*,ql,qr);
if(qr>mid)res2=qry(mid+,rr,rt*+,ql,qr);
return res1*res2;
} void init()
{
cin>>n;
for(int i=;i<=n-;++i)
{
int a,b;
cin>>a>>b;
mp[a].push_back(b);
mp[b].push_back(a);
}
dfs(,-);
} void work()
{
int qnum;
cin>>qnum;
for(int i=;i<=qnum;++i)
{
int type,x;
cin>>type>>x;
if(type==)
{
if(x!=&&!qry(,cnt,,bg[x],fn[x]))update(,cnt,,bg[pre[x]]);
update(,cnt,,bg[x],fn[x]);
}
else if(type==)
{
update(,cnt,,bg[x]);
}
else
{
//cout<<"3: "<<bg[x]<<" "<<fn[x]<<endl;
cout<<qry(,cnt,,bg[x],fn[x])<<endl;
}
}
} int main()
{
//std::ios::sync_with_stdio(0);
//freopen("d:\\2.txt","r",stdin);
//freopen("d:\\3.txt","w",stdout);
lon casenum;
//cin>>casenum;
//cout<<casenum<<endl;
//for(lon time=1;time<=casenum;++time)
//for(lon time=1;scanf("%d%d",&n,&m)!=EOF;++time)
{
init();
work();
release();
}
return ;
}
codeforces 343d的更多相关文章
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- CodeForces - 343D 树链剖分
题目链接:http://codeforces.com/problemset/problem/343/D 题意:给定一棵n个n-1条边的树,起初所有节点权值为0,然后m个操作. 1 x:把x为根的子树的 ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
- Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14:38 98人阅读 评论(0) 收藏
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- CodeForces 343D 线段树维护dfs序
给定一棵树,初始时树为空 操作1,往某个结点注水,那么该结点的子树都注满了水 操作2,将某个结点的水放空,那么该结点的父亲的水也就放空了 操作3,询问某个点是否有水 我们将树进行dfs, 生成in[u ...
- Codeforces 343D WaterTree - 线段树, DFS序
Description Translated by @Nishikino_Maki from Luogu 行吧是我翻的 Mad scientist Mike has constructed a roo ...
- codeforces 343D 树剖后odt维护
子树修改+路径修改+单点查询 树链剖分+区间维护即可 由于只有单点查询,我直接用了odt,复杂度还行 #include<bits/stdc++.h> #define endl '\n' # ...
- CodeForces 343D water tree(树链剖分)
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- Codeforces 343D Water Tree
题意简述 维护一棵树,支持以下操作: 0 v:将以v为跟的子树赋值为1 1 v:将v到根节点的路径赋值为0 2 v:询问v的值 题解思路 树剖+珂朵莉树 代码 #include <set> ...
随机推荐
- 20190422 DW/BI系统
其实很多人都不能理解操作型应用系统和分析型系统的意义,主要问题在于这两个系统所面对的用户和需求是不相同的. 建模重点“凡事应该简单,简单到不能再简单”
- 实验二《Java面向对象程序设计》的一点说明
实验二<Java面向对象程序设计>的一点说明 本周的实验要求是: 完成实验二<Java面向对象程序设计>中的内容 其中Eclipse相关的内容参考Intellj IDEA 简易 ...
- 判断np.array里面为空字符串的方法
#多在编译器里尝试新操作 import numpy as np for i range(100): eval1 = {"A": ''"} eval2 = {"A ...
- ndarray的用法总结
#发现ndarray的一维,二维都可以用[i][j], 它们都是下标索引的连用, 比如j表示第j个元素;#二维ndarray可以用[m, n]来进行行列的操作,类似matlab中的用法.取某一列是[: ...
- 关于${pageContext.request.contextPath}的理解 (转载)
${pageContext.request.contextPath}是JSP取得绝对路径的方法,等价于<%=request.getContextPath()%> . 也就是取出部署的应用程 ...
- React时间组件(时分秒补0)页面全局引用
1.common.js export function formatTime(data){ var d = new Date(data); function doTime(d){ if(d<10 ...
- JAVA:IDEA使用Hibernate(2)
1.打开IDEA新建项目 勾选Hibernate.勾选下方生成相关配置和类 下方选择Download,如果使用本地已经下载的包则选择 Use Library ,路径选到包里面的require里导入即可 ...
- Windows平台搭建Kafka
1. 安装JDK 1.1 安装文件:http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.htm ...
- Spark实时案例
1.概述 最近有同学问道,除了使用 Storm 充当实时计算的模型外,还有木有其他的方式来实现实时计算的业务.了解到,在使用 Storm 时,需要编写基于编程语言的代码.比如,要实现一个流水指标的统计 ...
- 使用dbeaver查mysql的表会导致锁表的问题
查询完成之后接着需要使用rollback,不然其它session没法执行语句.