Submission #776994

# Submission time Handle Problem Language Result Execution time Memory
776994 2023-07-08T13:10:16 Z borisAngelov Meteors (POI11_met) C++17
100 / 100
1029 ms 65536 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 300005;

int n, m, q;

long long required_sum[maxn];

struct element
{
    int l;
    int r;
    long long x;
};

element queries[maxn];

int answer[maxn];

vector<int> positions[maxn];

long long preffix[maxn];

long long tree[maxn];

vector<int> to_clear;

void update(int pos, long long val)
{
    for (int i = pos; i <= m; i += (i & (-i)))
    {
        tree[i] += val;
        to_clear.push_back(i);
    }
}

long long query(int pos)
{
    long long result = 0;

    for (int i = pos; i >= 1; i -= (i & (-i)))
    {
        result += tree[i];
    }

    return result;
}

void increase(int l, int r, long long delta)
{
    if (l <= r)
    {
        //preffix[l] += delta;
        //preffix[r + 1] -= delta;

        update(l, +delta);
        update(r + 1, -delta);
    }
    else
    {
        //preffix[1] += delta;
        //preffix[r + 1] -= delta;
        //preffix[l] += delta;

        update(1, +delta);
        update(r + 1, -delta);
        update(l, +delta);
    }
}

void divide(int l, int r, vector<int>& active)
{
    if (l > r)
    {
        return;
    }

    int mid = (l + r) / 2;

    for (int i = l; i <= mid; ++i)
    {
        increase(queries[i].l, queries[i].r, queries[i].x);
    }

    vector<int> left_active;
    vector<int> right_active;

    for (int i = 0; i < active.size(); ++i)
    {
        int id = active[i];

        long long curr_sum = 0;

        for (auto pos : positions[id])
        {
            curr_sum += query(pos);

            if (curr_sum >= required_sum[id])
            {
                break;
            }
        }

        if (curr_sum >= required_sum[id])
        {
            answer[id] = mid;
            left_active.push_back(id);
        }
        else
        {
            required_sum[id] -= curr_sum;
            right_active.push_back(id);
        }
    }

    for (auto pos : to_clear)
    {
        tree[pos] = 0LL;
    }

    to_clear.clear();

    divide(l, mid - 1, left_active);
    divide(mid + 1, r, right_active);
}

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

int main()
{
    fastIO();

    cin >> n >> m;

    for (int i = 1; i <= m; ++i)
    {
        int id;
        cin >> id;

        positions[id].push_back(i);
    }

    vector<int> active;

    for (int i = 1; i <= n; ++i)
    {
        cin >> required_sum[i];
        answer[i] = -1;
        active.push_back(i);
    }

    cin >> q;

    for (int i = 1; i <= q; ++i)
    {
        cin >> queries[i].l >> queries[i].r >> queries[i].x;
    }

    divide(1, q, active);

    for (int i = 1; i <= n; ++i)
    {
        if (answer[i] == -1)
        {
            cout << "NIE\n";
        }
        else
        {
            cout << answer[i] << "\n";
        }
    }

    return 0;
}

Compilation message

met.cpp: In function 'void divide(int, int, std::vector<int>&)':
met.cpp:90:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |     for (int i = 0; i < active.size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7508 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 11136 KB Output is correct
2 Correct 98 ms 14516 KB Output is correct
3 Correct 73 ms 11440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 13380 KB Output is correct
2 Correct 82 ms 13348 KB Output is correct
3 Correct 93 ms 13836 KB Output is correct
4 Correct 21 ms 9296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 13068 KB Output is correct
2 Correct 73 ms 13688 KB Output is correct
3 Correct 37 ms 10380 KB Output is correct
4 Correct 74 ms 11592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 13012 KB Output is correct
2 Correct 85 ms 13372 KB Output is correct
3 Correct 63 ms 11080 KB Output is correct
4 Correct 93 ms 14028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 784 ms 52612 KB Output is correct
2 Correct 492 ms 55176 KB Output is correct
3 Correct 188 ms 21952 KB Output is correct
4 Correct 949 ms 64824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 769 ms 52140 KB Output is correct
2 Correct 492 ms 53700 KB Output is correct
3 Correct 155 ms 22204 KB Output is correct
4 Correct 1029 ms 65536 KB Output is correct