Submission #308293

# Submission time Handle Problem Language Result Execution time Memory
308293 2020-09-30T20:37:18 Z andreiomd Employment (JOI16_employment) C++11
40 / 100
491 ms 20600 KB
#include <iostream>
#include <algorithm>

using namespace std;

typedef pair < int, int > PII;
typedef pair < int, PII > Type;

const int NMAX = 2e5 + 1, QMAX = 2e5 + 1;

int N, Q, A[NMAX];
Type Queries[QMAX];

int B[(NMAX + QMAX)], V[(NMAX + QMAX)];

static inline void Read ()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N >> Q;

    for(int i = 1; i <= N; ++i)
        cin >> A[i], B[++B[0]] = A[i];

    for(int i = 1; i <= Q; ++i)
    {
        cin >> Queries[i].first >> Queries[i].second.first;

        if(Queries[i].first == 2)
            cin >> Queries[i].second.second, B[++B[0]] = Queries[i].second.second;
        else
            B[++B[0]] = Queries[i].second.first;
    }

    return;
}

static inline int Find (int Val)
{
    return (int)(lower_bound(V + 1, V + V[0] + 1, Val) - V);
}

static inline void Replace ()
{
    for(int i = 1; i <= N; ++i)
        A[i] = Find(A[i]);

    for(int i = 1; i <= Q; ++i)
        if(Queries[i].first == 1)
            Queries[i].second.first = Find(Queries[i].second.first);
        else
            Queries[i].second.second = Find(Queries[i].second.second);

    return;
}

static inline void Normalize ()
{
    sort(B + 1, B + B[0] + 1);

    V[++V[0]] = B[1];

    for(int i = 2; i <= B[0]; ++i)
        if(B[i] != B[i - 1])
            V[++V[0]] = B[i];

    Replace();

    return;
}

class SegmentTree
{
    static const int NMAX = (4e5 + 1);
    int A[(NMAX << 2)], Lazy[(NMAX << 2)];

private:
    inline void Go (int Node, int a, int b)
    {
        if(a == b)
            return;

        if(Lazy[Node] == 0)
            return;

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

        A[(Node << 1)] += Lazy[Node] * (Mid - a + 1), Lazy[(Node << 1)] += Lazy[Node];
        A[((Node << 1) + 1)] += Lazy[Node] * (b - Mid), Lazy[((Node << 1) + 1)] += Lazy[Node];

        Lazy[Node] = 0;

        return;
    }

public:
    inline void Update (int Node, int a, int b, int ua, int ub, int Val)
    {
        if(ua <= a && b <= ub)
        {
            Lazy[Node] += Val;
            A[Node] += Val * (b - a + 1);

            return;
        }

        Go(Node, a, b);

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

        if(ua <= Mid)
            Update((Node << 1), a, Mid, ua, ub, Val);

        if(ub > Mid)
            Update(((Node << 1) + 1), Mid + 1, b, ua, ub, Val);

        A[Node] = A[(Node << 1)] + A[((Node << 1) + 1)];

        return;
    }

    inline int Query (int Node, int a, int b, int qa, int qb)
    {
        if(qa <= a && b <= qb)
            return A[Node];

        Go(Node, a, b);

        int Mid = ((a + b) >> 1);
        int p_Left = 0, p_Right = 0;

        if(qa <= Mid)
            p_Left = Query((Node << 1), a, Mid, qa, qb);

        if(qb > Mid)
            p_Right = Query(((Node << 1) + 1), Mid + 1, b, qa, qb);

        return (p_Left + p_Right);
    }
} AINT;

static inline void Initialize ()
{
    for(int i = 1; i <= N; ++i)
        if(A[i] > A[i - 1])
            AINT.Update(1, 1, V[0], A[i - 1] + 1, A[i], 1);

    return;
}

