답안 #335489

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
335489 2020-12-12T18:52:57 Z mihai145 Progression (NOI20_progression) C++14
100 / 100
2095 ms 78316 KB
#include <iostream>

using namespace std;

const int NMAX = 3e5;
int N, Q, d[NMAX + 2];

struct Node
{

    bool dummy;

    long long sum;

    int sz;

    int maxPref;
    long long valPref;
    int maxSuf;
    long long valSuf;
    int maxProgression;

    int lazyType;
    long long lazyVal;

    Node()
    {
        dummy = false;

        sum = 0LL;
        sz = 0LL;

        maxPref = 0;
        valPref = 0LL;
        maxSuf = 0;
        valSuf = 0LL;

        maxProgression = 0;

        lazyType = 0;
        lazyVal = 0LL;
    }
};

Node Merge(Node A, Node B)
{

    if(A.dummy == true)
        return B;

    if(B.dummy == true)
        return A;

    Node R;

    R.sz = A.sz + B.sz;
    R.sum = A.sum + B.sum;

    if(A.maxPref == A.sz)
    {
        if(A.valPref == B.valPref)
        {
            R.maxPref = A.sz + B.maxPref;
            R.valPref = A.valPref;
        }
        else
        {
            R.maxPref = A.sz;
            R.valPref = A.valPref;
        }
    }
    else
    {
        R.maxPref = A.maxPref;
        R.valPref = A.valPref;
    }

    if(B.maxSuf == B.sz)
    {
        if(B.valSuf == A.valSuf)
        {
            R.maxSuf = B.sz + A.maxSuf;
            R.valSuf = B.valSuf;
        }
        else
        {
            R.maxSuf = B.sz;
            R.valSuf = B.valSuf;
        }
    }
    else
    {
        R.maxSuf = B.maxSuf;
        R.valSuf = B.valSuf;
    }

    R.maxProgression = max(R.maxPref, R.maxSuf);
    R.maxProgression = max(R.maxProgression, A.maxProgression);
    R.maxProgression = max(R.maxProgression, B.maxProgression);

    if(A.valSuf == B.valPref)
    {
        R.maxProgression = max(R.maxProgression, A.maxSuf + B.maxPref);
    }

    return R;
}

struct SegTree
{

    Node v[4 * NMAX];

    void Build(int node, int st, int dr)
    {
        if(st == dr)
        {
            v[node].sz = dr - st + 1;
            v[node].sum = v[node].valPref = v[node].valSuf = d[st] - d[st - 1];
            v[node].maxPref = v[node].maxSuf = v[node].maxProgression = 1;

            return ;
        }

        int mid = (st + dr) >> 1;

        Build(2 * node, st, mid);
        Build(2 * node + 1, mid + 1, dr);

        v[node] = Merge(v[2 * node], v[2 * node + 1]);
    }

    /*
    void AddPoint(int node, int st, int dr, int pos, long long val) {

    }
    */

    /*
    void SetPoint(int node, int st, int dr, int pos, long long val) {

    }
    */

    void Propagate(int node, int st, int dr)
    {
        if(v[node].lazyType == 0)
            return ;

        int szNode = dr - st + 1;

        if(v[node].lazyType == 1)
        {

            v[node].sum += v[node].lazyVal * szNode;
            v[node].valPref += v[node].lazyVal;
            v[node].valSuf += v[node].lazyVal;

            if(szNode > 1)
            {

                int mid = (st + dr) >> 1;

                if(v[2 * node].lazyType == 2)
                {
                    Propagate(2 * node, st, mid);
                }

                v[2 * node].lazyType = 1;
                v[2 * node].lazyVal += v[node].lazyVal;

                if(v[2 * node + 1].lazyType == 2)
                {
                    Propagate(2 * node + 1, mid + 1, dr);
                }

                v[2 * node + 1].lazyType = 1;
                v[2 * node + 1].lazyVal += v[node].lazyVal;
            }

        }
        else
        {

            v[node].sum = v[node].lazyVal * szNode;
            v[node].valPref = v[node].valSuf = v[node].lazyVal;
            v[node].maxPref = v[node].maxSuf = v[node].maxProgression = szNode;

            if(szNode > 1)
            {
                v[2 * node].lazyType = 2;
                v[2 * node].lazyVal = v[node].lazyVal;

                v[2 * node + 1].lazyType = 2;
                v[2 * node + 1].lazyVal = v[node].lazyVal;
            }

        }

        v[node].lazyType = 0;
        v[node].lazyVal = 0;
    }

