Knight Tournament(合并区间)

Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the message to all knights in the kingdom and they in turn agreed to participate in this grand event.

As for you, you’re just a simple peasant. There’s no surprise that you slept in this morning and were late for the tournament (it was a weekend, after all). Now you are really curious about the results of the tournament. This time the tournament in Berland went as follows:

There are n knights participating in the tournament. Each knight was assigned his unique number — an integer from 1 to n.
The tournament consisted of m fights, in the i-th fight the knights that were still in the game with numbers at least li and at most ri have fought for the right to continue taking part in the tournament.
After the i-th fight among all participants of the fight only one knight won — the knight number xi, he continued participating in the tournament. Other knights left the tournament.
The winner of the last (the m-th) fight (the knight number xm) became the winner of the tournament.
You fished out all the information about the fights from your friends. Now for each knight you want to know the name of the knight he was conquered by. We think that the knight number b was conquered by the knight number a, if there was a fight with both of these knights present and the winner was the knight number a.

Write the code that calculates for each knight, the name of the knight that beat him.

Input
The first line contains two integers n, m (2 ≤ n ≤ 3·105; 1 ≤ m ≤ 3·105) — the number of knights and the number of fights. Each of the following m lines contains three integers li, ri, xi (1 ≤ li < ri ≤ n; li ≤ xi ≤ ri) — the description of the i-th fight.

It is guaranteed that the input is correct and matches the problem statement. It is guaranteed that at least two knights took part in each battle.

Output
Print n integers. If the i-th knight lost, then the i-th number should equal the number of the knight that beat the knight number i. If the i-th knight is the winner, then the i-th number must equal 0.

Examples
Input
4 3
1 2 1
1 3 3
1 4 4
Output
3 1 4 0
Input
8 4
3 5 4
3 7 6
2 8 8
1 8 1
Output
0 8 4 6 4 8 6 1
题意:有n个人进行m次比赛。每次比赛在一个区间里,有一个赢家。输的人立即被淘汰。最后求每个人被谁战胜的。
解题思路:一开始想用线段树。但Me了,应该时dfs的时候超了。最后去搜博客发现了区间并查集,先收藏了。其实只要记录他的上一个值一次就是祖先了,所以不需要更新根节点。重点是区间合并,qwe起到跳跃作用.

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define LL long long
#define MT(a,b) memset(a,b,sizeof(a))
const int maxn=5E5+5;
int ans[maxn];
int pre[maxn];
int qwe[maxn];
const int ONF=-0x3f3f3f3f;
const int INF=0x3f3f3f3f;

int search(int x){
    return pre[x]==x? x: pre[x]=search(pre[x]);
}

int main (){
    int n,m;
    int l,r,win;
    scanf("%d%d",&m,&n);
    for (int i=1;i<=m;i++){
        pre[i]=i;
        qwe[i]=i+1;
    }
    MT(ans,0);
    for (int i=1;i<=n;i++){
        scanf("%d%d%d",&l,&r,&win);
        for (int j=l;j<=r;){
            int Pre=search(j);
            if (pre[j]==j)
                ans[j]=win;
            ans[Pre]=win;
            pre[Pre]=win;
            int tmp=j;
            j=qwe[j];
            qwe[tmp]=r+1;
        }
    }
    for (int i=1;i<=m;i++){
        if (win==i) printf("0\n");
        else printf("%d ",ans[i]);
    }
    printf("\n");
    return 0;
}
发布了33 篇原创文章 · 获赞 15 · 访问量 892

猜你喜欢

转载自blog.csdn.net/weixin_43925900/article/details/103949297
今日推荐