如何用3级线段树高效解决Codeforces 356 A. 骑士锦标赛问题?

2026-04-19 23:292阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1119个文字,预计阅读时间需要5分钟。

如何用3级线段树高效解决Codeforces 356 A. 骑士锦标赛问题?

A. +Knight Tournament +time limit per test +memory limit per test +input +output +Hooray! +Berl II, the king of Berland, is organizing a knight tournament. +The king has already sent the invitation to all knights in the kingdom, and they have all agreed to participate.

A. Knight Tournament

time limit per test

memory limit per test

input

output

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:

  • nknights participating in the tournament. Each knight was assigned his unique number — an integer from 1 ton.
  • mfights, in thei-th fight the knights that were still in the game with numbers at leastliand at mostrihave fought for the right to continue taking part in the tournament.
  • i-th fight among all participants of the fight only one knight won — the knight numberxi, he continued participating in the tournament. Other knights left the tournament.
  • m-th) fight (the knight numberxm) became the winner of the tournament.

bwas conquered by the knight numbera, if there was a fight with both of these knights present and the winner was the knight numbera.

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

Input

n,m(2 ≤ n ≤ 3·105;1 ≤ m ≤ 3·105)— the number of knights and the number of fights. Each of the followingmlines contains three integersli, ri, xi(1 ≤ li < ri ≤ n;li ≤ xi ≤ ri)— the description of thei-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

nintegers. If thei-th knight lost, then thei-th number should equal the number of the knight that beat the knight numberi. If thei-th knight is the winner, then thei-th number must equal0.

Sample test(s)

input

4 31 2 11 3 31 4 4

output

3 1 4 0

input

8 43 5 43 7 62 8 81 8 1

output

0 8 4 6 4 8 6 1

Note

Consider the first test case. Knights 1 and 2 fought the first fight and knight 1 won. Knights 1 and 3 fought the second fight and knight 3 won. The last fight was between knights 3 and 4, knight 4 won.

思路:1.直接线段树覆盖,从后往前

如何用3级线段树高效解决Codeforces 356 A. 骑士锦标赛问题?

2.从前往后,用set 查找区间,然后标记,删除。

#include<cstdio>#include<cstring>#include<iostream>#include<cmath>#include<queue>#include<algorithm>#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))#define lson t<<1#define rson t<<1|1using namespace std;const int mm=6e5+9;int ans[mm];class Node{ public:int l,r,who,lazy;}rt[mm*4];int max_node;void build(int t,int l,int r){ max_node=max(max_node,t); rt[t].l=l;rt[t].r=r; rt[t].lazy=-1;rt[t].who=0; if(l==r)return; int mid=(l+r)/2; build(lson,l,mid);build(rson,mid+1,r);}class ppp{ public:int l,r,x,len; bool operator<(const ppp&t)const { if(len^t.len)return len<t.len; return l<t.l; } void out() { printf("%d %d %d %d\n",l,r,x,len); }}f[mm];void pushdown(int t){ if(rt[t].l==rt[t].r)return; if(rt[t].lazy==-1)return; rt[lson].who=rt[t].lazy;rt[rson].who=rt[t].lazy; rt[lson].lazy=rt[rson].lazy=rt[t].lazy; rt[t].lazy=-1;}void update(int t,int l,int r,int x){ pushdown(t); if(l<=rt[t].l&&rt[t].r<=r) { rt[t].lazy=x;rt[t].who=x;return; } if(l<=rt[lson].r&&r>=rt[lson].l)update(lson,l,r,x); if(r>=rt[rson].l&&l<=rt[rson].r)update(rson,l,r,x); rt[t].lazy=-1;}int query(int t,int x){ if(rt[t].lazy>=0)return rt[t].lazy; if(rt[t].l==x&&rt[t].r==x) { return rt[t].who; } if(rt[lson].r>=x)return query(lson,x); else return query(rson,x);}int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { max_node=0; build(1,1,n); FOR(i,1,m) {scanf("%d%d%d",&f[i].l,&f[i].r,&f[i].x);f[i].len=f[i].r-f[i].l; } int pos=m; for(int i=m;i>=1;--i) { if(f[i].x==f[i].l) { f[i].l++; } else if(f[i].x==f[i].r) --f[i].r; else if(f[i].x>f[i].l&&f[i].x<f[i].r) {update(1,f[i].x+1,f[i].r,f[i].x);f[i].r=f[i].x-1; } update(1,f[i].l,f[i].r,f[i].x); } clr(ans,0); FOR(i,1,n)ans[i]=query(1,i); FOR(i,1,n) printf("%d ",ans[i]); printf("\n"); } return 0;}

