Submission #51054

#TimeUsernameProblemLanguageResultExecution timeMemory
51054SpaimaCarpatilorMatryoshka (JOI16_matryoshka)C++17
100 / 100
298 ms11068 KiB
#include<bits/stdc++.h>

using namespace std;

int nr, N, M, Q, h[200009], B[200009], realV[200009], aib[200009], dp[200009], ans[200009];
pair < int, int > v[200009], nrm[200009];
pair < pair < int, int >, int > qr[200009];

void Update (int pos, int val)
{
    while (pos <= M)
        aib[pos] = max (aib[pos], val),
        pos += pos - (pos & (pos - 1));
}

int Query (int pos)
{
    int ans = 0;
    while (pos)
        ans = max (ans, aib[pos]),
        pos &= pos - 1;
    return ans;
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

scanf ("%d %d", &N, &Q);
for (int i=1; i<=N; i++)
    scanf ("%d %d", &v[i].first, &v[i].second), v[i].second = -v[i].second;
sort (v + 1, v + N + 1);
for (int i=1; i<=N; i++)
    v[i].second = -v[i].second, nrm[i] = {v[i].second, i};
sort (nrm + 1, nrm + N + 1), M = 0;
for (int i=1; i<=N; i++)
{
    if (i == 1 || nrm[i].first != nrm[i - 1].first)
        realV[++M] = nrm[i].first;
    B[nrm[i].second] = M;
}
for (int i=N; i>=1; i--)
    dp[i] = Query (B[i]) + 1,
    Update (B[i], dp[i]);
for (int i=1; i<=Q; i++)
    scanf ("%d %d", &qr[i].first.first, &qr[i].first.second),
    qr[i].first.first = -qr[i].first.first, qr[i].second = i;
sort (qr + 1, qr + Q + 1);
memset (aib, 0, sizeof (aib));
for (int i=1; i<=Q; i++)
    qr[i].first.first = -qr[i].first.first;
int j = N;
for (int i=1; i<=Q; i++)
{
    while (v[j].first >= qr[i].first.first && j > 0)
        Update (B[j], dp[j]), j --;
    int pos = upper_bound (realV + 1, realV + M + 1, qr[i].first.second) - realV - 1;
    ans[qr[i].second] = Query (pos);
}
for (int i=1; i<=Q; i++)
    printf ("%d\n", ans[i]);
return 0;
}

Compilation message (stderr)

matryoshka.cpp: In function 'int main()':
matryoshka.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf ("%d %d", &N, &Q);
 ~~~~~~^~~~~~~~~~~~~~~~~
matryoshka.cpp:32:47: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d %d", &v[i].first, &v[i].second), v[i].second = -v[i].second;
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
matryoshka.cpp:48:43: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d %d", &qr[i].first.first, &qr[i].first.second),
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     qr[i].first.first = -qr[i].first.first, qr[i].second = i;
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...