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的更多相关文章

  1. Problem - D - Codeforces Fix a Tree

    Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...

  2. Codeforces 765 E. Tree Folding

    题目链接:http://codeforces.com/problemset/problem/765/E $DFS子$树进行$DP$ 大概分以下几种情况: 1.为叶子,直接返回. 2.长度不同的路径长度 ...

  3. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  4. CodeForces 383C Propagating tree

    Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

  5. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  8. codeforces 375D:Tree and Queries

    Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  9. 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 ...

随机推荐

  1. Nodejs的安装配置及如何在sublimetext2中运行js

    Nodejs的安装配置及如何在sublimetext2中运行js听语音 | 浏览:4554 | 更新:2015-06-16 11:29 Nodejs的安装配置及如何在sublimetext2中运行js ...

  2. 修改帝国cms栏目后,如何更新

    修改栏目后,要依次做如下更新: 1.    2.  3.    如果只是修改了栏目里的属性,只需要做第三步就行了

  3. python版接口自动化测试框架源码完整版(requests + unittest)

    python版接口自动化测试框架:https://gitee.com/UncleYong/my_rf [框架目录结构介绍] bin: 可执行文件,程序入口 conf: 配置文件 core: 核心文件 ...

  4. 【nginx】nginx的工作模式和信号量控制

    nginx是一个多进程/多线程高性能web服务器,在linux系统中,nginx启动后会以后台守护进程(daemon)的方式去运行,后台进程包含一个master进程和多个worker进程(这个数量可以 ...

  5. sublime text 批量删除空白行

    CTRL+H打开replace功能,勾选上左侧的regular expression,并填写 find what栏 : \s+$  (正则表达式)replace with栏 : (这行留空) 接着点r ...

  6. python selenium简单安装及使用

    1.安装 pip install selenium 2.下载浏览器对应的Driver 链接地址:https://sites.google.com/a/chromium.org/chromedriver ...

  7. L1-023 输出GPLT (20 分)

    L1-023 输出GPLT (20 分) 给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符.当然,四种字符(不区 ...

  8. 【Java面试题】19 final,finally和finalize的区别

    总体区别 final       用于申明属性,方法和类,表示属性不可变,方法不可以被覆盖,类不可以被继承.finally     是异常处理语句结构中,表示总是执行的部分. finallize   ...

  9. SpringBoot入门-2(两种热部署方式)

    在编写代码的时候,你会发现我们只是简单把打印信息改变了,就需要重新部署,如果是这样的编码方式,那么我们估计一天下来就真的是打几个Hello World就下班了.那么如何解决热部署的问题呢?那就是spr ...

  10. MD1——2 Corner

    基本句型 被分为 5 种全然因为[动词] 造成的. 那么补语 就是因为 动词被解释成“是”的时候所需要的一种补足. [补语 Complement 传统的毒瘤说法] 不完全不及物动词 不完全及物动词~~ ...