QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#196814#7513. Palindromic Beadsucup-team1134WA 602ms85440kbC++179.6kb2023-10-01 23:31:182023-10-01 23:31:18

Judging History

你现在查看的是最新测评结果

  • [2024-03-27 16:34:54]
  • hack成功,自动添加数据
  • (/hack/584)
  • [2024-03-27 16:18:45]
  • hack成功,自动添加数据
  • (/hack/583)
  • [2023-10-01 23:31:18]
  • 评测
  • 测评结果:WA
  • 用时:602ms
  • 内存:85440kb
  • [2023-10-01 23:31:18]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=1<<30;

// https://kopricky.github.io/code/SegmentTrees/rangetree_pointupdate.html

#define all(v) (v).begin(),(v).end()

template<typename T> class segtree {
private:
    int n, sz;
    vector<T> node;
public:
    void init(const vector<T>& v){
        sz = (int)v.size();
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.assign(2*n, numeric_limits<T>::min());
        for(int i = 0; i < sz; i++){
            node[i+n] = v[i];
        }
        for(int i=n-1; i>=1; i--){
            node[i] = max(node[2*i], node[2*i+1]);
        }
    }
    void update(int k, const T a)
    {
        node[k+=n] = a;
        while(k>>=1){
            node[k] = max(node[2*k], node[2*k+1]);
        }
    }
    T query(int a, int b)
    {
        T res1 = numeric_limits<T>::min(), res2 = numeric_limits<T>::min();
        a += n, b += n;
        while(a != b){
            if(a % 2) res1 = max(res1, node[a++]);
            if(b % 2) res2 = max(res2, node[--b]);
            a >>= 1, b >>= 1;
        }
        return max(res1, res2);
    }
    void print(){
        for(int i = 0; i < sz; i++){
            cout << query(i, i+1) << " ";
        }
        cout << endl;
    }
};

//座標の型, 値の型
template<typename CandidateType, typename ValueType> class RangeTree
{
public:
    static_assert(std::is_integral<CandidateType>::value, "Integral required.");
private:
    using CT = CandidateType;
    using VT = ValueType;
    using pcc = pair<CT, CT>;
    using pci = pair<CT, int>;
    int n, sz;
    vector<segtree<VT> > seg;
    // y座標, x座標
    vector<vector<pcc> > yx;
    // y座標, x座標
    vector<pcc> sorted;
    void update_(int id, const CT x, const CT y, const VT val) {
        id += n-1;
        const int yid = lower_bound(all(yx[id]), pcc(y, x)) - yx[id].begin();
        seg[id].update(yid, val);
        while(id > 0){
            id = (id - 1) / 2;
            const int yid = lower_bound(all(yx[id]), pcc(y, x)) - yx[id].begin();
            seg[id].update(yid, val);
        }
    }
    VT query(const int lxid, const int rxid, const CT ly, const CT ry, const int k, const int l, const int r) {
        if(r <= lxid || rxid <= l) return numeric_limits<VT>::min();
        if(lxid <= l && r <= rxid){
            const int lyid = lower_bound(all(yx[k]), pcc(ly, numeric_limits<CT>::min())) - yx[k].begin();
            const int ryid = upper_bound(all(yx[k]), pcc(ry, numeric_limits<CT>::min())) - yx[k].begin();
            return (lyid >= ryid) ? numeric_limits<VT>::min() : seg[k].query(lyid, ryid);
        }else{
            return max(query(lxid, rxid, ly, ry, 2*k+1, l, (l+r)/2), query(lxid, rxid, ly, ry, 2*k+2, (l+r)/2, r));
        }
    }
public:
    // 座標, 点の値
    RangeTree(const vector<pcc>& cand, const vector<VT>& val) : n(1), sz((int)cand.size()), sorted(sz){
        while(n < sz) n *= 2;
        for(int i = 0; i < sz; ++i){
            sorted[i] = {cand[i].first, i};
        }
        sort(all(sorted), [&](const pcc& a, const pcc& b){
            return (a.first == b.first) ? (cand[a.second].second < cand[b.second].second) : (a.first < b.first);
        });
        yx.resize(2*n-1), seg.resize(2*n-1);
        for(int i = 0; i < sz; ++i){
            yx[i+n-1] = {{sorted[i].second, sorted[i].first}};
            vector<VT> arg = {val[sorted[i].second]};
            seg[i+n-1].init(arg);
            sorted[i].second = cand[sorted[i].second].second;
        }
        for(int i = n-2; i >= 0; --i){
            yx[i].resize((int)yx[2*i+1].size() + (int)yx[2*i+2].size());
            if(yx[i].empty()) continue;
            merge(all(yx[2*i+1]), all(yx[2*i+2]), yx[i].begin(), [&](const pcc& a, const pcc& b){
                return (cand[a.first].second == cand[b.first].second)
                ? (a.second < b.second) : (cand[a.first].second < cand[b.first].second);
            });
            vector<VT> arg((int)yx[i].size());
            for(int j = 0; j < (int)yx[i].size(); ++j){
                arg[j] = val[yx[i][j].first];
            }
            seg[i].init(arg);
        }
        for(int i = 0; i < 2*n-1; ++i){
            for(pcc& e : yx[i]){
                e.first = cand[e.first].second;
            }
        }
    }
    // 点 (x,y) の更新を行う
    void update(const CT x, const CT y, const VT val){
        const int id = lower_bound(all(sorted), pcc(x, y)) - sorted.begin();
        return update_(id, x, y, val);
    }
    // [lx,rx) × [ly,ry) の長方形領域のクエリに答える
    VT query(const CT lx, const CT ly, const CT rx, const CT ry){
        const int lxid = lower_bound(all(sorted), pcc(lx, numeric_limits<CT>::min())) - sorted.begin();
        const int rxid = upper_bound(all(sorted), pcc(rx, numeric_limits<CT>::min())) - sorted.begin();
        return (lxid >= rxid) ? numeric_limits<VT>::min() : query(lxid, rxid, ly, ry, 0, 0, n);
    }
};