static inline void Solve ()
{
    Initialize();

    for(int i = 1; i <= Q; ++i)
        if(Queries[i].first == 1)
            cout << AINT.Query(1, 1, V[0], Queries[i].second.first, Queries[i].second.first) << '\n';
        else
        {
            int pos = Queries[i].second.first, X = Queries[i].second.second;

            if(A[pos] > A[pos - 1])
                AINT.Update(1, 1, V[0], A[pos - 1] + 1, A[pos], -1);

            if(A[pos] < A[pos + 1])
                AINT.Update(1, 1, V[0], A[pos] + 1, A[pos + 1], -1);

            A[pos] = X;

            if(A[pos] > A[pos - 1])
                AINT.Update(1, 1, V[0], A[pos - 1] + 1, A[pos], 1);

            if(A[pos] < A[pos + 1])
                AINT.Update(1, 1, V[0], A[pos] + 1, A[pos + 1], 1);

            A[pos] = X;
        }

    return;
}

int main()
{
    Read();

    Normalize();

    Solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 416 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 4 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
15 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 3 ms 640 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 19 ms 1792 KB Output is correct
5 Correct 39 ms 2936 KB Output is correct
6 Correct 64 ms 4728 KB Output is correct
7 Correct 104 ms 8824 KB Output is correct
8 Correct 132 ms 10104 KB Output is correct
9 Correct 238 ms 18168 KB Output is correct
10 Correct 221 ms 16372 KB Output is correct
11 Correct 291 ms 19052 KB Output is correct
12 Correct 319 ms 20088 KB Output is correct
13 Correct 324 ms 20088 KB Output is correct
14 Correct 315 ms 20000 KB Output is correct
15 Correct 312 ms 19960 KB Output is correct
16 Correct 327 ms 20216 KB Output is correct
17 Correct 322 ms 20252 KB Output is correct
18 Correct 341 ms 20344 KB Output is correct
19 Correct 322 ms 20216 KB Output is correct
20 Correct 330 ms 20600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 416 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 4 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
15 Correct 3 ms 512 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 3 ms 640 KB Output is correct
18 Correct 3 ms 640 KB Output is correct
19 Correct 19 ms 1792 KB Output is correct
20 Correct 39 ms 2936 KB Output is correct
21 Correct 64 ms 4728 KB Output is correct
22 Correct 104 ms 8824 KB Output is correct
23 Correct 132 ms 10104 KB Output is correct
24 Correct 238 ms 18168 KB Output is correct
25 Correct 221 ms 16372 KB Output is correct
26 Correct 291 ms 19052 KB Output is correct
27 Correct 319 ms 20088 KB Output is correct
28 Correct 324 ms 20088 KB Output is correct
29 Correct 315 ms 20000 KB Output is correct
30 Correct 312 ms 19960 KB Output is correct
31 Correct 327 ms 20216 KB Output is correct
32 Correct 322 ms 20252 KB Output is correct
33 Correct 341 ms 20344 KB Output is correct
34 Correct 322 ms 20216 KB Output is correct
35 Correct 330 ms 20600 KB Output is correct
36 Correct 2 ms 512 KB Output is correct
37 Correct 3 ms 512 KB Output is correct
38 Correct 4 ms 640 KB Output is correct
39 Correct 25 ms 1792 KB Output is correct
40 Correct 51 ms 3072 KB Output is correct
41 Correct 95 ms 5496 KB Output is correct
42 Correct 132 ms 8440 KB Output is correct
43 Correct 189 ms 9848 KB Output is correct
44 Correct 375 ms 18168 KB Output is correct
45 Correct 289 ms 16376 KB Output is correct
46 Correct 333 ms 17404 KB Output is correct
47 Correct 455 ms 19960 KB Output is correct
48 Correct 475 ms 20220 KB Output is correct
49 Correct 464 ms 20344 KB Output is correct
50 Correct 452 ms 19960 KB Output is correct
51 Correct 491 ms 20472 KB Output is correct
52 Incorrect 486 ms 20344 KB Output isn't correct
53 Halted 0 ms 0 KB -