本文共计1119个文字,预计阅读时间需要5分钟。

如何用3级线段树高效解决Codeforces 356 A. 骑士锦标赛问题?

A. +Knight Tournament +time limit per test +memory limit per test +input +output +Hooray! +Berl II, the king of Berland, is organizing a knight tournament. +The king has already sent the invitation to all knights in the kingdom, and they have all agreed to participate.

A. Knight Tournament

time limit per test

memory limit per test

input

output

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:

  • nknights participating in the tournament. Each knight was assigned his unique number — an integer from 1 ton.
  • mfights, in thei-th fight the knights that were still in the game with numbers at leastliand at mostrihave fought for the right to continue taking part in the tournament.
  • i-th fight among all participants of the fight only one knight won — the knight numberxi, he continued participating in the tournament. Other knights left the tournament.
  • m-th) fight (the knight numberxm) became the winner of the tournament.

bwas conquered by the knight numbera, if there was a fight with both of these knights present and the winner was the knight numbera.

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

Input

n,m(2 ≤ n ≤ 3·105;1 ≤ m ≤ 3·105)— the number of knights and the number of fights. Each of the followingmlines contains three integersli, ri, xi(1 ≤ li < ri ≤ n;li ≤ xi ≤ ri)— the description of thei-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

nintegers. If thei-th knight lost, then thei-th number should equal the number of the knight that beat the knight numberi. If thei-th knight is the winner, then thei-th number must equal0.

Sample test(s)

input

4 31 2 11 3 31 4 4

output

3 1 4 0

input

8 43 5 43 7 62 8 81 8 1

output

0 8 4 6 4 8 6 1

Note

Consider the first test case. Knights 1 and 2 fought the first fight and knight 1 won. Knights 1 and 3 fought the second fight and knight 3 won. The last fight was between knights 3 and 4, knight 4 won.

思路:1.直接线段树覆盖,从后往前

如何用3级线段树高效解决Codeforces 356 A. 骑士锦标赛问题?

2.从前往后,用set 查找区间,然后标记,删除。

#include<cstdio>#include<cstring>#include<iostream>#include<cmath>#include<queue>#include<algorithm>#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))#define lson t<<1#define rson t<<1|1using namespace std;const int mm=6e5+9;int ans[mm];class Node{ public:int l,r,who,lazy;}rt[mm*4];int max_node;void build(int t,int l,int r){ max_node=max(max_node,t); rt[t].l=l;rt[t].r=r; rt[t].lazy=-1;rt[t].who=0; if(l==r)return; int mid=(l+r)/2; build(lson,l,mid);build(rson,mid+1,r);}class ppp{ public:int l,r,x,len; bool operator<(const ppp&t)const { if(len^t.len)return len<t.len; return l<t.l; } void out() { printf("%d %d %d %d\n",l,r,x,len); }}f[mm];void pushdown(int t){ if(rt[t].l==rt[t].r)return; if(rt[t].lazy==-1)return; rt[lson].who=rt[t].lazy;rt[rson].who=rt[t].lazy; rt[lson].lazy=rt[rson].lazy=rt[t].lazy; rt[t].lazy=-1;}void update(int t,int l,int r,int x){ pushdown(t); if(l<=rt[t].l&&rt[t].r<=r) { rt[t].lazy=x;rt[t].who=x;return; } if(l<=rt[lson].r&&r>=rt[lson].l)update(lson,l,r,x); if(r>=rt[rson].l&&l<=rt[rson].r)update(rson,l,r,x); rt[t].lazy=-1;}int query(int t,int x){ if(rt[t].lazy>=0)return rt[t].lazy; if(rt[t].l==x&&rt[t].r==x) { return rt[t].who; } if(rt[lson].r>=x)return query(lson,x); else return query(rson,x);}int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { max_node=0; build(1,1,n); FOR(i,1,m) {scanf("%d%d%d",&f[i].l,&f[i].r,&f[i].x);f[i].len=f[i].r-f[i].l; } int pos=m; for(int i=m;i>=1;--i) { if(f[i].x==f[i].l) { f[i].l++; } else if(f[i].x==f[i].r) --f[i].r; else if(f[i].x>f[i].l&&f[i].x<f[i].r) {update(1,f[i].x+1,f[i].r,f[i].x);f[i].r=f[i].x-1; } update(1,f[i].l,f[i].r,f[i].x); } clr(ans,0); FOR(i,1,n)ans[i]=query(1,i); FOR(i,1,n) printf("%d ",ans[i]); printf("\n"); } return 0;}