Codeforces Round #443 (Div. 1) C. Tournament

C. Tournament

http://codeforces.com/contest/878/problem/Cnode

description

Recently a tournament in k kinds of sports has begun in Berland. Vasya wants to make money on the bets.c++

The scheme of the tournament is very mysterious and not fully disclosed. Competitions are held back to back, each of them involves two sportsmen who have not left the tournament yet. Each match can be held in any of the k kinds of sport. Loser leaves the tournament. The last remaining sportsman becomes the winner. Apart of this, the scheme can be arbitrary, it is not disclosed in advance.less

Vasya knows powers of sportsmen in each kind of sport. He believes that the sportsmen with higher power always wins.this

The tournament is held every year, and each year one new participant joins it. In the first tournament, only one sportsman has participated, in the second there were two sportsmen, and so on. Vasya has been watching the tournament for the last n years. Help him to find the number of possible winners for each of the n tournaments.spa

Input

The first line contains two integers n and k (1 ≤ n ≤ 5·104, 1 ≤ k ≤ 10) — the number of tournaments and the number of kinds of sport, respectively.code

Each of the next n lines contains k integers si1, si2, ..., sik (1 ≤ sij ≤ 109), where sij is the power of the i-th sportsman in the j-th kind of sport. The sportsman with higher powers always wins. It's guaranteed that for any kind of sport all of these powers are distinct.ip

Output

For each of the n tournaments output the number of contenders who can win.ci

Examples

input

3 2
1 5
5 1
10 10rem

output

1
2
1get

input

3 2
2 2
3 3
1 10

output

1
1
3

input

3 2
2 3
1 1
3 2

output

1
1
2

Note

In the first sample:

In the first tournament there is only one sportsman, and he is the winner.

In the second tournament, there are two sportsmen, and everyone can defeat another, depending on kind of sports.

In the third tournament, the third sportsman in the strongest in both kinds of sports, so he is the winner regardless of the scheme.

題意

如今有n我的參加比賽,每一個人都有k項能力值,每一個能力值表示這我的在這個技能上面的能力。
如今這些人會兩兩進行比賽,他們會挑選一個技能進行比較,分值高的獲勝,勝利者留下來,失敗者離開。
你須要回答,最後究竟會有多少我的可能成爲冠軍。

題解

假設如今有一羣人均可能得到冠軍,那麼必須知足任何一我的,都能找到一我的的某項技能小於等於他,也能找到一我的的某項技能強於他。
那麼對於這個集合,對於每一個人都能存在一個比賽方案,使得他成爲冠軍。
而後咱們動態維護這個集羣就行了,用set去維護。

你能夠用圖論的方式去理解。若是A可以戰勝B,那麼連一條A->B的邊,顯然勝利者的含義就是若是A可以達到其餘的全部點,那麼A就是勝利者。
最後的勝利者集合,裏面的任何兩個點都能互相到達,若是已經成爲了團,咱們就進行縮點就行了,咱們用set去縮點。
而後維護每個團就行。

代碼

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int n,k;
struct node{
    int mx[10],mi[10],sz;
    bool operator < (const node &b)const{
        for(int i=0;i<k;i++){
            if(mx[i]>b.mi[i])return false;
        }
        return true;
    }
};
set<node>S;
int main(){
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++){
        node a;
        for(int j=0;j<k;j++){
            scanf("%d",&a.mx[j]);
            a.mi[j]=a.mx[j];a.sz=1;
        }
        set<node>::iterator x = S.lower_bound(a);
        set<node>::iterator y = S.upper_bound(a);
        while(x!=y){
            a.sz+=x->sz;
            for(int j=0;j<k;j++){
                a.mi[j]=min(a.mi[j],x->mi[j]);
                a.mx[j]=max(a.mx[j],x->mx[j]);
            }
            S.erase(x++);
        }
        S.insert(a);
        cout<<S.rbegin()->sz<<endl;
    }
}
相關文章
相關標籤/搜索