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> ...
随机推荐
- echart 判断数据是否为空
formatter 判断数据是否为空
- SqlServer 行转列,列转行 以及PIVOT函数快速实现行转列,UNPIVOT实现列转行
一 .列转行 创建所需的数据 CREATE TABLE [StudentScores]( [UserName] NVARCHAR(20), --学生姓名 [Subject] NVARCHAR(3 ...
- opencart3如何安装模板
opencart 3模板采用twig模式,安装模板也有点不大一样,随ytkah一起来看看opencart3如何安装模板吧1.下载模板文件,用ftp上传到对应的位置,一般有几个文件夹,比如:admin. ...
- 用maven创建一个web项目
下面所使用的Eclipse开发工具为Eclipse Java EE IDE 版本. 1.创建一个maven项目,如图所示: 选择“maven-archetype-webapp”,如图所示: 后面几步按 ...
- 【LeetCode每天一题】Merge Intervals(合并区间)
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- Python3.6安装使用tesserocr文件时遇到问题
本机运行环境: Win 10 version 1709; Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit ...
- [Python] Print input and output in table
Print the input and output in a table using prettyTable. from prettytable import PrettyTable import ...
- python常用函数总结
原文地址https://www.cnblogs.com/nice107/p/8118876.html 我们在学习python的时候,接触最多的往往则是那些函数,对于python函数,在这里为大家总结归 ...
- Mybatis(二)总结
1. 输入映射(就是映射文件中可以传入哪些参数类型) 1)基本类型 2)pojo类型 3)Vo类型2. 输出映射(返回的结果集可以有哪些类型) 1)基本类型 2)pojo类型 3)List类型3. 动 ...
- Spring框架第二天
## Spring框架第二天 ## ---------- **课程回顾:Spring框架第一天** 1. 概述 * IOC和AOP 2. 框架的IOC的入门 * 创建applicationContex ...