CF 787D Legacy(线段树思想构图+最短路)

D. Legacy
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them. So Rick wants to give his legacy to Morty before bad guys catch them.

There are n planets in their universe numbered from 1 to n. Rick is in planet number s (the earth) and he doesn't know where Morty is. As we all know, Rick owns a portal gun. With this gun he can open one-way portal from a planet he is in to any other planet (including that planet). But there are limits on this gun because he's still using its free trial.

By default he can not open any portal by this gun. There are q plans in the website that sells these guns. Every time you purchase a plan you can only use it once but you can purchase it again if you want to use it more.

Plans on the website have three types:

  1. With a plan of this type you can open a portal from planet v to planet u.
  2. With a plan of this type you can open a portal from planet v to any planet with index in range [l, r].
  3. With a plan of this type you can open a portal from any planet with index in range [l, r] to planet v.

Rick doesn't known where Morty is, but Unity is going to inform him and he wants to be prepared for when he finds and start his journey immediately. So for each planet (including earth itself) he wants to know the minimum amount of money he needs to get from earth to that planet.

Input

The first line of input contains three integers nq and s (1 ≤ n, q ≤ 1051 ≤ s ≤ n) — number of planets, number of plans and index of earth respectively.

The next q lines contain the plans. Each line starts with a number t, type of that plan (1 ≤ t ≤ 3). If t = 1 then it is followed by three integers vu and w where w is the cost of that plan (1 ≤ v, u ≤ n1 ≤ w ≤ 109). Otherwise it is followed by four integers vlr and w where w is the cost of that plan (1 ≤ v ≤ n1 ≤ l ≤ r ≤ n1 ≤ w ≤ 109).

Output

In the first and only line of output print n integers separated by spaces. i-th of them should be minimum money to get from earth to i-th planet, or  - 1 if it's impossible to get to that planet.

Examples
input
3 5 1
2 3 2 3 17
2 3 2 2 16
2 2 2 3 3
3 3 1 1 12
1 3 3 17
output
0 28 12 
input
4 3 1
3 4 1 3 12
2 2 3 4 10
1 2 4 16
output
0 -1 -1 12 
Note

In the first sample testcase, Rick can purchase 4th plan once and then 2nd plan in order to get to get to planet number 2.

 

 

题目链接:CF 787D

很脑洞的一道题,比较好的做法是用线段树分割区间的思想,把1-n看成区间构建两颗线段树A和B,然后把所有的子区间都看成一个点,这样后面的加边就是对这些区间加边了,构建好A和B,A父子节点之间自底向上加有向边,B树自顶向下加有向边,然后把B树的子节点加无向边到对应的A树的子节点,代表这些点可以推出(到达)哪些点,由于一开始在子节点s(A[s]还是B[s]无所谓,两者本来就是强连通的),哪里都不能去,因此最后要检查的就是B树的子节点的dis值(在A数的话哪里都可以)。

然后就是三种区间操作

1:u->v,加边$<A的叶子节点u,B的叶子节点v,w>$

2:u->[l,r],加边$<A的叶子节点u,\{B树中[l,r]区间对应的节点集合\},w>$

3:[l,r]->v,加边$<\{A树中[l,r]区间对应的节点集合\},B树的叶子节点v,w>$

这样一来三种边都不会多也不会少刚好可以到达正确的点。然后从B[s]的跑最短路即可,最后检查B[i]的dis即可,当然一开始要把A、B树的叶子节点记录下来方便后面加边使用

边数最坏情况应该是每一层左右均取两个区间,一直递归下去,有$log_{2}N$层,因此边数大约是$O(4*q*log_{2}N+2N)$

代码:

#include <stdio.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1e5 + 7;
struct edge
{
    int to, nxt;
    LL w;
    edge() {}
    edge(int _to, int _nxt, LL _w): to(_to), nxt(_nxt), w(_w) {}
} E[N * 68];
int head[N << 2], tot;
LL d[N << 2];
bitset < N << 2 > vis;
int id[2][N << 2], sz;
vector<int>st;
int A[N], B[N];

