답안 #1088971

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1088971 2024-09-15T16:33:58 Z damwuan Progression (NOI20_progression) C++14
100 / 100
805 ms 86900 KB
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define for1(i,x,n) for(int i=x;i<=n;i++)
#define for2(i,x,n) for(int i=x;i>=n;i--)
#define int ll

typedef long long ll;
typedef pair<int,int> pii;

const ll maxn=3e5+69;
const ll mod = 1e9+7;
const ll inf = 1e18;

int n,q,d[maxn];

namespace sub2
{
    void solve()
    {
        for1(i,1,q)
        {
            int t,l,r; cin >>t>> l>> r;
            if(t==3)
            {
                int cnt=2,res=1;
                for1(i,l+1,r)
                {
                    if (i!=l+1 && d[i]-d[i-1]==d[i-1]-d[i-2])
                    {
                        cnt++;
                    }
                    else cnt=2;
                    res=max(res,cnt);
                }
                cout << res<<'\n';
            }
            else if (t==1)
            {
                int s,c;  cin >> s>>c;
                for1(i,l,r) d[i]+=s+c*(i-l);
            }
            else
            {
                int s,c;  cin >> s>>c;
                for1(i,l,r) d[i]=s+c*(i-l);
            }
        }
    }
}

namespace sub6
{
    struct Node
    {
        int al,ar,pre,suf,res,sum,sz,lazy_add,lazy_set;
        void dbg()
        {
            cerr<< al<<' '<<ar<<' '<<pre<<' '<<suf<<' '<<res<<' '<<sum<<' '<<sz<<'\n';
        }
    }st[maxn*4];
    Node Merge(const Node& l, const Node& r)
    {
        if (l.res==-1) return r;
        if (r.res==-1) return l;
        Node c;
        c.al=l.al;
        c.ar=r.ar;
        c.pre=l.pre;
        if (l.pre==l.sz && l.ar==r.al) c.pre=l.res+r.pre;
        c.suf=r.suf;
        if (r.suf==r.sz && l.ar==r.al) c.suf=r.res+l.suf;
        c.res=max(l.res,r.res);
        if (l.ar==r.al) c.res=max(c.res,l.suf+r.pre);
        c.sum=l.sum+r.sum;
        c.lazy_add=0;
        c.lazy_set=-inf;
        c.sz=l.sz+r.sz;
        return c;
    }
    void push(int id,int l,int r)
    {
        int mid=l+r>>1;
        if (st[id].lazy_set!=-inf)
        {
            int& val=st[id].lazy_set;
            st[id*2].al=val;
            st[id*2].ar=val;
            st[id*2].pre=st[id*2].suf=st[id*2].res=mid-l+1;
            st[id*2].sum=(mid-l+1)*val;
            st[id*2].lazy_add=0;
            st[id*2].lazy_set=val;

            st[id*2+1].al=val;
            st[id*2+1].ar=val;
            st[id*2+1].pre=st[id*2+1].suf=st[id*2+1].res=r-mid;
            st[id*2+1].sum=(r-mid)*val;
            st[id*2+1].lazy_add=0;
            st[id*2+1].lazy_set=val;
            val=-inf;
        }
        if (st[id].lazy_add)
        {
            int& val=st[id].lazy_add;
            st[id*2].al+=val;
            st[id*2].ar+=val;
            st[id*2].sum+=(mid-l+1)*val;
            st[id*2].lazy_add+=val;

            st[id*2+1].al+=val;
            st[id*2+1].ar+=val;
            st[id*2+1].sum+=(r-mid)*val;
            st[id*2+1].lazy_add+=val;
            val=0;
        }
    }
    void Add(int u,int v,int val,int id=1,int l=1,int r=n)
    {
        if (u>r || v<l) return;
        if (u<=l && r<=v)
        {
            st[id].al+=val;
            st[id].ar+=val;
            st[id].sum+=(r-l+1)*val;
            st[id].lazy_add+=val;
            return;
        }
        int mid=l+r>>1;
        if (st[id].lazy_add || st[id].lazy_set!=inf) push(id,l,r);
        Add(u,v,val,id*2,l,mid);
        Add(u,v,val,id*2+1,mid+1,r);
        st[id]=Merge(st[id*2],st[id*2+1]);
    }
    void Set(int u,int v,int val,int id=1,int l=1,int r=n)
    {
        if (u>r || v<l) return;
//        cerr<<"skrt "<< u<<' '<<v<<" "<<id<<' '<< st[2].lazy_set<<'\n';
        if (u<=l && r<=v)
        {
            if (l==r) st[id].sz=1;
            st[id].al=val;
            st[id].ar=val;
//            cerr<<id<<' '<< l<<' '<<r<<' '<<st[4].lazy_set<<'\n';
            st[id].pre=st[id].suf=st[id].res=r-l+1;
            st[id].sum=(r-l+1)*val;
            st[id].lazy_add=0;
            st[id].lazy_set=val;
            return;
        }
        int mid=l+r>>1;
        if (st[id].lazy_add || st[id].lazy_set!=-inf) push(id,l,r);
        Set(u,v,val,id*2,l,mid);
        Set(u,v,val,id*2+1,mid+1,r);
//        cerr<<"skrt2 "<< u<<' '<<v<<" "<<id<<' '<< st[2].lazy_set<<'\n';
        st[id]=Merge(st[id*2],st[id*2+1]);
    }
    Node Get(int u,int v,int id=1,int l=1,int r=n)
    {
//        cerr<<id<<' '<<l<<' '<<r<<'\n';
        if (u>r || v<l) return {-1,-1,-1,-1,-1,-1,-1,-1,-1};
        if (u<=l && r<=v) return st[id];
        int mid=l+r>>1;
        if (st[id].lazy_add || st[id].lazy_set!=-inf) push(id,l,r);
        return Merge(Get(u,v,id*2,l,mid),Get(u,v,id*2+1,mid+1,r));
    }
    void solve()
    {
        for1(i,1,n)
        {
//        cerr<< st[2].lazy_set<<'\n';
            Set(i,i,d[i]-d[i-1]);
//            cerr << i<<' '<< d[i]-d[i-1]<<'\n';
        }
//        st[5].dbg();
//        st[3].dbg();
        for1(i,1,q)
        {
//            cerr<< "ok?\n";
            int t,l,r; cin >>t>> l>> r;
            if(t==3)
            {
//                st[3].dbg();
                if (l==r) cout <<"1\n";
                else cout << Get(l+1,r).res+1<<'\n';
            }
            else if (t==1)
            {
                int s,c;  cin >> s>>c;
                if (l<r) Add(l+1,r,c);
                Add(l,l,s);
                if (r<n) Add(r+1,r+1,-s-c*(r-l));
            }
            else
            {
                int s,c;  cin >> s>>c;
                int x=0;
                if (l>1) x=Get(1,l-1).sum;
                int y=Get(1,r+1).sum;
                if (l<r) Set(l+1,r,c);
//                cerr<< "y = "<<
//                if (l<r)cerr<< "set "<<l+1<<' '<<r<<' '<<c<<'\n';
                Set(l,l,s-x);
//                cerr<< "set "<<l<<' '<<l<<' '<<s-x<<'\n';
                if (r<n) Set(r+1,r+1,y-s-c*(r-l));
//                if (r<n)cerr<< "set "<<r+1<<' '<<r+1<<' '<<y-s-c*(r-l)<<'\n';
            }
        }
    }
}

