题目链接http://codeforces.com/problemset/problem/357/C点击打开链接
或者用并查集 注意用一个next数组储存他的下一个集合 不能直接用pre作为打败他的人 因为在过程中可能pre会因缩短路径变化 因此要多开一个
并查集代码
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <limits>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
int pre[333333];
int nnext[333333];
int ppre[333333];
int findx(int x)
{
int r=x;
while(ppre[r]!=r)
{
r=ppre[r];
}
int i=x;int j;
while(ppre[i]!=r)
{
j=ppre[i];
ppre[i]=r;
i=j;
}
return r;
}
void join (int x,int y,int win)
{
int nnnext=nnext[y];
int temp=x;
while(temp<nnnext)
{
int mid=nnext[temp];
nnext[temp]=nnnext;
int midmid=findx(temp);
if(pre[midmid]==midmid)
pre[midmid]=win;
if(ppre[temp]!=win)
ppre[midmid]=win;
temp=mid;
}
int p1=findx(x);
int p2=findx(y);
if(p1!=p2)
{
ppre[p2]=p1;
}
}
int main()
{
int n;int m;
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++)
ppre[i]=i,pre[i]=i,nnext[i]=i+1;
for(int i=0;i<m;i++)
{
int l,r,win;
scanf("%d%d%d",&l,&r,&win);
//if(l>r)
//swap(l,r);
join(l,r,win);
}
for(int i=1;i<=n;i++)
if(pre[i]!=i)
printf("%d ",pre[i]);
else
printf("0 ");
}
乱搞代码
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <limits>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
set<int> s;
set <int > ::iterator it,re[333333];
int a[333333];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
s.insert(i);
for(int i=1;i<=m;i++)
{
int l,r,win;
int step=0;
scanf("%d%d%d",&l,&r,&win);
for(it=s.lower_bound(l);(*it)<=r&&it!=s.end();it++)
{
if((*it)!=win)
{
re[step++]=it;
a[*it]=win;
}
}
for(int ii=0;ii<step;ii++)
s.erase(re[ii]);
}
for(int i=1;i<=n;i++)
{
printf("%d ",a[i]);
}
}