답안 #776979

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
776979 2023-07-08T12:55:21 Z borisAngelov Meteors (POI11_met) C++17
24 / 100
6000 ms 44740 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];

struct state
{
    long long sum;
    long long lazy;
};

state tree[4 * maxn];

void reset_tree()
{
    for (int i = 0; i < 4 * maxn; ++i)
    {
        tree[i].sum = 0;
        tree[i].lazy = 0;
    }
}

void push_lazy(int node, int l, int r)
{
    if (tree[node].lazy != 0)
    {
        tree[node].sum += (1LL * (r - l + 1)) * tree[node].lazy;

        if (l != r)
        {
            tree[2 * node].lazy += tree[node].lazy;
            tree[2 * node + 1].lazy += tree[node].lazy;
        }

        tree[node].lazy = 0LL;
    }
}

void update(int node, int l, int r, int ql, int qr, long long delta)
{
    push_lazy(node, l, r);

    if (l > qr || r < ql)
    {
        return;
    }

    if (ql <= l && r <= qr)
    {
        tree[node].lazy += delta;
        push_lazy(node, l, r);
        return;
    }

    int mid = (l + r) / 2;

    update(2 * node, l, mid, ql, qr, delta);
    update(2 * node + 1, mid + 1, r, ql, qr, delta);

    tree[node].sum = tree[2 * node].sum + tree[2 * node + 1].sum;
}

long long point_query(int node, int l, int r, int idx)
{
    push_lazy(node, l, r);

    if (l == r)
    {
        return tree[node].sum;
    }

    int mid = (l + r) / 2;

    if (idx <= mid)
    {
        return point_query(node * 2, l, mid, idx);
    }

    return point_query(node * 2 + 1, mid + 1, r, idx);
}

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

    int mid = (l + r) / 2;

    reset_tree();

    for (int i = l; i <= mid; ++i)
    {
        if (queries[i].l > queries[i].r)
        {
            update(1, 1, m, queries[i].l, m, queries[i].x);
            update(1, 1, m, 1, queries[i].r, queries[i].x);
        }
        else
        {
            update(1, 1, m, 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 += point_query(1, 1, m, 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);
        }
    }

    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:127:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  127 |     for (int i = 0; i < active.size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1598 ms 26208 KB Output is correct
2 Correct 1474 ms 26220 KB Output is correct
3 Correct 1541 ms 26208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1365 ms 26232 KB Output is correct
2 Correct 1344 ms 26236 KB Output is correct
3 Correct 1445 ms 26368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6058 ms 28232 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6067 ms 28628 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6069 ms 28236 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6052 ms 28240 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6077 ms 44740 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6033 ms 44192 KB Time limit exceeded
2 Halted 0 ms 0 KB -