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. json内存级非关系数据库

    介绍 `jsonDB2`是一个基于内存的键值对数据库(非关系型数据库) 开发初衷:实现个人tornado项目中内存session存储功能(不想引入redis等非关系型数据库) 项目地址: https: ...

  2. Could not find package vendor/name in a version matching v-Number 是坑!

    当我遇到这个问题的时候曾去发布了issue -https://github.com/composer/packagist/issues/934 主要的问题是,composer require vend ...

  3. [问题]Android listView item edittext 不能调用软键盘输入法

    android listview item edittext not  softkeyboard edittext可以获取焦点, 可以触发事件, 但是就是不能调用输入法, 不知道为什么? 难道不能在i ...

  4. <数据结构基础学习>(四)链表 Part 2

    一.使用链表实现栈 增,删,查只对链表头进行操作,时间复杂度都为O(1) 链表头作为栈顶 LinkedListStack<E> implements Stack<E> publ ...

  5. <数据结构基础学习>(三)Part 2 队列

    一.队列 Queue 队列也是一种线性结构 相比数组,队列对应的操作是数组的子集 只能从一端(队尾)添加元素,只能从另一端(队首)取出元素. (排队) 队列是一种先进先出的数据结构(先到先得)FIFO ...

  6. 测试框架httpclent 4.HttpClient Post方法实现

    startupWithCookies.json [ { "description":"这是一个会返回cookies信息的get请求", "reques ...

  7. pta编译总结1

    打印沙漏 (20 分) 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号: ...

  8. CMDB资产管理系统开发【day25】:表结构设计1

    资产表 # _*_coding:utf-8_*_ __author__ = 'jieli' from assets.myauth import UserProfile from django.db i ...

  9. 使用echarts-for-react 绘制折线图 报错:`series.type should be specified `

    解决办法: 在动态获取值的函数前面加 访问器属性  get ,去获取对象的属性 @inject('commonStore', 'reportUIStore') @observer class Line ...

  10. C# - 操作符

    操作符(Operator) C#的操作符是一种告诉编译器执行计算.逻辑判断的符号. default(x) 获取类型的默认值,x是类型.虽然可以为任意类型使用此操作符,但此操作符主要用于泛型,在不确定泛 ...