제출 #1314960

#제출 시각아이디문제언어결과실행 시간메모리
1314960cubedProgression (NOI20_progression)C++20
0 / 100
190 ms62460 KiB
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define f first
// #define s second
#define pb(x) push_back(x)
#define int long long
 
const int MOD = 1e9+7;
const int inf = 1e9;
const int INF = 1e18+20;
const int LOG = 25;

int n;
vector<int> a;

struct Node {
    int max_len;   // Longest constant subarray in this range
    int pref_len;  // Length of constant subarray starting from the left
    int suff_len;  // Length of constant subarray ending at the right
    int size;      // Total elements in this range
    long long pref_val; // Value at the left boundary
    long long suff_val; // Value at the right boundary

    // Constructor for a "null" or empty node
    Node() : max_len(0), pref_len(0), suff_len(0), size(0), pref_val(-1e18), suff_val(-1e18) {}

    // Constructor for a leaf node (single element)
    Node(long long val) : max_len(1), pref_len(1), suff_len(1), size(1), pref_val(val), suff_val(val) {}
};

// Function to merge two Segment Tree nodes
Node merge(Node L, Node R) {
    if (L.size == 0) return R;
    if (R.size == 0) return L;

    Node res;
    res.size = L.size + R.size;
    res.pref_val = L.pref_val;
    res.suff_val = R.suff_val;

    // 1. Calculate Prefix Length
    if (L.pref_len == L.size && L.pref_val == R.pref_val) {
        res.pref_len = L.size + R.pref_len;
    } else {
        res.pref_len = L.pref_len;
    }

    // 2. Calculate Suffix Length
    if (R.suff_len == R.size && R.suff_val == L.suff_val) {
        res.suff_len = R.size + L.suff_len;
    } else {
        res.suff_len = R.suff_len;
    }

    // 3. Calculate Global max_len
    res.max_len = max(L.max_len, R.max_len);
    if (L.suff_val == R.pref_val) {
        res.max_len = max(res.max_len, L.suff_len + R.pref_len);
    }

    return res;
}

vector<long long> arr;
vector<Node> tree;

void build(int node, int start, int end) {
    if (start == end) {
        tree[node] = Node(arr[start]);
        return;
    }
    int mid = (start + end) / 2;
    build(2 * node, start, mid);
    build(2 * node + 1, mid + 1, end);
    tree[node] = merge(tree[2 * node], tree[2 * node + 1]);
}

Node query(int node, int start, int end, int l, int r) {
    if (r < start || end < l) return Node(); // Out of range
    if (l <= start && end <= r) return tree[node]; // Fully in range

    int mid = (start + end) / 2;
    Node p1 = query(2 * node, start, mid, l, r);
    Node p2 = query(2 * node + 1, mid + 1, end, l, r);
    return merge(p1, p2);
}

void solve() {
    int q;
    cin>>n>>q;

    a.resize(n);
    for (int i=0; i<n; i++) {
        cin>>a[i];
    }

    tree.resize(4*n);

    arr.resize(n);
    arr[0]=0;
    for (int i=1; i<n; i++) {
        arr[i] = a[i] - a[i-1];
    }

    build(1, 0, n-1);

    while (q--) {
        int type;
        cin>>type;

        if (type==1) {

            int l, r, s, c;
            cin>>l>>r>>s>>c;
            l--; r--;

            /*for (int i=l; i<=r; i++) {
                a[i]+= (s + (i-l)*c);
            }*/

        } else if (type == 2) {

            int l, r, s, c;
            cin>>l>>r>>s>>c;
            l--; r--;

            /*for (int i=l; i<=r; i++) {
                a[i] = (s + (i-l)*c);
            }*/

        } else {

            int l, r;
            cin>>l>>r;
            l--; r--;

            Node res = query(1, 0, n - 1, l, r);

            cout<<res.max_len<<endl;

        }
    }
}

bool multi=false;

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
  

    int t=1;
    if (multi) cin>>t;
 
    while (t--) solve();
 
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...