답안 #423347

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
423347 2021-06-11T02:34:25 Z ansol4328 조화행렬 (KOI18_matrix) C++14
100 / 100
2140 ms 100616 KB
#include<bits/stdc++.h>
#define pb push_back

using namespace std;

struct fenwick{
    vector<int> maxv; int sz;
    fenwick(int a){
        maxv.resize(a+5); sz=a;
    }
    void updt(int i, int v){
        for( ; i<=sz ; i+=i&-i) maxv[i]=max(maxv[i],v);
    }
    int qry(int i){
        int ret=0;
        for( ; i ; i-=i&-i) ret=max(maxv[i],ret);
        return ret;
    }
};

struct MGT{
    vector<vector<int>> tree;
    int base;
    MGT(int a, int arr[]){
        base=1;
        while(base<a) base<<=1;
        tree.resize(base*2+5);
        base--;
        for(int i=1 ; i<=a ; i++) tree[base+i].pb(arr[i]);
        setup();
    }
    void mg(vector<int> &cur, int i){
        vector<int> &L=tree[i*2];
        vector<int> &R=tree[i*2+1];
        int i1=0, i2=0, sz1=L.size(), sz2=R.size();
        while(i1<sz1 && i2<sz2){
            if(L[i1]<R[i2]) cur.pb(L[i1++]);
            else if(L[i1]>R[i2]) cur.pb(R[i2++]);
            else cur.pb(L[i1++]), cur.pb(R[i2++]);
        }
        while(i1<sz1) cur.pb(L[i1++]);
        while(i2<sz2) cur.pb(R[i2++]);
    }
    void setup(){
        for(int i=base ; i>=1 ; i--) mg(tree[i],i);
    }
    int get_idx(int i, int v){
        return lower_bound(tree[i].begin(),tree[i].end(),v)-tree[i].begin();
    }
};

struct A{
    int v[3];
    bool operator<(const A &rhs){
        return v[0]<rhs.v[0];
    }
};

int N;
A m[200005];
fenwick *seg[524300];
int bs, tmp[200005];

void cont(int i)
{
    vector<int> x;
    for(int j=1 ; j<=N ; j++) x.pb(m[j].v[i]);
    sort(x.begin(),x.end());
    for(int j=1 ; j<=N ; j++) m[j].v[i]=lower_bound(x.begin(),x.end(),m[j].v[i])-x.begin()+1;
}

int get_val(int fn, int v, MGT &mgt)
{
    int st=bs+1, ret=0; fn+=bs;
    while(st<fn){
        if(st&1){
            ret=max(ret,seg[st]->qry(mgt.get_idx(st,v))+1);
            st++;
        }
        if(~fn&1){
            ret=max(ret,seg[fn]->qry(mgt.get_idx(fn,v))+1);
            fn--;
        }
        st>>=1, fn>>=1;
    }
    if(st==fn) ret=max(ret,seg[st]->qry(mgt.get_idx(st,v))+1);
    return ret;
}

void update(int i, int f, int v, MGT &mgt)
{
    for(i+=bs ; i>=1 ; i>>=1){
        seg[i]->updt(mgt.get_idx(i,f)+1,v);
    }
}

int main()
{
    int r;
    scanf("%d %d",&r,&N);
    for(int i=0 ; i<r ; i++){
        for(int j=1 ; j<=N ; j++) scanf("%d",&m[j].v[i]);
        cont(i);
    }
    if(r==2) for(int i=1 ; i<=N ; i++) m[i].v[r]=m[i].v[r-1];
    
    sort(m+1,m+1+N);
    for(int i=1 ; i<=N ; i++) tmp[m[i].v[1]]=m[i].v[2];
    MGT mgt(N,tmp); bs=mgt.base;
    for(int i=1 ; i<=N+bs ; i++) seg[i]=new fenwick(mgt.tree[i].size());
    
    int ans=0;
    for(int i=1 ; i<=N ; i++){
        int val=get_val(m[i].v[1],m[i]
        .v[2],mgt);
        ans=max(ans,val);
        update(m[i].v[1],m[i].v[2],val,mgt);
    }
    printf("%d",ans);
    return 0;
}

