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 ...
随机推荐
- 在Django中使用ORM创建图书管理系统
一.ORM(对象关系映射) 很多语言的web框架中都有这个概念 1. 为什么要有ORM? 1. 写程序离不开数据,要使用数据就需要连接数据库,但是不同的数据库在sql语句上(mysql,oracle等 ...
- jatoolsprinter web打印控件直接打印不弹出
1.功能 主要是实现页面点击按钮,不弹窗,直接打印. 可以指定某个打印机打印 可以使用默认打印机打印 2.版本 主要有:免费版跟付费版 免费版官网:http://printfree.jatools.c ...
- opencv 边缘检测原理
只是实现一下,暂不考虑效率 import cv2 as cv import numpy as np import math # 从源码层面实现边缘检测 img = cv.imread('../imag ...
- Java【第二篇】基本语法之--进制、运算符
进制 对于整数,有四种表示方式: 二进制:0,1 ,满 2 进 1.以 0b 或 0B 开头. 十进制:0-9 ,满 10 进 1. 八进制:0-7 ,满 8 进1. 以数字 0 开头表示. 十六进制 ...
- shell 基础(一)
废话少说 往下看 1. 查看 Shell Shell 是一个程序,一般都是放在/bin或者/user/bin目录下,当前 Linux 系统可用的 Shell 都记录在/etc/shells文件中./e ...
- python之路day03--数据类型分析,转换,索引切片,str常用操作方法
数据类型整体分析 int :用于计算bool:True False 用户判断str:少量数据的存储 list:列表 储存大量数据 上亿数据[1,2,3,'zzy',[aa]] 元组:只读列表(1,23 ...
- 网页换肤,模块换肤,jQuery的Cookie插件使用(转)
具体效果如下: 第一次加载如下图: 然后点击天蓝色按钮换成天蓝色皮肤如下图: 然后关闭网页重新打开或者在打开另一个网页如下图: 因为皮肤用Cookie保存了下来,所以不会重置 具体的实现代码如下: & ...
- 乘积型Sobolev不等式
(Multiplicative Sobolev inequality). Let $\mu,\lambda$ and $\gamma$ be three parameters that satisfy ...
- LINQ to SQL 调用 SQL Server 的系统函数
Ø 简介 在 C# 中比较常用的 ORM(Object Relational Mapping)框架就是 EF 了,EF 经常结合 LINQ to SQL 来操作数据库.本文主要讨论如何在 LINQ ...
- 7、Servlet会话跟踪
一.会话跟踪: 不管操作多少功能,都是与当前登录用户相关的信息,当前的登录用户始终没有改变,也就是用户名和密码都没有丢失.但HTTP协议是一个无状态的协议,当一个客户向服务器发出请求(request) ...