[bzoj4919][Lydsy1706 race] big root heap [dp], [heuristic combination], [stl]

Keywords: PHP

[title link]
  https://www.lydsy.com/JudgeOnline/problem.php?id=4919
[Abstract]
A wonderful question. Consider the method of finding the longest ascending sequence by dp of N * logNN * logN. We store each of these values in multiset. Then the ranking of one value from small to large is the sequence length with the value at the end of the sequence.
Now consider extending it to trees. Obviously, the two subtrees do not interfere with each other, so they can be merged heuristic. After merging, process the root node and change the first number larger than it to it. If not, insert directly. (imagine the process of a chain).
Time complexity: O(N * logN)O(N * logN)

/* --------------
    user Vanisher
    problem tree
----------------*/
# include <bits/stdc++.h>
# define    ll      long long
# define    inf     0x3f3f3f3f
# define    N       200010
using namespace std;
int read(){
    int tmp=0, fh=1; char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') fh=-1; ch=getchar();}
    while (ch>='0'&&ch<='9'){tmp=tmp*10+ch-'0'; ch=getchar();}
    return tmp*fh;
} 
multiset <int> mp[N];
struct node{
    int val,id,fa;
}p[N];
int n,f[N];
bool cmpval(node a, node b){return a.val<b.val;}
bool cmpid(node a, node b){return a.id<b.id;}
int mixed(int x, int y){
    if (mp[x].size()<mp[y].size())
        swap(x,y);
    multiset <int> :: iterator it=mp[y].begin();
    while (it!=mp[y].end()){
        mp[x].insert(*it);
        it++;
    }
    return x; 
}
int main(){
    n=read();
    for (int i=1; i<=n; i++){
        p[i].val=read(); p[i].fa=read();
        p[i].id=i;
    }
    sort(p+1,p+n+1,cmpval);
    int las=-1,cnt=0;
    for (int i=1; i<=n; i++){
        if (p[i].val!=las){
            las=p[i].val;
            cnt++;
        } 
        p[i].val=cnt;
    } 
    sort(p+1,p+n+1,cmpid);
    for (int i=1; i<=n; i++) f[i]=i;
    for (int i=n; i>=1; i--){
        multiset <int> :: iterator it=mp[f[i]].lower_bound(p[i].val);
        if (it!=mp[f[i]].end()) mp[f[i]].erase(it);
        mp[f[i]].insert(p[i].val);
        f[p[i].fa]=mixed(f[i],f[p[i].fa]);
    }
    printf("%d\n",(int)mp[f[1]].size());
    return 0;
}

Posted by gsb on Sat, 04 Apr 2020 20:06:42 -0700