Compilation message

matrix.cpp: In function 'int main()':
matrix.cpp:100:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |     scanf("%d %d",&r,&N);
      |     ~~~~~^~~~~~~~~~~~~~~
matrix.cpp:102:40: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |         for(int j=1 ; j<=N ; j++) scanf("%d",&m[j].v[i]);
      |                                   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 5324 KB Output is correct
2 Correct 26 ms 5324 KB Output is correct
3 Correct 31 ms 5344 KB Output is correct
4 Correct 24 ms 5316 KB Output is correct
5 Correct 28 ms 5352 KB Output is correct
6 Correct 24 ms 5284 KB Output is correct
7 Correct 24 ms 5284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 5452 KB Output is correct
2 Correct 28 ms 5476 KB Output is correct
3 Correct 35 ms 5432 KB Output is correct
4 Correct 36 ms 5476 KB Output is correct
5 Correct 33 ms 5452 KB Output is correct
6 Correct 28 ms 5432 KB Output is correct
7 Correct 44 ms 5452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 5324 KB Output is correct
2 Correct 26 ms 5324 KB Output is correct
3 Correct 31 ms 5344 KB Output is correct
4 Correct 24 ms 5316 KB Output is correct
5 Correct 28 ms 5352 KB Output is correct
6 Correct 24 ms 5284 KB Output is correct
7 Correct 24 ms 5284 KB Output is correct
8 Correct 1677 ms 98552 KB Output is correct
9 Correct 1198 ms 98596 KB Output is correct
10 Correct 683 ms 98568 KB Output is correct
11 Correct 659 ms 98508 KB Output is correct
12 Correct 842 ms 98564 KB Output is correct
13 Correct 652 ms 98564 KB Output is correct
14 Correct 725 ms 98532 KB Output is correct
15 Correct 656 ms 98652 KB Output is correct
16 Correct 655 ms 98568 KB Output is correct
17 Correct 645 ms 98472 KB Output is correct
18 Correct 649 ms 98568 KB Output is correct
19 Correct 682 ms 98552 KB Output is correct
20 Correct 1822 ms 98592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 5452 KB Output is correct
2 Correct 28 ms 5476 KB Output is correct
3 Correct 35 ms 5432 KB Output is correct
4 Correct 36 ms 5476 KB Output is correct
5 Correct 33 ms 5452 KB Output is correct
6 Correct 28 ms 5432 KB Output is correct
7 Correct 44 ms 5452 KB Output is correct
8 Correct 845 ms 100500 KB Output is correct
9 Correct 1180 ms 100508 KB Output is correct
10 Correct 766 ms 100560 KB Output is correct
11 Correct 711 ms 100512 KB Output is correct
12 Correct 987 ms 100556 KB Output is correct
13 Correct 788 ms 100504 KB Output is correct
14 Correct 1054 ms 100504 KB Output is correct
15 Correct 739 ms 100504 KB Output is correct
16 Correct 774 ms 100544 KB Output is correct
17 Correct 1159 ms 100508 KB Output is correct
18 Correct 2113 ms 100504 KB Output is correct
19 Correct 1358 ms 100544 KB Output is correct
20 Correct 923 ms 100556 KB Output is correct
21 Correct 717 ms 100508 KB Output is correct
22 Correct 2140 ms 100500 KB Output is correct
23 Correct 1215 ms 100616 KB Output is correct
24 Correct 1379 ms 100508 KB Output is correct
25 Correct 1652 ms 100496 KB Output is correct
26 Correct 1858 ms 100608 KB Output is correct
27 Correct 1059 ms 100504 KB Output is correct
28 Correct 738 ms 100536 KB Output is correct
29 Correct 1118 ms 100504 KB Output is correct
30 Correct 1260 ms 100504 KB Output is correct
31 Correct 1851 ms 100504 KB Output is correct
32 Correct 1202 ms 100504 KB Output is correct