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] ...
随机推荐
- [笔记]Laravel TDD 胡乱记录
TDD: 测试驱动开发(Test-Driven Development),TDD的原理是在开发功能代码之前,先编写单元测试用例代码,测试代码确定需要编写什么产品代码. -- 载自TDD百度百科 参考 ...
- leetcode-63-不同路径②
题目描述: 第一次提交: class Solution: def uniquePathsWithObstacles(self, obstacleGrid) : m = len(obstacleGrid ...
- 阿里云POLARDB如何帮助猿辅导打造“孩子喜欢老师好”的网课平台?
海量的题库.音视频答题资料.用户数据以及日志,对猿辅导后台数据存储和处理能力都提出了严峻的要求.而由于教育辅导行业的业务特点,猿辅导也面临着业务峰值对于数据库能力的巨大挑战.本文就为大家介绍阿里云PO ...
- 线性dp——cf1067A
考虑三种情况,刷表dp+前缀和预处理即可 #include<bits/stdc++.h> using namespace std; ; ],f[][][],ans,s; int main( ...
- mysql的卸载重装+导入大量数据失败的解决方案+工具执行和项目执行结果不同
1.卸载 1>快捷键win+r输入regedit进入注册表 找到3个文件夹,全部删除 . HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Eve ...
- ECMAScript 6中的Set和Map数据结构
一.Set 基本用法: Set本身是一个构造函数,用来生成Set数据结构.Set函数可以接受一个数组作为参数用来初始化. const arr = new Set([2,2,3,3,4,4,5,8]); ...
- JavaScript中的浏览器对象模型
浏览器对象模型 1.浏览器引入JavaScript 1.直接在HTML文件中引入 首先第1种方式就是直接在HTML文档里面引入JavaScript代码.在维护一些老项目的时候,经常 可以看到J ava ...
- webstorm 初次上传代码到 远程gitlab中
1. 在 公司搭建的gitlab网站,创建project,然后生成了 git的地址. 2.在 本地电脑上,打开 webstorm,要将已有的代码上传到 git网站,那么需要在webstrom编辑器的t ...
- 解决方案 -SQL脚本建表产生ORA-00942错误
一.问题简介 1.开发环境 操作系统:win10 数 据 库:Oracle11g 数据库连接工具:Navicat Premium 2.问题简述 在使用SQL Development.Navicat ...
- 委托_deleget
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...