    void AddRange(int node, int st, int dr, int l, int r, long long val)
    {
        Propagate(node, st, dr);

        if(r < st || l > dr)
            return ;

        if(l <= st && dr <= r)
        {
            v[node].lazyType = 1;
            v[node].lazyVal = val;
            Propagate(node, st, dr);

            return ;
        }

        int mid = (st + dr) >> 1;

        AddRange(2 * node, st, mid, l, r, val);
        AddRange(2 * node + 1, mid + 1, dr, l, r, val);

        v[node] = Merge(v[2 * node], v[2 * node + 1]);
    }

    void SetRange(int node, int st, int dr, int l, int r, long long val)
    {
        Propagate(node, st, dr);

        if(r < st || l > dr)
            return ;

        if(l <= st && dr <= r)
        {
            v[node].lazyType = 2;
            v[node].lazyVal = val;
            Propagate(node, st, dr);

            return ;
        }

        int mid = (st + dr) >> 1;

        SetRange(2 * node, st, mid, l, r, val);
        SetRange(2 * node + 1, mid + 1, dr, l, r, val);

        v[node] = Merge(v[2 * node], v[2 * node + 1]);
    }

    Node Query(int node, int st, int dr, int l, int r)
    {
        Propagate(node, st, dr);

        if(r < st || l > dr)
        {
            Node _dummy;
            _dummy.dummy = true;
            return _dummy;
        }

        if(l <= st && dr <= r)
            return v[node];

        int mid = (st + dr) >> 1;

        Node a1 = Query(2 * node, st, mid, l, r);
        Node a2 = Query(2 * node + 1, mid + 1, dr, l, r);

        return Merge(a1, a2);
    }

};

SegTree aint;

