BZOJ4383/LuoGuP3588 Pustynia/PUS 线段树建图优化
我会告诉你我看了很久很久才把题目看懂吗???怀疑智商了
原来他给的l,r还有k个数字都是下标...
比如给了一个样例 l, r, k, x1,x2,x3...xk,代表的是一个数组num[l]~num[r],其中有k个数num[x1],num[x2]....num[xk]这k个数都比l~r区间剩下的(下标不是x1...xk)的任何一个数大。题目就是给m个这种信息然后构造一个符合条件的数列
知道了这一点可以发现每一个信息都是一组偏序关系,即num[x1] > l~r区间剩下的数 .....num[xk] > l~r区间剩下的数.当然如果数量级小的话直接就每一个关系建一条边直接拓扑排序就可以了.但是这道题数量实在太大,然后这里就有一种黑科技---线段树建图优化.因为我们可以保证每次建边的时候,num[xi]都是和一个区间相连的(单个点也算一个区间),这样我们可考虑区间这个整体,当然具体代码怎么写没这么简单.
又学到一种黑科技....
#include <iostream>
#include <string.h>
#include <cstdio>
#include <vector>
#include <queue>
#include <math.h>
#include <string>
#include <algorithm>
#include <time.h> #define SIGMA_SIZE 26
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) (x&-x)
#define foe(i, a, b) for(int i=a; i<=b; i++)
#define fo(i, a, b) for(int i=a; i<b; i++)
#pragma warning ( disable : 4996 ) using namespace std;
typedef long long LL;
inline LL LMax(LL a, LL b) { return a>b ? a : b; }
inline LL LMin(LL a, LL b) { return a>b ? b : a; }
inline LL lgcd(LL a, LL b) { return b == ? a : lgcd(b, a%b); }
inline LL llcm(LL a, LL b) { return a / lgcd(a, b)*b; } //a*b = gcd*lcm
inline int Max(int a, int b) { return a>b ? a : b; }
inline int Min(int a, int b) { return a>b ? b : a; }
inline int gcd(int a, int b) { return b == ? a : gcd(b, a%b); }
inline int lcm(int a, int b) { return a / gcd(a, b)*b; } //a*b = gcd*lcm
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const int maxk = 3e6+;
const int maxn = 1e6+; int cnt, tot;
int indexx[maxn], val1[maxn], val2[maxn], x[maxn], in[maxn];
int linjie[maxn];
int n, s, m;
queue<int> q;
struct node {
int to, nnext, len;
}pp[maxk]; void addedge( int u, int v, int l )
{ pp[cnt].to = v; pp[cnt].len = l; pp[cnt].nnext = linjie[u]; linjie[u] = cnt++; in[v]++; } void build(int rt, int L, int R)
{
if ( L == R )
{ indexx[L] = rt; return; } int mid = (L+R)>>;
build(lson, L, mid);
build(rson, mid+, R);
addedge(lson, rt, ); addedge(rson, rt, );
} void update(int rt, int L, int R, int lhs, int rhs, int C)
{
if ( lhs <= L && rhs >= R )
{
addedge(rt, C, );
return;
} int mid = (L+R)>>;
if ( lhs <= mid ) update(lson, L, mid, lhs, rhs, C);
if ( rhs > mid ) update(rson, mid+, R, lhs, rhs, C);
} void init()
{
cnt = ;
memset(linjie, -, sizeof(linjie));
build(, , n); tot = n << ; int a, b;
while (s--)
{
scanf("%d %d", &a, &b);
val1[indexx[a]] = val2[indexx[a]] = b;
}
} int main()
{
cin >> n >> s >> m;
init(); int l, r, k;
while (m--)
{
scanf("%d %d %d", &l, &r, &k);
x[] = l-; x[k+] = r+; tot++; foe(i, , k) {
scanf("%d", &x[i]);
addedge(tot, indexx[x[i]], );
} foe(i, , k) {
if (x[i+] - x[i] > )
update(, , n, x[i]+, x[i+]-, tot);
}
} foe(i, , tot)
if (!in[i]) {
val1[i] = Max(val1[i], );
q.push(i);
} int a, b;
while (!q.empty())
{
a = q.front(); q.pop();
for (int i = linjie[a]; ~i; i = pp[i].nnext) {
val1[pp[i].to] = Max(val1[pp[i].to], val1[a]+pp[i].len);
in[pp[i].to] --; if (val2[pp[i].to] && val1[pp[i].to] > val2[pp[i].to]) {
printf("NIE\n");
return ;
}
if ( !in[pp[i].to] )q.push(pp[i].to);
}
} foe(i, , n)
if ( !val1[indexx[i]] || val1[indexx[i]] > )
{ printf("NIE\n"); return ; } printf("TAK\n");
fo(i, , n)
printf("%d ", val1[indexx[i]]);
printf("%d\n", val1[indexx[n]]);
return ;
}
BZOJ4383/LuoGuP3588 Pustynia/PUS 线段树建图优化的更多相关文章
- 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- HDU5669 Road 分层最短路+线段树建图
分析:(官方题解) 首先考虑暴力,显然可以直接每次O(n^2) 的连边,最后跑一次分层图最短路就行了. 然后我们考虑优化一下这个连边的过程 ,因为都是区间上的操作,所以能够很明显的想到利用线段树来维 ...
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Codeforces Round #406 (Div. 2) D. Legacy (线段树建图dij)
D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- POJ 2374 线段树建图+Dijkstra
题意: 思路: 线段树+Dijkstra(要堆优化的) 线段树要支持打标记 一个栅栏 拆成两个点 :左和右 新加一个栅栏的时候 看看左端点有没有被覆盖过 如果有的话 就分别从覆盖的那条线段的左右向当前 ...
- 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)
D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- 『The Captain 最短路建图优化』
The Captain(BZOJ 4152) Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小 ...
- 【BZOJ3672】【NOI2014】购票(线段树,斜率优化,动态规划)
[BZOJ3672][NOI2014]购票(线段树,斜率优化,动态规划) 题解 首先考虑\(dp\)的方程,设\(f[i]\)表示\(i\)的最优值 很明显的转移\(f[i]=min(f[j]+(de ...
- [POI2015]PUS [线段树优化建图]
problem 线段树优化建图,拓扑,没了. #include <bits/stdc++.h> #define ls(x) ch[x][0] #define rs(x) ch[x][1] ...
随机推荐
- Linux 添加时间
添加在指令后面 `date +%Y%m%d%H%M`注意date和+之间一定要有空格 ps: %% 一个文字的 % %a 当前locale 的星期名缩写(例如: 日,代表星期日) %A ...
- MySQL回滚到某一时刻数据的方法
MySQL回滚到某一时刻数据的方法 对于有归档日志的数据库来说,原理上都具备全库回滚到之前某一时刻的能力.在这方面最好用的Orale数据库,使用Oracle数据库的RMAN工具,可以方便的 ...
- 【JZOJ6375】华灵[蝶妄想]
description analysis 明显括号序长度是偶数,如果其中一个是奇数,那么只能让这奇数行或列是括号序 对于两个都是偶数,需要分类讨论,假设\(n<m\) 有一种是牺牲掉\(n\ov ...
- SpringCloudBus
不重启微服务的情况下更新配置 配置服务端 tensquare_config配置依赖 <dependency> <groupId>org.springframework.clou ...
- Windows 设置内网和外网同时使用
想要电脑同时使用内网和外网必须具备两个网卡,一个是无线网卡一个是本地连接,无线网卡用来连接wifi也就是外网,而本地连接需要网线连接内网,外网是不需要做设置的,我们只需要设置内网即可,鼠标右击电脑右下 ...
- 携程的 Dubbo 之路
本篇文章整理自董艺荃在 Dubbo 社区开发者日上海站的演讲. 缘起 携程当初为什么要引入 Dubbo 呢?实际上从 2013 年底起,携程内主要使用的就是基于 HTTP 协议的 SOA 微服务框架. ...
- thinkphp 获取模板地址
为了更方便的输出模板文件,新版封装了一个T函数用于生成模板文件名. 用法: 大理石平台检验标准 T([资源://][模块@][主题/][控制器/]操作,[视图分层]) T函数的返回值是一个完整的模板文 ...
- C++仿函数和回调函数的异同
C++回调函数(callback)与仿函数(functor)的异同 c++仿函数 functor C++仿函数和回调函数的异同
- MFC基础类及其层次结构
从类CCmdTarget派生出绝大多数MFC中的类,其层次结构如下图: 从根类Cobject层层派生出绝大多数MFC中的类,层次结构如下图: MFC中重点类: CObject类是MFC的绝大部分类的基 ...
- hibernate_05_hibernateHQL查询QBC查询和SQL查询
1.HQL简介:HQL是Hibernate Query Language(Hibernate 查询语言)的缩写,提供更加丰富灵活.更为强大的查询能力:HQL更接近SQL语句查询语法.Hibernate ...