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 ...
随机推荐
- jQuery之事件和批量操作、事件委托示例
一.常用事件 click(function(){...}) // 点击时触发 focus(function(){...}) // 获得焦点触发 blur(function(){...}) // 失去焦 ...
- Assignment HDU - 2853(求最小改变边数)
Assignment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 在Ubuntu上使用离线方式快速安装K8S v1.11.1
在Ubuntu上使用离线方式快速安装K8S v1.11.1 0.安装包文件下载 https://pan.baidu.com/s/1nmC94Uh-lIl0slLFeA1-qw v1.11.1 文件大小 ...
- 连接SQL Server数据库
SqlConnection来连接数据库,注意数据库目标的格式. using System.Data.SqlClient;//载入数据库命名空间 namespace WindowsFormsApplic ...
- (最短路 Floyd) P2910 [USACO08OPEN]寻宝之路Clear And Present Danger 洛谷
题意翻译 题目描述 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛. 一张藏宝图上说,如果他的路程上经过的小岛依次出现了 ...
- LINQ To SQL 语法及实例大全【转】
转http://blog.csdn.net/pan_junbiao/article/details/7015633 LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查 ...
- 横向滚动布局 white-space:nowrap
float + 两层DOM实现 html <div class="container"> <div class="div1 clearfix" ...
- [物理学与PDEs]第3章第2节 磁流体力学方程组 2.4 不可压情形的磁流体力学方程组
不可压情形的磁流体力学方程组 $$\beex \bea \cfrac{\rd {\bf H}}{\rd t}-({\bf H}\cdot\n){\bf u}&=\cfrac{1}{\sigma ...
- end to end testing
概念 https://www.softwaretestinghelp.com/what-is-end-to-end-testing/ What is “End to End Testing”? Ter ...
- docker学习-----docker可视化portainer
docker的可视化操作界面portainer 1.创建一个挂载区 docker volume create portainer_data 2.安装( docker run -d - ...