Constrained Tree

没写出来好菜啊啊。

首先根据输入我们能算出某些节点的左儿子的范围, 右儿子的范围(此时并不准确)

然后我们在划分u这个节点的时候我们从左右开始用树状数组check每一个点是否可行, 即这个点没有被覆盖,

因为左右同时开始所以复杂度是nlognlogn,以前做过这种从两头开始check的。

还有一种方法用线段树, 从n - > 1取更新每个点的准确范围,然后直接输出答案。

还有一种方法是dfs的过程中, 先给所子树划分一个区域, 这个区域可能是不对的,然后递归左子树, 能到正确的区域才去递归右子树。

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define LD long double
  4. #define ull unsigned long long
  5. #define fi first
  6. #define se second
  7. #define mk make_pair
  8. #define PLL pair<LL, LL>
  9. #define PLI pair<LL, int>
  10. #define PII pair<int, int>
  11. #define SZ(x) ((int)x.size())
  12. #define ALL(x) (x).begin(), (x).end()
  13. #define fio ios::sync_with_stdio(false); cin.tie(0);
  14. using namespace std;
  15.  
  16. const int N = 1e6 + ;
  17. const int inf = 0x3f3f3f3f;
  18. const LL INF = 0x3f3f3f3f3f3f3f3f;
  19. const int mod = 1e9 + ;
  20. const double eps = 1e-;
  21. const double PI = acos(-);
  22.  
  23. template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
  24. template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
  25. template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
  26. template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}
  27.  
  28. struct Bit {
  29. int a[N];
  30. void modify(int x, int v) {
  31. for(int i = x; i < N; i += i & -i)
  32. a[i] += v;
  33. }
  34. int sum(int x) {
  35. int ans = ;
  36. for(int i = x; i; i -= i & -i)
  37. ans += a[i];
  38. return ans;
  39. }
  40. };
  41.  
  42. int n, m;
  43. char s[];
  44. vector<PII> vc[N];
  45. vector<int> ans;
  46. Bit bit;
  47.  
  48. void dfs(int l, int r) {
  49. if(l > r) return;
  50. if(l == r) {
  51. ans.push_back(l);
  52. return;
  53. }
  54. int L = l, R = r;
  55. for(auto& t : vc[l]) {
  56. if(t.se) chkmin(R, t.fi - );
  57. else chkmax(L, t.fi);
  58. bit.modify(l, -);
  59. bit.modify(t.fi, );
  60. }
  61. for( ; L <= R; L++, R--) {
  62. if(!bit.sum(L)) {
  63. dfs(l + , L);
  64. ans.push_back(l);
  65. dfs(L + , r);
  66. return;
  67. }
  68. if(!bit.sum(R)) {
  69. dfs(l + , R);
  70. ans.push_back(l);
  71. dfs(R + , r);
  72. return;
  73. }
  74. }
  75. puts("IMPOSSIBLE");
  76. exit();
  77. }
  78.  
  79. int main() {
  80. scanf("%d%d", &n, &m);
  81. for(int i = ; i <= m; i++) {
  82. int a, b;
  83. scanf("%d%d%s", &a, &b, s);
  84. if(b <= a) {
  85. puts("IMPOSSIBLE");
  86. return ;
  87. }
  88. if(s[] == 'L') vc[a].push_back(mk(b, ));
  89. else vc[a].push_back(mk(b, ));
  90. bit.modify(a, );
  91. bit.modify(b, -);
  92. }
  93. dfs(, n);
  94. for(auto& t : ans) printf("%d ", t);
  95. puts("");
  96. return ;
  97. }
  98.  
  99. /*
  100. */

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. 在Django中使用ORM创建图书管理系统

    一.ORM(对象关系映射) 很多语言的web框架中都有这个概念 1. 为什么要有ORM? 1. 写程序离不开数据,要使用数据就需要连接数据库,但是不同的数据库在sql语句上(mysql,oracle等 ...

  2. jatoolsprinter web打印控件直接打印不弹出

    1.功能 主要是实现页面点击按钮,不弹窗,直接打印. 可以指定某个打印机打印 可以使用默认打印机打印 2.版本 主要有:免费版跟付费版 免费版官网:http://printfree.jatools.c ...

  3. opencv 边缘检测原理

    只是实现一下,暂不考虑效率 import cv2 as cv import numpy as np import math # 从源码层面实现边缘检测 img = cv.imread('../imag ...

  4. Java【第二篇】基本语法之--进制、运算符

    进制 对于整数,有四种表示方式: 二进制:0,1 ,满 2 进 1.以 0b 或 0B 开头. 十进制:0-9 ,满 10 进 1. 八进制:0-7 ,满 8 进1. 以数字 0 开头表示. 十六进制 ...

  5. shell 基础(一)

    废话少说 往下看 1. 查看 Shell Shell 是一个程序,一般都是放在/bin或者/user/bin目录下,当前 Linux 系统可用的 Shell 都记录在/etc/shells文件中./e ...

  6. python之路day03--数据类型分析,转换,索引切片,str常用操作方法

    数据类型整体分析 int :用于计算bool:True False 用户判断str:少量数据的存储 list:列表 储存大量数据 上亿数据[1,2,3,'zzy',[aa]] 元组:只读列表(1,23 ...

  7. 网页换肤,模块换肤,jQuery的Cookie插件使用(转)

    具体效果如下: 第一次加载如下图: 然后点击天蓝色按钮换成天蓝色皮肤如下图: 然后关闭网页重新打开或者在打开另一个网页如下图: 因为皮肤用Cookie保存了下来,所以不会重置 具体的实现代码如下: & ...

  8. 乘积型Sobolev不等式

    (Multiplicative Sobolev inequality). Let $\mu,\lambda$ and $\gamma$ be three parameters that satisfy ...

  9. LINQ to SQL 调用 SQL Server 的系统函数

    Ø  简介 在 C# 中比较常用的 ORM(Object Relational Mapping)框架就是 EF 了,EF 经常结合 LINQ to SQL 来操作数据库.本文主要讨论如何在 LINQ ...

  10. 7、Servlet会话跟踪

    一.会话跟踪: 不管操作多少功能,都是与当前登录用户相关的信息,当前的登录用户始终没有改变,也就是用户名和密码都没有丢失.但HTTP协议是一个无状态的协议,当一个客户向服务器发出请求(request) ...