void init()
{
    CLR(head, -1);
    tot = 0;
    sz = 0;
}
inline void add(int s, int t, LL w)
{
    E[tot] = edge(t, head[s], w);
    head[s] = tot++;
}
void build(int k, int l, int r, int o)
{
    id[o][k] = ++sz;
    if (l == r)
    {
        if (o)
            A[l] = id[o][k];
        else
            B[l] = id[o][k];
        return ;
    }
    else
    {
        int mid = MID(l, r);
        build(LC(k), l, mid, o);
        build(RC(k), mid + 1, r, o);
        if (o) //A树自底向上
        {
            add(id[o][LC(k)], id[o][k], 0);
            add(id[o][RC(k)], id[o][k], 0);
        }
        else//B树自顶向下
        {
            add(id[o][k], id[o][LC(k)], 0);
            add(id[o][k], id[o][RC(k)], 0);
        }
    }
}
void Findset(int k, int l, int r, int ql, int qr, int o)
{
    if (ql <= l && r <= qr)
        st.push_back(id[o][k]);
    else
    {
        int mid = MID(l, r);
        if (qr <= mid)
            Findset(LC(k), l, mid, ql, qr, o);
        else if (ql > mid)
            Findset(RC(k), mid + 1, r, ql, qr, o);
        else
            Findset(LC(k), l, mid, ql, mid, o), Findset(RC(k), mid + 1, r, mid + 1, qr, o);
    }
}
void SPFA(int s)
{
    queue<int>Q;
    Q.push(s);
    vis.reset();
    CLR(d, INF);
    d[s] = 0;
    while (!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        vis[u] = 0;
        for (int i = head[u]; ~i; i = E[i].nxt)
        {
            int v = E[i].to;
            if (d[v] > d[u] + E[i].w)
            {
                d[v] = d[u] + E[i].w;
                if (!vis[v])
                {
                    vis[v] = 1;
                    Q.push(v);
                }
            }
        }
    }
}
int main(void)
{
    int n, q, s, i, ops;
    while (~scanf("%d%d%d", &n, &q, &s))
    {
        init();
        build(1, 1, n, 1);
        build(1, 1, n, 0);
        for (i = 1; i <= n; ++i)
        {
            add(A[i], B[i], 0);
            add(B[i], A[i], 0);
        }
        for (i = 0; i < q; ++i)
        {
            scanf("%d", &ops);
            if (ops == 1)
            {
                int u, v;
                LL w;
                scanf("%d%d%I64d", &u, &v, &w);
                add(A[u], B[v], w);
            }
            else if (ops == 2)
            {
                int u, l, r;
                LL w;
                scanf("%d%d%d%I64d", &u, &l, &r, &w);
                st.clear();
                Findset(1, 1, n, l, r, 0);
                for (auto &x : st)
                    add(A[u], x, w);
            }
            else if (ops == 3)
            {
                int v, l, r;
                LL w;
                scanf("%d%d%d%I64d", &v, &l, &r, &w);
                st.clear();
                Findset(1, 1, n, l, r, 1);
                for (auto &x : st)
                    add(x, B[v], w);
            }
        }
        SPFA(B[s]);
        for (i = 1; i <= n; ++i)
            printf("%I64d%c", d[B[i]] == 0x3f3f3f3f3f3f3f3f ? -1 : d[B[i]], " \n"[i == n]);
    }
    return 0;
}

转载于:https://www.cnblogs.com/Blackops/p/7478172.html

Linux的启动过程可以分为以下几个步骤(以Legacy+MBR为例): 1. BIOS启动:计算机上电后,首先由基本输入输出系统(BIOS)进行初始化和自检。BIOS会检测并识别硬件设备,并加载位于主引导记录(MBR)中的引导程序。 2. 主引导记录(MBR)加载:MBR是硬盘上的一个特殊扇区,位于第一个物理扇区(扇区编号为0)。MBR中包含了引导程序的代码和分区表。BIOS将MBR加载到内存中,并将控制权交给引导程序。 3. 引导程序加载:MBR中的引导程序通常是GRUB(GRand Unified Bootloader)。它的主要作用是加载操作系统的内核。 4. 内核加载:引导程序会查找并加载操作系统内核。内核是操作系统的核心部分,负责管理硬件、内存和其他系统资源。一旦内核加载完毕,它将成为系统的第一个进程。 5. 初始化过程:内核会进行初始化,包括初始化设备驱动程序、加载必要的模块和设置系统参数等。内核还会启动用户空间进程init(通常是systemd或SysV init),init进程是用户空间的第一个进程。 6. 用户空间初始化:init进程会继续初始化用户空间环境,启动各种系统服务和守护进程。这些服务和进程提供了各种功能,例如网络服务、文件系统服务等。 7. 登录界面:一旦用户空间初始化完成,系统会显示登录界面(如图形界面的登录管理器或命令行登录提示符)。用户可以输入凭据登录操作系统。 8. 用户登录:用户成功登录后,系统会加载用户配置文件,并启动用户的图形界面或命令行终端。此时,用户可以开始使用操作系统进行各种任务。 这些步骤仅是Linux启动过程的概览,实际过程可能会因不同的发行版、引导程序和配置而有所差异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值