Submission #853485

# Submission time Handle Problem Language Result Execution time Memory
853485 2023-09-24T12:25:02 Z sofijavelkovska trapezoid (balkan11_trapezoid) C++14
100 / 100
186 ms 8784 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN=(1<<17);
const int MOD=30013;

int a[MAXN], b[MAXN], c[MAXN], d[MAXN];
pair<int, int> tree[2*MAXN-1]={{0, 0}};

bool compare(int x, int y)
{
    return a[x]<a[y];
}

pair<int, int> treemerge(pair<int, int> x, pair<int, int> y)
{
    if (x.first==y.first)
        return {x.first, (x.second+y.second)%MOD};

    return max(x, y);
}

void update(int x, int l, int r, int i, pair<int, int> value)
{
    if (l>i || r<=i)
        return;
    tree[x]=treemerge(tree[x], value);
    if (r-l==1)
        return;
    update(2*x+1, l, (l+r)/2, i, value);
    update(2*x+2, (l+r)/2, r, i, value);

    return;
}

pair<int, int> find(int x, int l, int r, int lt, int rt)
{
    if (l>=rt || r<=lt)
        return {0, 0};
    if (l>=lt && r<=rt)
        return tree[x];

    return treemerge(find(2*x+1, l, (l+r)/2, lt, rt), find(2*x+2, (l+r)/2, r, lt, rt));
}

int main()
{
    int n, maxcardinality, totalways=0, i;
    priority_queue<pair<int, int> > pq;
    cin >> n;
    int asorted[n], dpmax[n], dpcount[n];
    pair<int, int> dsorted[n];
    for (i=0; i<n; i++)
        cin >> a[i] >> b[i] >> c[i] >> d[i];
    for (i=0; i<n; i++)
        asorted[i]=i;
    sort(asorted, asorted+n, compare);
    for (i=0; i<n; i++)
        dsorted[i]={d[i], i};
    sort(dsorted, dsorted+n);
    for (i=0; i<n; i++)
    {
        dpmax[i]=1;
        dpcount[i]=1;
        while (!pq.empty() && -pq.top().first<a[asorted[i]])
        {
            int j=pq.top().second;
            int maxlower=lower_bound(dsorted, dsorted+n, make_pair(d[asorted[j]], -1))-dsorted;
            update(0, 0, MAXN, maxlower, {dpmax[j], dpcount[j]});
            pq.pop();
        }
        int maxlower=lower_bound(dsorted, dsorted+n, make_pair(c[asorted[i]], -1))-dsorted;
        if (maxlower!=0)
        {
            auto result=find(0, 0, MAXN, 0, maxlower);
            if (result.first+1>dpmax[i])
            {
                dpmax[i]=result.first+1;
                dpcount[i]=result.second;
            }
            else if (result.first+1==dpmax[i])
                dpcount[i]=(dpcount[i]+result.second)%MOD;
        }
        pq.push({-b[asorted[i]], i});
    }
    maxcardinality=*max_element(dpmax, dpmax+n);
    for (i=0; i<n; i++)
        if (dpmax[i]==maxcardinality)
            totalways=(totalways+dpcount[i])%MOD;
    cout << maxcardinality << " " << totalways;

    return 0;
}



/*#include <bits/stdc++.h>
using namespace std;

const int MAXN=100000;
const int MOD=30013;

int a[MAXN], b[MAXN], c[MAXN], d[MAXN];

bool compare(int x, int y)
{
    return b[x]<b[y];
}

bool intersect(int x, int y)
{
    if (b[x]>a[y])
        return true;
    if (d[x]>c[y])
        return true;

    return false;
}

int main()
{
    int n, maxcardinality, totalways=0, i, j;
    cin >> n;
    int sorted[n], dpmax[n], dpcount[n];
    for (i=0; i<n; i++)
        cin >> a[i] >> b[i] >> c[i] >> d[i];
    for (i=0; i<n; i++)
        sorted[i]=i;
    sort(sorted, sorted+n, compare);
    for (i=0; i<n; i++)
    {
        dpmax[i]=1;
        dpcount[i]=1;
        for (j=0; j<i; j++)
        {
            if (!intersect(sorted[j], sorted[i]))
            {
                if (dpmax[j]+1>dpmax[i])
                {
                    dpmax[i]=dpmax[j]+1;
                    dpcount[i]=dpcount[j];
                }
                else if (dpmax[j]+1==dpmax[i])
                    dpcount[i]=(dpcount[i]+dpcount[j])%30013;
            }
        }
    }
    maxcardinality=*max_element(dpmax, dpmax+n);
    for (i=0; i<n; i++)
        if (dpmax[i]==maxcardinality)
            totalways=(totalways+dpcount[i])%MOD;
    cout << maxcardinality << " " << totalways;

    return 0;
}*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 2 ms 2908 KB Output is correct
5 Correct 3 ms 2396 KB Output is correct
6 Correct 5 ms 2652 KB Output is correct
7 Correct 6 ms 2652 KB Output is correct
8 Correct 7 ms 2652 KB Output is correct
9 Correct 16 ms 2908 KB Output is correct
10 Correct 38 ms 3724 KB Output is correct
11 Correct 39 ms 3792 KB Output is correct
12 Correct 80 ms 5552 KB Output is correct
13 Correct 92 ms 5960 KB Output is correct
14 Correct 119 ms 6924 KB Output is correct
15 Correct 155 ms 6992 KB Output is correct
16 Correct 133 ms 7588 KB Output is correct
17 Correct 148 ms 7872 KB Output is correct
18 Correct 143 ms 8020 KB Output is correct
19 Correct 144 ms 8280 KB Output is correct
20 Correct 186 ms 8784 KB Output is correct