Submission

Status:

(PPPP)(PPP)(PPPP)(PPPPPPP)(PPPPPPP)(PPPPPPPPPPP)(PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP)

Subtask/Task Score:

{9/9}{5/5}{6/6}{11/11}{14/14}{25/25}{30/30}

Score: 100

User: Phat12

Problemset: ผลึกเวลา (Crystal)

Language: cpp

Time: 0.108 second

Submitted On: 2026-10-26 23:34:31

#include <bits/stdc++.h>
#define fast ios::sync_with_stdio(false); cin.tie(nullptr)
using namespace std;

using ll = long long;
using pii = pair<int,int>;
using ti = tuple<int,int,int>;

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

const int diri[] = {0,0,1,-1,1,1,-1,-1};
const int dirj[] = {1,-1,0,0,1,-1,1,-1};
const int MOD = 1e9 + 7;
const int N = 100100;
struct Node{
    int l,r;
    int mx;
    Node(){
        l=r=0;
        mx=0;
    }
};
Node seg[N*32];
int root[N];
int cnt = 1;
void update(int t,int l,int r,int node,int a){
    if (l==r){
        seg[node].mx = max(a,seg[node].mx);
        return;
    }
    int mid = (l+r)>>1;
    if (t <= mid) {
        if (!seg[node].l) seg[node].l = ++cnt;
        update(t,l,mid,seg[node].l,a);
    }
    else {
        if (!seg[node].r) seg[node].r = ++cnt;
        update(t,mid+1,r,seg[node].r,a);
    }
    seg[node].mx = max(seg[seg[node].l].mx,seg[seg[node].r].mx);
}
int query(int ql,int qr,int l,int r,int node){
    if (!node) return -2e9;
    if (ql > r || qr < l ) return -2e9;
    if (ql <= l && r <= qr) return seg[node].mx;
    int mid = (l+r)>>1;
    return max(query(ql,qr,l,mid,seg[node].l),query(ql,qr,mid+1,r,seg[node].r));
}
int main() {
    fast;
    int u,k,n;
    cin >> u >> k >> n;
    vector<ti> vec(n);
    vector<int> cord;
    cord.reserve(n);
    for (int i=0;i<=u;i++) root[i] = ++cnt;
    for (auto &[x,y,z] : vec) {
        cin >> y >> z >> x;
        cord.push_back(y);
    }
    sort(all(cord));
    cord.erase(unique(all(cord)),cord.end());
    sort(all(vec));
    int mx = 0;
    for (auto &[t,p,w] : vec){
        auto r = lower_bound(all(cord),p)-cord.begin();
        int curr = max(0,query(1,r,1,n,root[w]));
        curr = 1+max(curr,query(1,r,1,n,root[u])-k);
        mx = max(mx,curr);
        update(r,1,n,root[w],curr);
        update(r,1,n,root[u],curr);
    }
    
    cout << mx;
    

    return 0;
}