struct HeavyLightDecomposition{
    int n;
    vector<int> sz,in,out,nxt,par,depth;
    vector<vector<int>> G;
    
    HeavyLightDecomposition(){}
    
    HeavyLightDecomposition(int n_){
        n=n_;
        sz.assign(n,0);
        in.assign(n,0);
        out.assign(n,0);
        nxt.assign(n,0);
        par.assign(n,0);
        depth.assign(n,0);
        G.assign(n,vector<int>());
    }
    
    void add_edge(int u,int v){
        G[u].push_back(v);
        G[v].push_back(u);
    }
    
    void dfs_sz(int u,int p){
        par[u]=p;
        sz[u]=1;
        if(G[u].size()&&G[u][0]==p) swap(G[u][0],G[u].back());
        for(auto &a:G[u]){
            if(a==p) continue;
            depth[a]=depth[u]+1;
            dfs_sz(a,u);
            sz[u]+=sz[a];
            if(sz[a]>sz[G[u][0]]){
                swap(a,G[u][0]);
            }
        }
    }
    
    void dfs_hld(int u,int p,int &t){
        in[u]=t++;
        for(auto a:G[u]){
            if(a==p) continue;
            nxt[a]=(a==G[u][0] ? nxt[u] : a);
            dfs_hld(a,u,t);
        }
        out[u]=t;
    }
    
    void build(int u){
        int t=0;
        dfs_sz(u,-1);
        dfs_hld(u,-1,t);
    }
    
    int lca(int u,int v){
        if(in[u]>in[v]) swap(u,v);
        if(nxt[u]==nxt[v]) return u;
        return lca(u,par[nxt[v]]);
    }
    
