Submission #713464

# Submission time Handle Problem Language Result Execution time Memory
713464 2023-03-22T06:42:10 Z boris_mihov Examination (JOI19_examination) C++17
100 / 100
242 ms 17628 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

typedef long long llong;
const int MAXN = 100000 + 10;

int n, q;
struct Person
{
    int x, y;
    inline friend bool operator < (const Person &a, const Person &b)
    {
        return a.x < b.x;
    }
};

struct BIT
{
    int tree[MAXN];
    inline void reset()
    {
        std::fill(tree + 1, tree + 1 + n, 0);
    }

    inline void update(int pos, int value)
    {
        for (int idx = pos ; idx <= n ; idx += idx & (-idx))
        {
            tree[idx] += value;
        }
    }

    inline int query(int pos)
    {
        int ans = 0;
        for (int idx = pos ; idx > 0 ; idx -= idx & (-idx))
        {
            ans += tree[idx];
        }

        return ans;
    }
};

Person p[MAXN];
int answer[MAXN];

int first(int num)
{
    int l = 0, r = n + 1, mid;
    while (l < r - 1)
    {
        mid = (l + r) / 2;
        if (p[mid].x < num) l = mid;
        else r = mid;
    }

    return r;
}

// to, idx
int cntOf[MAXN];
std::vector <std::pair <int,int>> compr;
std::vector <std::pair <int,int>> bQueries[MAXN];
std::vector <std::pair <int,int>> sumQueries[MAXN];
BIT tree;

inline int firstBiggerCompr(int num)
{
    int l = -1, r = n, mid;
    while (l < r - 1)
    {
        mid = (l + r) / 2;
        if (compr[mid].first < num) l = mid;
        else r = mid;
    }

    if (l == -1) return 0;
    return cntOf[l];
}

void answerSUM()
{
    compr.reserve(n);
    for (int i = 1 ; i <= n ; ++i)
    {
        compr.push_back({p[i].x + p[i].y, i});               
    }

    int cnt = 0;
    std::sort(compr.begin(), compr.end());
    for (int i = 0 ; i < n ; ++i)
    {
        if (i == 0 || compr[i].first != compr[i - 1].first)
        {
            cnt++;
        }

        p[compr[i].second].x = cnt - p[compr[i].second].y;
        cntOf[i] = cnt;
    }

    for (int i = n ; i >= 1 ; --i)
    {
        tree.update(p[i].x + p[i].y, 1);
        for (const auto &[to, idx] : sumQueries[i])
        {
            if (idx < 0) answer[-idx] -= tree.query(n) - tree.query(firstBiggerCompr(to));
            else answer[idx] += tree.query(n) - tree.query(firstBiggerCompr(to));
        }
    }
}

void answerB()
{
    tree.reset();
    compr.clear();
    compr.reserve(n);
    for (int i = 1 ; i <= n ; ++i)
    {
        compr.push_back({p[i].y, i});               
    }

    int cnt = 0;
    std::sort(compr.begin(), compr.end());
    for (int i = 0 ; i < n ; ++i)
    {
        if (i == 0 || compr[i].first != compr[i - 1].first)
        {
            cnt++;
        }

        p[compr[i].second].y = cnt;
        cntOf[i] = cnt;
    }

    for (int i = n ; i >= 1 ; --i)
    {
        tree.update(p[i].y, 1);
        for (const auto &[to, idx] : bQueries[i])
        {
            answer[idx] += tree.query(n) - tree.query(firstBiggerCompr(to));
        }
    }
}

void solve()
{
    int a, b, c;
    std::sort(p + 1, p + 1 + n);
    for (int i = 1 ; i <= q ; ++i)
    {
        std::cin >> a >> b >> c;
        int firstL = first(a);
        int secondL = first(c - b);

        if (firstL < secondL)
        {
            sumQueries[firstL].push_back({c, i});
            sumQueries[secondL].push_back({c, -i});
        }

        if (std::max(firstL, secondL) <= n)
        {
            bQueries[std::max(firstL, secondL)].push_back({b, i});
        }
    }

    answerSUM();
    answerB();
}

void input()
{
    std::cin >> n >> q;
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> p[i].x >> p[i].y;
    }
}

void output()
{
    for (int i = 1 ; i <= q ; ++i)
    {
        std::cout << answer[i] << '\n';
    }
}

void fastIO()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIO();
    input();
    solve();
    output();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5036 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 6 ms 5280 KB Output is correct
