답안 #522278

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
522278 2022-02-04T12:26:58 Z andreiomd Employment (JOI16_employment) C++11
100 / 100
596 ms 29276 KB
#include <iostream>
#include <algorithm>

using namespace std;

const int NMAX = 4e5 + 2;

class SegmentTree
{
    long long Aint[4 * NMAX], Lazy[4 * NMAX];
private:
    void Propag(int nod, int a, int b)
    {
        if(a == b)
            return;
        if(Lazy[nod] == 0)
            return;

        int mid = (a + b) >> 1;

        Aint[2 * nod] += 1LL * Lazy[nod] * (mid - a + 1);
        Aint[2 * nod + 1] += 1LL * Lazy[nod] * (b - mid);

        Lazy[2 * nod] += Lazy[nod];
        Lazy[2 * nod + 1] += Lazy[nod];

        Lazy[nod] = 0;
        return;
    }
public:
    void Update(int node, int a, int b, int ua, int ub, int val)
    {
        if(ua > ub)
            return;
        if(ua <= a && b <= ub) {
            Aint[node] += 1LL * val * (b - a + 1);
            Lazy[node] += val;
            return;
        }

        Propag(node, a, b);
        int mid = (a + b) >> 1;

        if(ua <= mid)
            Update(2 * node, a, mid, ua, ub, val);
        if(mid < ub)
            Update(2 * node + 1, mid + 1, b, ua, ub, val);

        Aint[node] = Aint[2 * node] + Aint[2 * node + 1];
        return;
    }

    long long Query(int node, int a, int b, int qa, int qb)
    {
        if(qa <= a && b <= qb) {
            return Aint[node];
        }

        Propag(node, a, b);
        int mid = (a + b) >> 1;

        long long s1 = 0, s2 = 0;
        if(qa <= mid)
            s1 = Query(2 * node, a, mid, qa, qb);
        if(mid < qb)
            s2 = Query(2 * node + 1, mid + 1, b, qa, qb);

        return s1 + s2;
    }
} seg;

struct qry
{
    int tip;
    int x;
    int pos;
    int val;
};

int N, Q;
int v[NMAX], norm[NMAX], n2[NMAX];
qry q[NMAX];

void Normalise()
{
    sort(norm + 1, norm + norm[0] + 1);

    for(int i = 1; i <= norm[0]; i++)
        if(norm[i] != norm[i - 1] || i == 1)
            n2[++n2[0]] = norm[i];

    for(int i = 1; i <= N; i++)
        v[i] = (int)(lower_bound(n2 + 1, n2 + n2[0] + 1, v[i]) - n2);

    for(int i = 1; i <= Q; i++) {
        if(q[i].tip == 1)
            q[i].x = (int)(lower_bound(n2 + 1, n2 + n2[0] + 1, q[i].x) - n2);
        else q[i].val = (int)(lower_bound(n2 + 1, n2 + n2[0] + 1, q[i].val) - n2);
    }

}

void Read()
{
    cin >> N >> Q;

    for(int i = 1; i <= N; i++)
        cin >> v[i], norm[++norm[0]] = v[i];
    for(int i = 1; i <= Q; i++){
        cin >> q[i].tip;
        if(q[i].tip == 1)
            cin >> q[i].x, norm[++norm[0]] = q[i].x;
        else {
            cin >> q[i].pos >> q[i].val;
            norm[++norm[0]] = q[i].val;
        }
    }

}