void sol()
{
    cin >> n>>q;
    for1(i,1,n)
    {
        cin >> d[i];
    }
    sub6::solve();
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #define task "NOI20_progression"
    if (fopen(task".inp","r"))
    {
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    int t=1;//cin >> t;
    while (t--)
    {
        sol();
    }
}
/*
6 10
-1 -5 4 -5 -5 -5
2 1 3 1 4
1 1 2 -1 -5
2 3 3 -5 -3
2 2 6 0 0
2 5 5 -5 -2
3 3 6
1 4 6 3 4
3 1 3
2 2 6 -5 -3
2 5 5 -3 -3

*/

Compilation message

Progression.cpp: In function 'void sub6::push(ll, ll, ll)':
Progression.cpp:86:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   86 |         int mid=l+r>>1;
      |                 ~^~
Progression.cpp: In function 'void sub6::Add(ll, ll, ll, ll, ll, ll)':
Progression.cpp:131:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  131 |         int mid=l+r>>1;
      |                 ~^~
Progression.cpp: In function 'void sub6::Set(ll, ll, ll, ll, ll, ll)':
Progression.cpp:153:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  153 |         int mid=l+r>>1;
      |                 ~^~
Progression.cpp: In function 'sub6::Node sub6::Get(ll, ll, ll, ll, ll)':
Progression.cpp:165:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  165 |         int mid=l+r>>1;
      |                 ~^~
Progression.cpp: In function 'int32_t main()':
Progression.cpp:231:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  231 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Progression.cpp:232:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  232 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 346 ms 82708 KB Output is correct
2 Correct 89 ms 3668 KB Output is correct
3 Correct 97 ms 3668 KB Output is correct
4 Correct 91 ms 3668 KB Output is correct
5 Correct 89 ms 3664 KB Output is correct
6 Correct 101 ms 3528 KB Output is correct
7 Correct 86 ms 3648 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 368 ms 85164 KB Output is correct
12 Correct 350 ms 85092 KB Output is correct
13 Correct 358 ms 85408 KB Output is correct
14 Correct 350 ms 85584 KB Output is correct
15 Correct 351 ms 85396 KB Output is correct
16 Correct 357 ms 85072 KB Output is correct
17 Correct 353 ms 85048 KB Output is correct
18 Correct 355 ms 85072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 2 ms 604 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 304 ms 81244 KB Output is correct
2 Correct 68 ms 3104 KB Output is correct
3 Correct 57 ms 3132 KB Output is correct
4 Correct 45 ms 3132 KB Output is correct
5 Correct 56 ms 3284 KB Output is correct
6 Correct 58 ms 3244 KB Output is correct
7 Correct 70 ms 3148 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 340 ms 82348 KB Output is correct
12 Correct 299 ms 83544 KB Output is correct
13 Correct 319 ms 82180 KB Output is correct
14 Correct 326 ms 82256 KB Output is correct
15 Correct 288 ms 83540 KB Output is correct
16 Correct 310 ms 84304 KB Output is correct
17 Correct 331 ms 84160 KB Output is correct
18 Correct 337 ms 84304 KB Output is correct
19 Correct 314 ms 83536 KB Output is correct
20 Correct 329 ms 83640 KB Output is correct
21 Correct 312 ms 83540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 551 ms 84048 KB Output is correct
2 Correct 94 ms 3664 KB Output is correct
3 Correct 95 ms 3688 KB Output is correct
4 Correct 94 ms 3584 KB Output is correct
5 Correct 97 ms 3664 KB Output is correct
6 Correct 100 ms 3672 KB Output is correct
7 Correct 97 ms 3664 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 541 ms 83188 KB Output is correct
12 Correct 607 ms 86352 KB Output is correct
13 Correct 562 ms 83120 KB Output is correct
14 Correct 567 ms 83152 KB Output is correct
15 Correct 536 ms 86264 KB Output is correct
16 Correct 540 ms 86376 KB Output is correct
17 Correct 552 ms 86360 KB Output is correct
18 Correct 591 ms 86612 KB Output is correct
19 Correct 546 ms 86356 KB Output is correct
20 Correct 543 ms 86344 KB Output is correct
21 Correct 546 ms 86352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 304 ms 81244 KB Output is correct
2 Correct 68 ms 3104 KB Output is correct
3 Correct 57 ms 3132 KB Output is correct
4 Correct 45 ms 3132 KB Output is correct
5 Correct 56 ms 3284 KB Output is correct
6 Correct 58 ms 3244 KB Output is correct
7 Correct 70 ms 3148 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 340 ms 82348 KB Output is correct
12 Correct 299 ms 83544 KB Output is correct
13 Correct 319 ms 82180 KB Output is correct
14 Correct 326 ms 82256 KB Output is correct
15 Correct 288 ms 83540 KB Output is correct
16 Correct 310 ms 84304 KB Output is correct
17 Correct 331 ms 84160 KB Output is correct
18 Correct 337 ms 84304 KB Output is correct
19 Correct 314 ms 83536 KB Output is correct
20 Correct 329 ms 83640 KB Output is correct
21 Correct 312 ms 83540 KB Output is correct
22 Correct 626 ms 85948 KB Output is correct
23 Correct 97 ms 3556 KB Output is correct
24 Correct 102 ms 3664 KB Output is correct
25 Correct 105 ms 3680 KB Output is correct
26 Correct 98 ms 3728 KB Output is correct
27 Correct 97 ms 3668 KB Output is correct
28 Correct 112 ms 3704 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 652 ms 83168 KB Output is correct
33 Correct 621 ms 85720 KB Output is correct
34 Correct 661 ms 83036 KB Output is correct
35 Correct 652 ms 83028 KB Output is correct
36 Correct 569 ms 83024 KB Output is correct
37 Correct 569 ms 82984 KB Output is correct
38 Correct 567 ms 82840 KB Output is correct
39 Correct 676 ms 85864 KB Output is correct
40 Correct 661 ms 85992 KB Output is correct
41 Correct 669 ms 85880 KB Output is correct
42 Correct 666 ms 85780 KB Output is correct
43 Correct 688 ms 85840 KB Output is correct
44 Correct 663 ms 85936 KB Output is correct
45 Correct 636 ms 85844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 346 ms 82708 KB Output is correct
2 Correct 89 ms 3668 KB Output is correct
3 Correct 97 ms 3668 KB Output is correct
4 Correct 91 ms 3668 KB Output is correct
5 Correct 89 ms 3664 KB Output is correct
6 Correct 101 ms 3528 KB Output is correct
7 Correct 86 ms 3648 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 368 ms 85164 KB Output is correct
12 Correct 350 ms 85092 KB Output is correct
13 Correct 358 ms 85408 KB Output is correct
14 Correct 350 ms 85584 KB Output is correct
15 Correct 351 ms 85396 KB Output is correct
16 Correct 357 ms 85072 KB Output is correct
17 Correct 353 ms 85048 KB Output is correct
18 Correct 355 ms 85072 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 600 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 2 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 304 ms 81244 KB Output is correct
41 Correct 68 ms 3104 KB Output is correct
42 Correct 57 ms 3132 KB Output is correct
43 Correct 45 ms 3132 KB Output is correct
44 Correct 56 ms 3284 KB Output is correct
45 Correct 58 ms 3244 KB Output is correct
46 Correct 70 ms 3148 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 340 ms 82348 KB Output is correct
51 Correct 299 ms 83544 KB Output is correct
52 Correct 319 ms 82180 KB Output is correct
53 Correct 326 ms 82256 KB Output is correct
54 Correct 288 ms 83540 KB Output is correct
55 Correct 310 ms 84304 KB Output is correct
56 Correct 331 ms 84160 KB Output is correct
57 Correct 337 ms 84304 KB Output is correct
58 Correct 314 ms 83536 KB Output is correct
59 Correct 329 ms 83640 KB Output is correct
60 Correct 312 ms 83540 KB Output is correct
61 Correct 551 ms 84048 KB Output is correct
62 Correct 94 ms 3664 KB Output is correct
63 Correct 95 ms 3688 KB Output is correct
64 Correct 94 ms 3584 KB Output is correct
65 Correct 97 ms 3664 KB Output is correct
66 Correct 100 ms 3672 KB Output is correct
67 Correct 97 ms 3664 KB Output is correct
68 Correct 1 ms 348 KB Output is correct
69 Correct 0 ms 348 KB Output is correct
70 Correct 1 ms 348 KB Output is correct
71 Correct 541 ms 83188 KB Output is correct
72 Correct 607 ms 86352 KB Output is correct
73 Correct 562 ms 83120 KB Output is correct
74 Correct 567 ms 83152 KB Output is correct
75 Correct 536 ms 86264 KB Output is correct
76 Correct 540 ms 86376 KB Output is correct
77 Correct 552 ms 86360 KB Output is correct
78 Correct 591 ms 86612 KB Output is correct
79 Correct 546 ms 86356 KB Output is correct
80 Correct 543 ms 86344 KB Output is correct
81 Correct 546 ms 86352 KB Output is correct
82 Correct 626 ms 85948 KB Output is correct
83 Correct 97 ms 3556 KB Output is correct
84 Correct 102 ms 3664 KB Output is correct
85 Correct 105 ms 3680 KB Output is correct
86 Correct 98 ms 3728 KB Output is correct
87 Correct 97 ms 3668 KB Output is correct
88 Correct 112 ms 3704 KB Output is correct
89 Correct 1 ms 348 KB Output is correct
90 Correct 1 ms 348 KB Output is correct
91 Correct 1 ms 348 KB Output is correct
92 Correct 652 ms 83168 KB Output is correct
93 Correct 621 ms 85720 KB Output is correct
94 Correct 661 ms 83036 KB Output is correct
95 Correct 652 ms 83028 KB Output is correct
96 Correct 569 ms 83024 KB Output is correct
97 Correct 569 ms 82984 KB Output is correct
98 Correct 567 ms 82840 KB Output is correct
99 Correct 676 ms 85864 KB Output is correct
100 Correct 661 ms 85992 KB Output is correct
101 Correct 669 ms 85880 KB Output is correct
102 Correct 666 ms 85780 KB Output is correct
103 Correct 688 ms 85840 KB Output is correct
104 Correct 663 ms 85936 KB Output is correct
105 Correct 636 ms 85844 KB Output is correct
106 Correct 805 ms 86900 KB Output is correct
107 Correct 139 ms 3688 KB Output is correct
108 Correct 144 ms 3668 KB Output is correct
109 Correct 130 ms 3888 KB Output is correct
110 Correct 1 ms 348 KB Output is correct
111 Correct 1 ms 348 KB Output is correct
112 Correct 1 ms 348 KB Output is correct
113 Correct 677 ms 85836 KB Output is correct
114 Correct 700 ms 86100 KB Output is correct
115 Correct 656 ms 86008 KB Output is correct
116 Correct 662 ms 85844 KB Output is correct
117 Correct 798 ms 86868 KB Output is correct
118 Correct 650 ms 85912 KB Output is correct
119 Correct 637 ms 85844 KB Output is correct
120 Correct 318 ms 84820 KB Output is correct
121 Correct 318 ms 84304 KB Output is correct
122 Correct 320 ms 84560 KB Output is correct
123 Correct 308 ms 83536 KB Output is correct
124 Correct 320 ms 83488 KB Output is correct
125 Correct 307 ms 83580 KB Output is correct
126 Correct 750 ms 83504 KB Output is correct
127 Correct 793 ms 83536 KB Output is correct
128 Correct 766 ms 86840 KB Output is correct
129 Correct 746 ms 83540 KB Output is correct
130 Correct 540 ms 83540 KB Output is correct
131 Correct 538 ms 83672 KB Output is correct
132 Correct 548 ms 83536 KB Output is correct
133 Correct 754 ms 86868 KB Output is correct
134 Correct 762 ms 86868 KB Output is correct
135 Correct 753 ms 86864 KB Output is correct
136 Correct 128 ms 3668 KB Output is correct
137 Correct 130 ms 3668 KB Output is correct
138 Correct 133 ms 3920 KB Output is correct