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:
- With a plan of this type you can open a portal from planet v to planet u.
- With a plan of this type you can open a portal from planet v to any planet with index in range [l, r].
- 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.
The first line of input contains three integers n, q and s (1 ≤ n, q ≤ 105, 1 ≤ 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 v, u and w where w is the cost of that plan (1 ≤ v, u ≤ n, 1 ≤ w ≤ 109). Otherwise it is followed by four integers v, l, r and w where w is the cost of that plan (1 ≤ v ≤ n, 1 ≤ l ≤ r ≤ n, 1 ≤ w ≤ 109).
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.
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
0 28 12
4 3 1 3 4 1 3 12 2 2 3 4 10 1 2 4 16
0 -1 -1 12
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
*/