int main()
{
    Read();
    Normalise();

    for(int i = 1; i <= N; i++) {
        if(v[i - 1] < v[i])
            seg.Update(1, 1, n2[0], v[i - 1] + 1, v[i], +1);
    }

    for(int i = 1; i <= Q; i++) {
        if(q[i].tip == 1) {
            cout << seg.Query(1, 1, n2[0], q[i].x, q[i].x) << '\n';
        }
        else {
            int p = q[i].pos;

            if(v[p - 1] < v[p])
                seg.Update(1, 1, n2[0], v[p - 1] + 1, v[p], -1);
            if(p != N && v[p] < v[p + 1])
                seg.Update(1, 1, n2[0], v[p] + 1, v[p + 1], -1);

            v[p] = q[i].val;

            if(v[p - 1] < v[p])
                seg.Update(1, 1, n2[0], v[p - 1] + 1, v[p], +1);
            if(p != N && v[p] < v[p + 1])
                seg.Update(1, 1, n2[0], v[p] + 1, v[p + 1], +1);
        }
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 444 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 3 ms 444 KB Output is correct
8 Correct 3 ms 444 KB Output is correct
9 Correct 3 ms 444 KB Output is correct
10 Correct 4 ms 588 KB Output is correct
11 Correct 4 ms 572 KB Output is correct
12 Correct 4 ms 588 KB Output is correct
13 Correct 4 ms 508 KB Output is correct
14 Correct 6 ms 588 KB Output is correct
15 Correct 4 ms 588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 444 KB Output is correct
2 Correct 5 ms 716 KB Output is correct
3 Correct 4 ms 644 KB Output is correct
4 Correct 35 ms 2300 KB Output is correct
5 Correct 59 ms 4228 KB Output is correct
6 Correct 90 ms 7492 KB Output is correct
7 Correct 136 ms 13080 KB Output is correct
8 Correct 168 ms 14348 KB Output is correct
9 Correct 314 ms 26844 KB Output is correct
10 Correct 269 ms 24916 KB Output is correct
11 Correct 366 ms 27920 KB Output is correct
12 Correct 398 ms 28992 KB Output is correct
13 Correct 423 ms 29000 KB Output is correct
14 Correct 381 ms 28868 KB Output is correct
15 Correct 423 ms 28860 KB Output is correct
16 Correct 406 ms 29148 KB Output is correct
17 Correct 397 ms 29144 KB Output is correct
18 Correct 401 ms 29276 KB Output is correct
19 Correct 447 ms 29184 KB Output is correct
20 Correct 425 ms 29196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 444 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 3 ms 444 KB Output is correct
8 Correct 3 ms 444 KB Output is correct
9 Correct 3 ms 444 KB Output is correct
10 Correct 4 ms 588 KB Output is correct
11 Correct 4 ms 572 KB Output is correct
12 Correct 4 ms 588 KB Output is correct
13 Correct 4 ms 508 KB Output is correct
14 Correct 6 ms 588 KB Output is correct
15 Correct 4 ms 588 KB Output is correct
16 Correct 2 ms 444 KB Output is correct
17 Correct 5 ms 716 KB Output is correct
18 Correct 4 ms 644 KB Output is correct
19 Correct 35 ms 2300 KB Output is correct
20 Correct 59 ms 4228 KB Output is correct
21 Correct 90 ms 7492 KB Output is correct
22 Correct 136 ms 13080 KB Output is correct
23 Correct 168 ms 14348 KB Output is correct
24 Correct 314 ms 26844 KB Output is correct
25 Correct 269 ms 24916 KB Output is correct
26 Correct 366 ms 27920 KB Output is correct
27 Correct 398 ms 28992 KB Output is correct
28 Correct 423 ms 29000 KB Output is correct
29 Correct 381 ms 28868 KB Output is correct
30 Correct 423 ms 28860 KB Output is correct
31 Correct 406 ms 29148 KB Output is correct
32 Correct 397 ms 29144 KB Output is correct
33 Correct 401 ms 29276 KB Output is correct
34 Correct 447 ms 29184 KB Output is correct
35 Correct 425 ms 29196 KB Output is correct
36 Correct 2 ms 312 KB Output is correct
37 Correct 5 ms 572 KB Output is correct
38 Correct 5 ms 716 KB Output is correct
39 Correct 31 ms 2300 KB Output is correct
40 Correct 62 ms 4136 KB Output is correct
41 Correct 135 ms 7388 KB Output is correct
42 Correct 154 ms 12752 KB Output is correct
43 Correct 222 ms 14220 KB Output is correct
44 Correct 407 ms 26744 KB Output is correct
45 Correct 360 ms 24956 KB Output is correct
46 Correct 390 ms 26064 KB Output is correct
47 Correct 538 ms 28872 KB Output is correct
48 Correct 556 ms 29008 KB Output is correct
49 Correct 534 ms 28992 KB Output is correct
50 Correct 552 ms 28816 KB Output is correct
51 Correct 536 ms 29148 KB Output is correct
52 Correct 596 ms 29256 KB Output is correct
53 Correct 562 ms 29132 KB Output is correct
54 Correct 535 ms 29252 KB Output is correct
55 Correct 547 ms 29220 KB Output is correct