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] ...
随机推荐
- 6.RDD算子实战
from pyspark import SparkContext,SparkConf import sys if __name__ == '__main__': if len(sys.argv) != ...
- hdu6089 Rikka with Terrorist
题意:n*m的平面内有K个不安全点,Q个询问位置在(x,y)的人能走到多少个点?走到:(x,y)和(x',y')之间的矩形中不包含不安全点. 标程: #include<bits/stdc++.h ...
- bzoj1568 Blue Mary
题意:P:加入一条一次函数.Q:询问x位置的最大函数值. 标程: #include<bits/stdc++.h> using namespace std; ; int q,x,n; dou ...
- code+第四次网络赛div2
T1 组合数问题: 用k个不完全相同的组合数表示一个数n. 用k-1个1和一个n-k+1表示即可. #include<cstdio> using namespace std; int x, ...
- Python代码中func(*args, **kwargs)
这是Python函数可变参数 args及kwargs *args表示任何多个无名参数,它是一个tuple **kwargs表示关键字参数,它是一个dict 测试代码如下: def foo(*args, ...
- Hibernate之Inverse的用法
在多的一端配置Inverse设置为true,来自动管理关系
- c++与js脚本交互,C++调用JS函数JS调用C++函数
一.javascript调用c++,方法有两种 方案1: 1.html编写 <html><head></head><body><h1>TES ...
- HttpClient 使用案例
package com.qifeng.config.ygx.common.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fas ...
- oracle增加用户密码,cmd导入数据库
1.tomcat中sql语句 用户名 pdmis 密码pdmis create USER pdmis IDENTIFIED BY pdmis;grant create session to pdmis ...
- Mysql优化系列之查询性能优化前篇2
接前一篇,这一篇主要总结下几个经常要用的命令 命令一:explain+sql mysql> explain select * from servers; +----+-------------+ ...