8 Correct 7 ms 5284 KB Output is correct
9 Correct 7 ms 5332 KB Output is correct
10 Correct 7 ms 5300 KB Output is correct
11 Correct 6 ms 5204 KB Output is correct
12 Correct 5 ms 5176 KB Output is correct
13 Correct 7 ms 5204 KB Output is correct
14 Correct 6 ms 5300 KB Output is correct
15 Correct 8 ms 5312 KB Output is correct
16 Correct 5 ms 5172 KB Output is correct
17 Correct 6 ms 5332 KB Output is correct
18 Correct 5 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 127 ms 12556 KB Output is correct
2 Correct 152 ms 12564 KB Output is correct
3 Correct 126 ms 12544 KB Output is correct
4 Correct 102 ms 11856 KB Output is correct
5 Correct 96 ms 11344 KB Output is correct
6 Correct 83 ms 10612 KB Output is correct
7 Correct 111 ms 11944 KB Output is correct
8 Correct 133 ms 12456 KB Output is correct
9 Correct 123 ms 11948 KB Output is correct
10 Correct 79 ms 10392 KB Output is correct
11 Correct 98 ms 11576 KB Output is correct
12 Correct 72 ms 9240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 127 ms 12556 KB Output is correct
2 Correct 152 ms 12564 KB Output is correct
3 Correct 126 ms 12544 KB Output is correct
4 Correct 102 ms 11856 KB Output is correct
5 Correct 96 ms 11344 KB Output is correct
6 Correct 83 ms 10612 KB Output is correct
7 Correct 111 ms 11944 KB Output is correct
8 Correct 133 ms 12456 KB Output is correct
9 Correct 123 ms 11948 KB Output is correct
10 Correct 79 ms 10392 KB Output is correct
11 Correct 98 ms 11576 KB Output is correct
12 Correct 72 ms 9240 KB Output is correct
13 Correct 155 ms 14280 KB Output is correct
14 Correct 155 ms 13684 KB Output is correct
15 Correct 143 ms 12568 KB Output is correct
16 Correct 156 ms 13856 KB Output is correct
17 Correct 96 ms 12052 KB Output is correct
18 Correct 100 ms 10848 KB Output is correct
19 Correct 163 ms 14188 KB Output is correct
20 Correct 164 ms 14084 KB Output is correct
21 Correct 164 ms 14592 KB Output is correct
22 Correct 79 ms 10316 KB Output is correct
23 Correct 96 ms 11616 KB Output is correct
24 Correct 53 ms 9212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5036 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 6 ms 5280 KB Output is correct
8 Correct 7 ms 5284 KB Output is correct
9 Correct 7 ms 5332 KB Output is correct
10 Correct 7 ms 5300 KB Output is correct
11 Correct 6 ms 5204 KB Output is correct
12 Correct 5 ms 5176 KB Output is correct
13 Correct 7 ms 5204 KB Output is correct
14 Correct 6 ms 5300 KB Output is correct
15 Correct 8 ms 5312 KB Output is correct
16 Correct 5 ms 5172 KB Output is correct
17 Correct 6 ms 5332 KB Output is correct
18 Correct 5 ms 5204 KB Output is correct
19 Correct 127 ms 12556 KB Output is correct
20 Correct 152 ms 12564 KB Output is correct
21 Correct 126 ms 12544 KB Output is correct
22 Correct 102 ms 11856 KB Output is correct
23 Correct 96 ms 11344 KB Output is correct
24 Correct 83 ms 10612 KB Output is correct
25 Correct 111 ms 11944 KB Output is correct
26 Correct 133 ms 12456 KB Output is correct
27 Correct 123 ms 11948 KB Output is correct
28 Correct 79 ms 10392 KB Output is correct
29 Correct 98 ms 11576 KB Output is correct
30 Correct 72 ms 9240 KB Output is correct
31 Correct 155 ms 14280 KB Output is correct
32 Correct 155 ms 13684 KB Output is correct
33 Correct 143 ms 12568 KB Output is correct
34 Correct 156 ms 13856 KB Output is correct
35 Correct 96 ms 12052 KB Output is correct
36 Correct 100 ms 10848 KB Output is correct
37 Correct 163 ms 14188 KB Output is correct
38 Correct 164 ms 14084 KB Output is correct
39 Correct 164 ms 14592 KB Output is correct
40 Correct 79 ms 10316 KB Output is correct
41 Correct 96 ms 11616 KB Output is correct
42 Correct 53 ms 9212 KB Output is correct
43 Correct 194 ms 16276 KB Output is correct
44 Correct 159 ms 16296 KB Output is correct
45 Correct 177 ms 15744 KB Output is correct
46 Correct 169 ms 15352 KB Output is correct
47 Correct 99 ms 13264 KB Output is correct
48 Correct 82 ms 10884 KB Output is correct
49 Correct 179 ms 16184 KB Output is correct
50 Correct 172 ms 16528 KB Output is correct
51 Correct 242 ms 17628 KB Output is correct
52 Correct 92 ms 12084 KB Output is correct
53 Correct 101 ms 12540 KB Output is correct