int main()
{
    // The following line disables syncing between cin/scanf and cout/printf.
    // It makes input faster, but you must not use functions from <cstdio> (e.g. scanf/printf) directly.
    // This line must be executed before any use of cin/cout.
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    // Your code goes here ...
    // (You can now read input and write output normally using cin/cout.)

    cin >> N >> Q;

    for(int i = 1; i <= N; i++)
        cin >> d[i];

    aint.Build(1, 1, N);

    for(int i = 1; i <= Q; i++)
    {
        int typ;
        cin >> typ;

        if(typ == 1)
        {
            ///patch

            int L, R, S, C;
            cin >> L >> R >> S >> C;

            long long rr = 0;
            if(R + 1 <= N)
            {
                rr = aint.Query(1, 1, N, 1, R + 1).sum;
            }

            if(L + 1 <= R)
                aint.AddRange(1, 1, N, L + 1, R, C);

            aint.AddRange(1, 1, N, L, L, S);

            if(R + 1 <= N)
                aint.SetRange(1, 1, N, R + 1, R + 1, rr - aint.Query(1, 1, N, 1, R).sum);
        }
        else if(typ == 2)
        {
            ///rewrite

            int L, R, S, C;
            cin >> L >> R >> S >> C;

            long long rr = 0;
            if(R + 1 <= N)
            {
                rr = aint.Query(1, 1, N, 1, R + 1).sum;
            }

            long long l = 0;
            if(L - 1 >= 1) l = aint.Query(1, 1, N, 1, L - 1).sum;
            aint.SetRange(1, 1, N, L, L, S - l);

            if(L + 1 <= R)
                aint.SetRange(1, 1, N, L + 1, R, C);

            if(R + 1 <= N)
            {
                aint.SetRange(1, 1, N, R + 1, R + 1, rr - aint.Query(1, 1, N, 1, R).sum);
            }
        }
        else
        {
            ///evaluate

            int L, R;
            cin >> L >> R;

            if(L == R)
                cout << 1 << '\n';
            else
            {
                cout << aint.Query(1, 1, N, L + 1, R).maxProgression + 1 << '\n';
            }
        }
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 592 ms 77548 KB Output is correct
2 Correct 248 ms 75756 KB Output is correct
3 Correct 245 ms 75756 KB Output is correct
4 Correct 250 ms 75756 KB Output is correct
5 Correct 245 ms 76140 KB Output is correct
6 Correct 241 ms 75884 KB Output is correct
7 Correct 243 ms 75756 KB Output is correct
8 Correct 40 ms 75520 KB Output is correct
9 Correct 40 ms 75500 KB Output is correct
10 Correct 39 ms 75500 KB Output is correct
11 Correct 588 ms 77420 KB Output is correct
12 Correct 586 ms 77292 KB Output is correct
13 Correct 597 ms 77804 KB Output is correct
14 Correct 587 ms 77676 KB Output is correct
15 Correct 585 ms 77676 KB Output is correct
16 Correct 591 ms 77292 KB Output is correct
17 Correct 589 ms 77292 KB Output is correct
18 Correct 592 ms 77292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 75500 KB Output is correct
2 Correct 40 ms 75500 KB Output is correct
3 Correct 41 ms 75500 KB Output is correct
4 Correct 48 ms 75500 KB Output is correct
5 Correct 39 ms 75444 KB Output is correct
6 Correct 39 ms 75500 KB Output is correct
7 Correct 40 ms 75500 KB Output is correct
8 Correct 44 ms 75500 KB Output is correct
9 Correct 41 ms 75500 KB Output is correct
10 Correct 44 ms 75500 KB Output is correct
11 Correct 42 ms 75500 KB Output is correct
12 Correct 42 ms 75500 KB Output is correct
13 Correct 41 ms 75500 KB Output is correct
14 Correct 41 ms 75500 KB Output is correct
15 Correct 42 ms 75500 KB Output is correct
16 Correct 42 ms 75500 KB Output is correct
17 Correct 42 ms 75500 KB Output is correct
18 Correct 44 ms 75500 KB Output is correct
19 Correct 41 ms 75500 KB Output is correct
20 Correct 40 ms 75520 KB Output is correct
21 Correct 40 ms 75500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 580 ms 77804 KB Output is correct
2 Correct 177 ms 76140 KB Output is correct
3 Correct 172 ms 76140 KB Output is correct
4 Correct 148 ms 76268 KB Output is correct
5 Correct 177 ms 76140 KB Output is correct
6 Correct 177 ms 76268 KB Output is correct
7 Correct 181 ms 76140 KB Output is correct
8 Correct 39 ms 75500 KB Output is correct
9 Correct 40 ms 75500 KB Output is correct
10 Correct 40 ms 75500 KB Output is correct
11 Correct 561 ms 77548 KB Output is correct
12 Correct 512 ms 77804 KB Output is correct
13 Correct 555 ms 77600 KB Output is correct
14 Correct 555 ms 77548 KB Output is correct
15 Correct 507 ms 77676 KB Output is correct
16 Correct 563 ms 78060 KB Output is correct
17 Correct 558 ms 78060 KB Output is correct
18 Correct 558 ms 78188 KB Output is correct
19 Correct 490 ms 77420 KB Output is correct
20 Correct 490 ms 77420 KB Output is correct
21 Correct 490 ms 77548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1172 ms 77064 KB Output is correct
2 Correct 305 ms 75724 KB Output is correct
3 Correct 310 ms 75628 KB Output is correct
4 Correct 307 ms 75756 KB Output is correct
5 Correct 315 ms 75756 KB Output is correct
6 Correct 316 ms 75756 KB Output is correct
7 Correct 314 ms 75628 KB Output is correct
8 Correct 41 ms 75500 KB Output is correct
9 Correct 40 ms 75500 KB Output is correct
10 Correct 40 ms 75500 KB Output is correct
11 Correct 1184 ms 76976 KB Output is correct
12 Correct 1172 ms 77164 KB Output is correct
13 Correct 1186 ms 77164 KB Output is correct
14 Correct 1188 ms 77292 KB Output is correct
15 Correct 1135 ms 77044 KB Output is correct
16 Correct 1203 ms 77100 KB Output is correct
17 Correct 1203 ms 77292 KB Output is correct
18 Correct 1204 ms 77420 KB Output is correct
19 Correct 1165 ms 77116 KB Output is correct
20 Correct 1177 ms 77164 KB Output is correct
21 Correct 1168 ms 77164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 580 ms 77804 KB Output is correct
2 Correct 177 ms 76140 KB Output is correct
3 Correct 172 ms 76140 KB Output is correct
4 Correct 148 ms 76268 KB Output is correct
5 Correct 177 ms 76140 KB Output is correct
6 Correct 177 ms 76268 KB Output is correct
7 Correct 181 ms 76140 KB Output is correct
8 Correct 39 ms 75500 KB Output is correct
9 Correct 40 ms 75500 KB Output is correct
10 Correct 40 ms 75500 KB Output is correct
11 Correct 561 ms 77548 KB Output is correct
12 Correct 512 ms 77804 KB Output is correct
13 Correct 555 ms 77600 KB Output is correct
14 Correct 555 ms 77548 KB Output is correct
15 Correct 507 ms 77676 KB Output is correct
16 Correct 563 ms 78060 KB Output is correct
17 Correct 558 ms 78060 KB Output is correct
18 Correct 558 ms 78188 KB Output is correct
19 Correct 490 ms 77420 KB Output is correct
20 Correct 490 ms 77420 KB Output is correct
21 Correct 490 ms 77548 KB Output is correct
22 Correct 1506 ms 77312 KB Output is correct
23 Correct 309 ms 75884 KB Output is correct
24 Correct 302 ms 75884 KB Output is correct
25 Correct 304 ms 75884 KB Output is correct
26 Correct 304 ms 76012 KB Output is correct
27 Correct 313 ms 75884 KB Output is correct
28 Correct 307 ms 75756 KB Output is correct
29 Correct 40 ms 75500 KB Output is correct
30 Correct 40 ms 75500 KB Output is correct
31 Correct 40 ms 75500 KB Output is correct
32 Correct 1498 ms 77164 KB Output is correct
33 Correct 1409 ms 77036 KB Output is correct
34 Correct 1484 ms 77164 KB Output is correct
35 Correct 1482 ms 77036 KB Output is correct
36 Correct 1139 ms 77292 KB Output is correct
37 Correct 1130 ms 77548 KB Output is correct
38 Correct 1132 ms 77292 KB Output is correct
39 Correct 1460 ms 77036 KB Output is correct
40 Correct 1543 ms 77164 KB Output is correct
41 Correct 1522 ms 77164 KB Output is correct
42 Correct 1511 ms 77292 KB Output is correct
43 Correct 1432 ms 77056 KB Output is correct
44 Correct 1435 ms 77036 KB Output is correct
45 Correct 1430 ms 77164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 592 ms 77548 KB Output is correct
2 Correct 248 ms 75756 KB Output is correct
3 Correct 245 ms 75756 KB Output is correct
4 Correct 250 ms 75756 KB Output is correct
5 Correct 245 ms 76140 KB Output is correct
6 Correct 241 ms 75884 KB Output is correct
7 Correct 243 ms 75756 KB Output is correct
8 Correct 40 ms 75520 KB Output is correct
9 Correct 40 ms 75500 KB Output is correct
10 Correct 39 ms 75500 KB Output is correct
11 Correct 588 ms 77420 KB Output is correct
12 Correct 586 ms 77292 KB Output is correct
13 Correct 597 ms 77804 KB Output is correct
14 Correct 587 ms 77676 KB Output is correct
15 Correct 585 ms 77676 KB Output is correct
16 Correct 591 ms 77292 KB Output is correct
17 Correct 589 ms 77292 KB Output is correct
18 Correct 592 ms 77292 KB Output is correct
19 Correct 42 ms 75500 KB Output is correct
20 Correct 40 ms 75500 KB Output is correct
21 Correct 41 ms 75500 KB Output is correct
22 Correct 48 ms 75500 KB Output is correct
23 Correct 39 ms 75444 KB Output is correct
24 Correct 39 ms 75500 KB Output is correct
25 Correct 40 ms 75500 KB Output is correct
26 Correct 44 ms 75500 KB Output is correct
27 Correct 41 ms 75500 KB Output is correct
28 Correct 44 ms 75500 KB Output is correct
29 Correct 42 ms 75500 KB Output is correct
30 Correct 42 ms 75500 KB Output is correct
31 Correct 41 ms 75500 KB Output is correct
32 Correct 41 ms 75500 KB Output is correct
33 Correct 42 ms 75500 KB Output is correct
34 Correct 42 ms 75500 KB Output is correct
35 Correct 42 ms 75500 KB Output is correct
36 Correct 44 ms 75500 KB Output is correct
37 Correct 41 ms 75500 KB Output is correct
38 Correct 40 ms 75520 KB Output is correct
39 Correct 40 ms 75500 KB Output is correct
40 Correct 580 ms 77804 KB Output is correct
41 Correct 177 ms 76140 KB Output is correct
42 Correct 172 ms 76140 KB Output is correct
43 Correct 148 ms 76268 KB Output is correct
44 Correct 177 ms 76140 KB Output is correct
45 Correct 177 ms 76268 KB Output is correct
46 Correct 181 ms 76140 KB Output is correct
47 Correct 39 ms 75500 KB Output is correct
48 Correct 40 ms 75500 KB Output is correct
49 Correct 40 ms 75500 KB Output is correct
50 Correct 561 ms 77548 KB Output is correct
51 Correct 512 ms 77804 KB Output is correct
52 Correct 555 ms 77600 KB Output is correct
53 Correct 555 ms 77548 KB Output is correct
54 Correct 507 ms 77676 KB Output is correct
55 Correct 563 ms 78060 KB Output is correct
56 Correct 558 ms 78060 KB Output is correct
57 Correct 558 ms 78188 KB Output is correct
58 Correct 490 ms 77420 KB Output is correct
59 Correct 490 ms 77420 KB Output is correct
60 Correct 490 ms 77548 KB Output is correct
61 Correct 1172 ms 77064 KB Output is correct
62 Correct 305 ms 75724 KB Output is correct
63 Correct 310 ms 75628 KB Output is correct
64 Correct 307 ms 75756 KB Output is correct
65 Correct 315 ms 75756 KB Output is correct
66 Correct 316 ms 75756 KB Output is correct
67 Correct 314 ms 75628 KB Output is correct
68 Correct 41 ms 75500 KB Output is correct
69 Correct 40 ms 75500 KB Output is correct
70 Correct 40 ms 75500 KB Output is correct
71 Correct 1184 ms 76976 KB Output is correct
72 Correct 1172 ms 77164 KB Output is correct
73 Correct 1186 ms 77164 KB Output is correct
74 Correct 1188 ms 77292 KB Output is correct
75 Correct 1135 ms 77044 KB Output is correct
76 Correct 1203 ms 77100 KB Output is correct
77 Correct 1203 ms 77292 KB Output is correct
78 Correct 1204 ms 77420 KB Output is correct
79 Correct 1165 ms 77116 KB Output is correct
80 Correct 1177 ms 77164 KB Output is correct
81 Correct 1168 ms 77164 KB Output is correct
82 Correct 1506 ms 77312 KB Output is correct
83 Correct 309 ms 75884 KB Output is correct
84 Correct 302 ms 75884 KB Output is correct
85 Correct 304 ms 75884 KB Output is correct
86 Correct 304 ms 76012 KB Output is correct
87 Correct 313 ms 75884 KB Output is correct
88 Correct 307 ms 75756 KB Output is correct
89 Correct 40 ms 75500 KB Output is correct
90 Correct 40 ms 75500 KB Output is correct
91 Correct 40 ms 75500 KB Output is correct
92 Correct 1498 ms 77164 KB Output is correct
93 Correct 1409 ms 77036 KB Output is correct
94 Correct 1484 ms 77164 KB Output is correct
95 Correct 1482 ms 77036 KB Output is correct
96 Correct 1139 ms 77292 KB Output is correct
97 Correct 1130 ms 77548 KB Output is correct
98 Correct 1132 ms 77292 KB Output is correct
99 Correct 1460 ms 77036 KB Output is correct
100 Correct 1543 ms 77164 KB Output is correct
101 Correct 1522 ms 77164 KB Output is correct
102 Correct 1511 ms 77292 KB Output is correct
103 Correct 1432 ms 77056 KB Output is correct
104 Correct 1435 ms 77036 KB Output is correct
105 Correct 1430 ms 77164 KB Output is correct
106 Correct 2000 ms 77536 KB Output is correct
107 Correct 381 ms 75756 KB Output is correct
108 Correct 377 ms 75756 KB Output is correct
109 Correct 372 ms 75756 KB Output is correct
110 Correct 40 ms 75500 KB Output is correct
111 Correct 40 ms 75500 KB Output is correct
112 Correct 39 ms 75500 KB Output is correct
113 Correct 1519 ms 77292 KB Output is correct
114 Correct 1521 ms 77288 KB Output is correct
115 Correct 1521 ms 77420 KB Output is correct
116 Correct 1431 ms 77292 KB Output is correct
117 Correct 2004 ms 77484 KB Output is correct
118 Correct 1428 ms 77184 KB Output is correct
119 Correct 1439 ms 77164 KB Output is correct
120 Correct 558 ms 78188 KB Output is correct
121 Correct 563 ms 78316 KB Output is correct
122 Correct 561 ms 78188 KB Output is correct
123 Correct 495 ms 77420 KB Output is correct
124 Correct 490 ms 77420 KB Output is correct
125 Correct 498 ms 77420 KB Output is correct
126 Correct 1981 ms 77292 KB Output is correct
127 Correct 2001 ms 77292 KB Output is correct
128 Correct 2017 ms 77560 KB Output is correct
129 Correct 1972 ms 77292 KB Output is correct
130 Correct 1309 ms 77932 KB Output is correct
131 Correct 1317 ms 77932 KB Output is correct
132 Correct 1339 ms 78060 KB Output is correct
133 Correct 1994 ms 77292 KB Output is correct
134 Correct 2029 ms 77540 KB Output is correct
135 Correct 2095 ms 77292 KB Output is correct
136 Correct 375 ms 75756 KB Output is correct
137 Correct 372 ms 75756 KB Output is correct
138 Correct 373 ms 75756 KB Output is correct