    int mov1(int a,int b){
        if(a==b) return a;
        int c=lca(a,b);
        if(c==a){
            int l=0,r=si(G[a]);
            while(r-l>1){
                int m=(l+r)/2;
                if(par[a]==G[a][m]){
                    if(m+1<r){
                        if(r-l==2){
                            l=m+1;
                            break;
                        }
                        if(in[G[a][m+1]]<=in[b]) l=m+1;
                        else r=m;
                    }else{
                        if(r-l==2){
                            l=m-1;
                            break;
                        }
                        if(in[G[a][m-1]]<=in[b]) l=m-1;
                        else r=m-1;
                    }
                }else{
                    if(in[G[a][m]]<=in[b]) l=m;
                    else r=m;
                }
            }
            if(par[a]!=G[a][l]) return G[a][l];
            else return G[a][l+1];
            //return G[a][l];
        }else{
            return par[a];
        }
    }
    //aからbに向かって1進んだところ
};
//部分木クエリはquery(in[a],out[a])
//点はquery(in[a],in[a]+1)

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N;cin>>N;
    vector<vector<int>> coc(N);
    for(int i=0;i<N;i++){
        int x;cin>>x;x--;
        coc[x].push_back(i);
    }
    
    HeavyLightDecomposition hld(N);
    for(int i=0;i<N-1;i++){
        int a,b;cin>>a>>b;a--;b--;
        hld.add_edge(a,b);
    }
    
    hld.build(0);
    
    int ans=1;
    
    vector<pair<int,int>> S;
    for(int i=0;i<N;i++){
        if(si(coc[i])==2){
            int a=coc[i][0],b=coc[i][1];
            if(hld.in[a]>hld.in[b]) swap(a,b);
            S.push_back(mp(a,b));
        }
    }
    
    auto dis=[&](int a,int b){
        return hld.depth[a]+hld.depth[b]-2*hld.depth[hld.lca(a,b)];
    };
    
    sort(all(S),[&](auto a,auto b){
        return dis(a.fi,a.se)>dis(b.fi,b.se);
    });
    
    vector<pair<int,int>> SS;
    for(auto [a,b]:S){
        SS.push_back(mp(hld.in[a],hld.in[b]));
    }
    
    vector<int> valval(si(SS));
    
    RangeTree RT(SS,valval);
    
    for(auto [a,b]:S){
        if(hld.lca(a,b)==a){
            int to=hld.mov1(a,b);
            int ma=0;
            chmax(ma,RT.query(0,hld.in[b],hld.in[to],hld.out[b]));
            chmax(ma,RT.query(hld.in[b],hld.out[a],hld.out[b],hld.out[0]+1));
            ma+=2;
            RT.update(hld.in[a],hld.in[b],ma);
            if(dis(a,b)>1) chmax(ans,ma+1);
            else chmax(ans,ma);
        }else{
            int ma=RT.query(hld.in[a],hld.in[b],hld.out[a],hld.out[b])+2;
            RT.update(hld.in[a],hld.in[b],ma);
            if(dis(a,b)>1) chmax(ans,ma+1);
            else chmax(ans,ma);
        }
    }
    
    cout<<ans<<endl;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3532kb

input:

4
1 1 2 2
1 2
2 3
2 4

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3668kb

input:

5
1 3 2 2 1
1 2
2 3
3 4
4 5

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3572kb

input:

6
1 1 2 2 3 3
1 2
2 3
3 4
4 5
5 6

output:

2

result:

ok single line: '2'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3640kb

input:

6
1 2 3 4 5 6
1 2
2 3
3 4
4 5
5 6

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 2ms
memory: 4012kb

input:

2000
845 1171 345 282 1181 625 754 289 681 493 423 840 1494 318 266 1267 967 379 135 14 39 191 60 972 116 1216 1205 19 194 185 1360 861 379 430 1262 1151 756 65 389 488 277 53 1283 1438 101 1465 195 714 737 980 80 298 961 1326 163 1163 1317 1152 992 35 334 802 1502 486 710 234 555 88 1278 146 46 696...

output:

5

result:

ok single line: '5'

Test #6:

score: 0
Accepted
time: 505ms
memory: 77692kb

input:

