Codeforces 513D2 Constrained Tree
没写出来好菜啊啊。
首先根据输入我们能算出某些节点的左儿子的范围, 右儿子的范围(此时并不准确)
然后我们在划分u这个节点的时候我们从左右开始用树状数组check每一个点是否可行, 即这个点没有被覆盖,
因为左右同时开始所以复杂度是nlognlogn,以前做过这种从两头开始check的。
还有一种方法用线段树, 从n - > 1取更新每个点的准确范围,然后直接输出答案。
还有一种方法是dfs的过程中, 先给所子树划分一个区域, 这个区域可能是不对的,然后递归左子树, 能到正确的区域才去递归右子树。
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);
using namespace std; const int N = 1e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} struct Bit {
int a[N];
void modify(int x, int v) {
for(int i = x; i < N; i += i & -i)
a[i] += v;
}
int sum(int x) {
int ans = ;
for(int i = x; i; i -= i & -i)
ans += a[i];
return ans;
}
}; int n, m;
char s[];
vector<PII> vc[N];
vector<int> ans;
Bit bit; void dfs(int l, int r) {
if(l > r) return;
if(l == r) {
ans.push_back(l);
return;
}
int L = l, R = r;
for(auto& t : vc[l]) {
if(t.se) chkmin(R, t.fi - );
else chkmax(L, t.fi);
bit.modify(l, -);
bit.modify(t.fi, );
}
for( ; L <= R; L++, R--) {
if(!bit.sum(L)) {
dfs(l + , L);
ans.push_back(l);
dfs(L + , r);
return;
}
if(!bit.sum(R)) {
dfs(l + , R);
ans.push_back(l);
dfs(R + , r);
return;
}
}
puts("IMPOSSIBLE");
exit();
} int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= m; i++) {
int a, b;
scanf("%d%d%s", &a, &b, s);
if(b <= a) {
puts("IMPOSSIBLE");
return ;
}
if(s[] == 'L') vc[a].push_back(mk(b, ));
else vc[a].push_back(mk(b, ));
bit.modify(a, );
bit.modify(b, -);
}
dfs(, n);
for(auto& t : ans) printf("%d ", t);
puts("");
return ;
} /*
*/
Codeforces 513D2 Constrained Tree的更多相关文章
- Problem - D - Codeforces Fix a Tree
Problem - D - Codeforces Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...
- Codeforces 765 E. Tree Folding
题目链接:http://codeforces.com/problemset/problem/765/E $DFS子$树进行$DP$ 大概分以下几种情况: 1.为叶子,直接返回. 2.长度不同的路径长度 ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- CodeForces 383C Propagating tree
Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
- codeforces 375D:Tree and Queries
Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...
- 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 ...
随机推荐
- springboot连接数据库报错testWhileIdle is true, validationQuery not set
问题描述: 使用springboot连接数据库,启动的时候报错:testWhileIdle is true, validationQuery not set.但是不影响系统使用,数据库等一切访问正常. ...
- kubernetes 安装metrics-server
metrics-server文件下载: https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/metrics-serv ...
- luogu4770 [NOI2018]你的名字 (SAM+主席树)
对S建SAM,拿着T在上面跑 跑的时候不仅无法转移要跳parent,转移过去不在范围内也要跳parent(注意因为范围和长度有关,跳的时候应该把长度一点一点地缩) 这样就能得到对于T的每个前缀,它最长 ...
- Matlab 中movie函数的使用
MATLAB中,创建电影动画的过程分为以下四步: step1:调用moviein函数对内存进行初始化(该步骤在Matlab5.3以上均可省略),创建一个足够大的矩阵,使之能够容纳基于当前坐标轴大小的一 ...
- CF914G Sum the Fibonacci
解:发现我们对a和b做一个集合卷积,对d和e做一个^FWT,然后把这三个全部对位乘上斐波那契数,然后做&FWT就行了. #include <bits/stdc++.h> , MO ...
- 金融量化分析【day110】:Pandas-DataFrame索引和切片
一.实验文档准备 1.安装 tushare pip install tushare 2.启动ipython C:\Users\Administrator>ipython Python 3.7.0 ...
- crm 添加用户 编辑用户 公户和私户的展示,公户和私户的转化
1.添加用户 和编辑可以写在一起 urls.py url(r'^customer_add/', customer.customer_change, name='customer_add'), url( ...
- dubbo本地服务化实现(dubbo三)
一.dubbo服务化架构包含的内容 对于传统工程而言,分层的依据是按照包来区分.由于在相同的工程中,所以服务的提供和调用可以方便的实现. 但是对于分布式架构而言,服务的提供者负责服务具体的实现和接口规 ...
- Typora使用说明(记录总结)
目录 区域元素 YAML FONT Matters 菜单 段落 标题 引注 序列 可选序列 代码块 数学块 表格 脚注 水平线 特征元素 链接 超链接 内链接 相关链 URLs 图片 斜体 加粗 删除 ...
- Pandas的一些简单函数总结
pd.Series(my_list) # 从一个可迭代的对象 my_list 中创建一个数据组df.index = pd.date_range('2017/1/1', periods=df.shape ...