Summer Training day6 codeforces787D 最短路spfa,线段树建图

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.

题解:这道题目如果老老实实的去建图的话,边会多到爆炸。所以我们需要另外想一个建图方法。

由于边的起点或者终点涉及到了区间的情况,我们可以使用线段树建图的套路。

对于整段区间,我们用把它划分成一颗线段树。

对于整个图,我们可以构造两颗线段树A和B。

在A线段树上,我们从底部往上连权值为0的边。对于B线段树来说我们从顶部往下连边权值为0的边。

然后我们把B线段树底部的所有点向A线段树底部的点连权值为0得边。

对于type=1的边,我们从A线段树底部的点向B线段树底部的点做权值为w的边。

对于type=2的边,我们从A线段树底部的点向B线段树对应的区间段连权值为w的一些边。

对于type=3的边,我们从A线段树对应的区间段向B线段树底部对应的点连权值为w的一些边。

然后跑最短路就行了。

花个丑图。。。。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN= 1e5+7;
char str[MAXN];
struct segtree{
	char c;
	int val[MAXN << 2];
	int mark[MAXN << 2];
	void pushup(int rt){
		val[rt] = val[rt*2] + val[rt*2+1];
	}
	void pushdown(int rt,int begin,int end){
		if(mark[rt] == 1){
			int mid = (begin + end) / 2;
			val[rt*2] = (mid - begin + 1);
			val[rt*2+1] = (end - mid);
			mark[rt*2] = mark[rt*2+1] = mark[rt];
		}
		else if (mark[rt] == 2){
			int mid = (begin + end) / 2;
			val[rt*2] = 0;
			val[rt*2+1] = 0;
			mark[rt*2] = mark[rt*2+1] = mark[rt];
		}
		mark[rt] = 0;
	}
	void build(int rt,int begin,int end){
		mark[rt] = 0;
		if(begin == end){
			val[rt] = str[begin-1] == c;
			return ;
		}
		int mid = (begin + end) / 2;
		build(rt*2,begin,mid);
		build(rt*2+1,mid+1,end);
		pushup(rt);
	}
	void update(int rt,int begin,int end,int ubegin,int uend,int key){
		//key == 1 set
		//key == 2 reset
		if(uend < ubegin) return ;
		if(begin > uend || ubegin > end){
			return ;
		}
		if(begin >= ubegin && end <= uend){
			mark[rt] = key;
			if(key == 1){
				val[rt] = (end - begin + 1) * 1;
			}
			else if(key == 2){
				val[rt] = 0;
			}
			return ;
		}
		pushdown(rt,begin,end);
		int mid = (begin + end) / 2;
		update(rt*2,begin,mid,ubegin,uend,key);
		update(rt*2+1,mid+1,end,ubegin,uend,key);
		pushup(rt);
	}
	int query(int rt,int begin,int end,int ubegin,int uend){
		if(begin > uend || ubegin > end){
			return 0;
		}
		if(begin >= ubegin && end <= uend){
			return val[rt];
		}
		pushdown(rt,begin,end);
		int mid = (begin + end) / 2;
		int a = query(rt*2,begin,mid,ubegin,uend);
		int b = query(rt*2+1,mid+1,end,ubegin,uend);
		return a + b;
	}
}segs[30];
int n, m;
int main(){
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	scanf("%d%d",&n,&m);
	scanf(" %s",str);
	for(int i = 0;i < 26;i++){
		segs[i].c = 'a' + i;
		segs[i].build(1,1,n);
	}
	for(int i = 0;i < m;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		int cnt = 0;
		vector<pair<int,int> > vec;
		for(int t = 0;t < 26;t++){
			int nums = segs[t].query(1,1,n,a,b);
			if(nums % 2) cnt++;
			if(nums) vec.push_back(make_pair(t,nums));
		}
		if(((a-b + 1) % 2 == 0 && cnt > 0) || ((a-b + 1) % 2 != 0 && cnt != 1)) continue;
		int l = 0;
		for(int t = 0;t < vec.size();t++){
			pair<int,int> p = vec[t];
			int id = p.first;
			int nums = p.second;
			segs[id].update(1,1,n,a,b,2);
			if(nums % 2){
				segs[id].update(1,1,n,(a+b)/2,(a+b)/2,1);
			}
			segs[id].update(1,1,n,a+l,a+l+nums/2-1,1);
			segs[id].update(1,1,n,b-l-nums/2+1,b-l,1);
			l += nums/2;
		}
	}
	for(int i = 1;i <= n;i++){
		for(int t = 0;t < 26;t++){
			if(segs[t].query(1,1,n,i,i)){
				putchar(t+ 'a');
				break;
			}
		}
	}
	return 0;
}
/*
7 1
aabcbaa
1 3

7 1
aabcbaa
5 7
*/





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值