200000
48015 47923 20609 71806 43752 68214 95683 89449 25809 58110 19878 52931 7845 45206 86245 82945 62977 37876 12456 105915 10509 92943 66950 88545 26442 26545 42278 66977 3970 9631 21524 43638 7979 58240 25719 56260 276 89721 9553 16550 52161 30307 82748 108443 36676 48581 59069 57412 62453 7965...

output:

5

result:

ok single line: '5'

Test #7:

score: 0
Accepted
time: 466ms
memory: 77736kb

input:

200000
13011 51198 65374 107045 66506 14385 35784 94265 71449 41817 24646 60714 53382 68358 9354 840 3139 71282 72215 69550 2121 41498 13675 76444 67690 40513 56439 12832 51976 35333 47208 59602 98993 9383 77866 10464 41517 89125 58804 91741 66160 74208 70991 63865 84870 14282 2441 78046 73372 36311...

output:

7

result:

ok single line: '7'

Test #8:

score: 0
Accepted
time: 484ms
memory: 78192kb

input:

200000
38715 33241 65919 39407 27500 36200 2259 42301 79147 57505 20 81399 69499 23658 14534 86934 14352 69558 59763 43318 35360 3281 38188 40058 40571 103709 75625 8434 53802 87159 98628 69421 53711 47986 18350 6079 37362 39377 71936 89573 25983 66882 48999 58918 66432 17453 82515 9588 95375 87287 ...

output:

45

result:

ok single line: '45'

Test #9:

score: 0
Accepted
time: 511ms
memory: 78808kb

input:

200000
36449 57574 3145 38591 832 17710 66613 78947 27635 83275 89878 48329 94614 584 96832 9321 72046 44873 5396 61452 63224 63740 26579 13706 108490 19092 89439 85884 12016 5105 48638 74004 41569 35006 22276 45609 25350 49906 35479 15875 68938 77699 48828 21628 11242 77040 70838 45771 27704 64865 ...

output:

135

result:

ok single line: '135'

Test #10:

score: 0
Accepted
time: 602ms
memory: 81908kb

input:

200000
13528 65006 30352 8565 36687 6748 5507 44320 7189 17847 46996 82728 102722 4727 36914 74228 21460 87970 11733 47170 67282 104558 66436 64504 57055 88619 42995 25569 101298 90984 76491 51994 62257 103424 8221 69668 99170 6808 29043 73058 5277 26614 23654 25152 64939 38418 78518 5330 37531 4305...

output:

425

result:

ok single line: '425'

Test #11:

score: 0
Accepted
time: 597ms
memory: 85440kb

input:

200000
92279 5566 62695 50240 45387 51097 57743 94873 53220 29260 72584 38043 86335 33441 12946 30267 12932 18258 4560 8896 64393 39608 53183 34285 36518 18501 51940 8658 101018 48522 21336 104735 25785 73132 33489 81905 94563 18128 87872 24765 54563 57218 61869 50458 75919 63764 48155 4489 35212 44...

output:

839

result:

ok single line: '839'

Test #12:

score: 0
Accepted
time: 458ms
memory: 74172kb

input:

200000
96438 84772 88103 81186 60908 116093 94738 28602 35022 91108 60687 75572 100094 50553 58445 117024 96154 53539 23185 112280 90369 32413 95244 11077 91008 109781 6404 2285 3544 111712 49414 10399 113626 81435 11321 52557 17023 113260 14225 66464 61352 98403 36521 110038 57172 42868 68512 69031...

output:

31

result:

ok single line: '31'

Test #13:

score: -100
Wrong Answer
time: 435ms
memory: 75088kb

input:

200000
50750 86282 92049 114579 8296 28675 45880 47381 71400 43379 111535 32316 37104 35968 100241 6914 81284 48969 62890 63486 107557 80178 76322 31515 24682 85646 12681 106054 5167 50339 39004 16152 112081 10605 66750 51623 96332 77287 75452 50609 1549 1652 45229 73171 10015 66323 90164 97491 1007...

output:

8106

result:

wrong answer 1st lines differ - expected: '8108', found: '8106'