잡글 가득 블로그
article thumbnail

문제 링크

꾸준히 다이아를 풀고 있어서 기쁘다.

Subtask: $N5\,000$ (40점)

더보기

단순히 $O(n^2)$ LIS를 돌리면 된다.

#include <bits/stdc++.h>
using namespace std; using ii = pair<int,int>; using ll = long long;
void o_o(){ cerr << endl; }
template <class H, class...T> void o_o(H h,T...t) { cerr << ' ' << h; o_o(t...); }
#define debug(...) cerr<<'['<<#__VA_ARGS__<<"]:",o_o(__VA_ARGS__)
#define rep(i,a,b) for (auto i = (a); i <= (b); ++i)
#define all(x) (x).begin(), (x).end()
#define size(x) int((x).size())
#define fi first
#define se second
#define Mup(x,y) x = max(x,y)

const int N = 1e5+3;
int n;
pair<ii,ii> t[N];
int lis[N], cnt[N];
int ans1, ans2;

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    rep(i,1,n) {
        cin >> t[i].fi.fi >> t[i].se.fi;
        cin >> t[i].fi.se >> t[i].se.se;
    }
    sort(t+1,t+n+1);
    rep(i,1,n) {
        lis[i] = 1, cnt[i] = 1;
        rep(j,1,n-1) {
            if (t[j].se.fi < t[i].fi.fi and t[j].se.se < t[i].fi.se) {
                if (lis[i] < lis[j]+1) {
                    lis[i] = lis[j]+1;
                    cnt[i] = cnt[j];
                } else if (lis[i] == lis[j]+1)
                    cnt[i] = (cnt[i]+cnt[j])%30013;
            }
        }
        Mup(ans1, lis[i]);
    }
    rep(i,1,n) if (ans1 == lis[i])
        ans2 = (ans2+cnt[i])%30013;
    cout << ans1 << ' ' << ans2;
}

Fulltask: $N\le 10^5$ (60점)

더보기

이런 스위핑 문제는 2차원 평면에 두고 보면 좀 이해가 잘 되는 것 같더라.

$(a_i,c_i),\ (b_i,d_i)$를 고려하면 되는 것인데,

$(a_i,c_i)$ 기준으로 앞에 나타나는 것들을 고려해주고, $(b_i,d_i)$는 대기했다가 $b_i$의 순서가 왔을 때 다시 넣어주는 식으로 풀면 된다.

#include <bits/stdc++.h>
using namespace std; using ii = pair<int,int>; using ll = long long;
void o_o(){ cerr << endl; }
template <class H, class...T> void o_o(H h,T...t) { cerr << ' ' << h; o_o(t...); }
#define debug(...) cerr<<'['<<#__VA_ARGS__<<"]:",o_o(__VA_ARGS__)
#define rep(i,a,b) for (auto i = (a); i <= (b); ++i)
#define all(x) (x).begin(), (x).end()
#define size(x) int((x).size())
#define fi first
#define se second
#define Mup(x,y) x = max(x,y)

const int N = 3e5+3;
int n;
priority_queue<pair<ii,ii>,vector<pair<ii,ii>>,greater<>> pq;
struct segment_tree {
    int t[2*N], c[2*N];
    void update(int k, int v1, int v2) {
        k += N;
        for (t[k] = v1, c[k] = v2%30013; (k /= 2) >= 1;) {
            if (t[2*k] < t[2*k+1]) c[k] = c[2*k+1]%30013;
            else if (t[2*k] > t[2*k+1]) c[k] = c[2*k]%30013;
            else c[k] = (c[2*k]+c[2*k+1])%30013;
            t[k] = max(t[2*k],t[2*k+1]);
        }
    }
    ii query(int a, int b) {
        int r = 0, s = 0;
        for (int x = a+N, y = b+N; x <= y; ++x /= 2, --y /= 2) {
            if (x&1) Mup(r,t[x]);
            if (~y&1) Mup(r,t[y]);
        }
        for (int x = a+N, y = b+N; x <= y; ++x /= 2, --y /= 2) {
            if (x&1) { if (t[x] == r) s = (s+c[x])%30013; }
            if (~y&1) { if (t[y] == r) s = (s+c[y])%30013; }
        }
        return {r,s};
    }
} ds;
pair<ii,ii> t[N];

void pre() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    vector<int> xs, ys;
    rep(i,1,n) {
        cin >> t[i].fi.fi >> t[i].se.fi;
        xs.push_back(t[i].fi.fi), xs.push_back(t[i].se.fi);
        cin >> t[i].fi.se >> t[i].se.se;
        ys.push_back(t[i].fi.se), ys.push_back(t[i].se.se);
    }
    sort(all(xs)), xs.erase(unique(all(xs)),end(xs));
    sort(all(ys)), ys.erase(unique(all(ys)),end(ys));
    rep(i,1,n) {
        t[i].fi.fi = lower_bound(all(xs),t[i].fi.fi)-begin(xs);
        t[i].fi.se = lower_bound(all(ys),t[i].fi.se)-begin(ys);
        t[i].se.fi = lower_bound(all(xs),t[i].se.fi)-begin(xs);
        t[i].se.se = lower_bound(all(ys),t[i].se.se)-begin(ys);
    }
    sort(t+1,t+n+1);
}
int main() {
    pre();
    rep(i,1,n) {
        auto [p1,p2] = t[i];
        while (not empty(pq) and pq.top().fi.fi < p1.fi) {
            ds.update(pq.top().fi.se, pq.top().se.fi,pq.top().se.se);
            pq.pop();
        }
        auto [v1,v2] = ds.query(0,p1.se);
        if (v1 == 0) v2 = 1;
        pq.emplace(p2,ii{v1+1,v2});
    }
    while (not empty(pq)) {
        ds.update(pq.top().fi.se,pq.top().se.fi,pq.top().se.se);
        pq.pop();
    }
    auto [a1,a2] = ds.query(0,2*n+2);
    cout << a1 << ' ' << a2;
}

디버깅

더보기

priority_queue에서 greater<>인데 less<>를 적고 있었다 ㅋㅋ

음... 그리고 마지막 답을 내는 쿼리에서 범위는 2*n보다 좀 크게 잡아야 되는데 n보다 좀 크게 잡았다.

아... 그리고 그냥 Pair 쓰다가 계속 쓴거긴 한데...

이거 pair 쓰니까 진짜 혼동이 오네 ㅋㅋ

앞으로 pair는 좀 지양해야겠다. struct 애용하기!!

profile

잡글 가득 블로그

@도훈.

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

profile on loading

Loading...