Giá trị lớn nhất ver2

View as PDF

Submit solution


Points: 0.10 (partial)
Time limit: 0.5s
Memory limit: 512M
Input: stdin
Output: stdout

Problem type
Allowed languages
C, C++, Go, Java, Kotlin, Pascal, PyPy, Python, Rust, Scratch

Cho một dãy gồm ~n~ phần tử có giá trị ban đầu bằng ~0~.

Cho ~m~ truy vấn có dạng:

  • ~0~ ~x~ ~y~ ~k~: tăng mỗi phần tử từ vị trí ~x~ đến vị trí ~y~ lên ~k~ đơn vị.
  • ~1~ ~x~ ~y~: cho biết giá trị lớn nhất của các phần tử có vị trí nằm trong đoạn [~x~, ~y~]

Input

  • ~n~: số phần tử của dãy ~(n \leq 50000)~.

  • ~m~: số lượng biến đổi và câu hỏi ~(m \le 10^5)~.

    • biến đổi có dạng: ~0~ ~x~ ~y~ ~k~ ~(1 \le k \le 10000)~
    • câu hỏi có dạng: ~1~ ~x~ ~y~

Output

Ghi ra trả lời cho lần lượt từng câu hỏi.

Sample Input

6 3
0 1 3 3
0 4 6 4
1 1 6

Sample Output

4

Comments

Please read the guidelines before commenting.



  • 0
    vominhmanh10  commented on Nov. 6, 2025, 4:31 a.m. edited

    lại lazy

    #include<bits/stdc++.h>
    using namespace std;
    using ll = long long;
    using pll = pair< ll, ll>;
    #define fi first
    #define se second
    const int maxn = 1e6 + 5;
    ll seg[4 * maxn], lazy[4 * maxn];
    int n, m;
    void down(int node) {
        ll val = lazy[node];
        if (val) {
            lazy[2 * node] += val;
            lazy[2 * node + 1] += val;
            seg[2 * node] += val;
            seg[2 * node + 1] += val;
            lazy[node] = 0;
        }
    }
    
    void add(int node, int start, int end, int l, int r, ll val) {
        if (start > r || end < l) return;
        if (start >= l && end <= r) {
            seg[node] += val;
            lazy[node] += val;
            return;
        }
        down(node);
        int mid = (start + end) / 2;
        add(2 * node, start, mid, l, r, val);
        add(2 * node + 1, mid + 1, end, l, r, val);
        seg[node] = max(seg[2 * node], seg[2 * node + 1]);
    }
    
    ll query(int node, int start, int end, int l, int r) {
        if (start > r || end < l) return -1e18;
        if (start >= l && end <= r) {
            return seg[node];
            }
            down(node);
        int mid = (start + end) / 2;
        return max(query(2 * node, start, mid, l, r), query(2 * node + 1, mid + 1, end, l, r));
    }
    
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        cin >> n >> m;
        int t, l, r, k;
        while (cin >> t) {
            if (t) {
                cin >> l >> r;
                cout << query(1, 1, n, l, r) << "\n";
            }
            else {
                cin >> l >> r >> k;
                add(1, 1, n, l, r, k);
            }
        }
    }
    

  • 2
    tuanpham2024  commented on June 26, 2025, 1:43 a.m.

    Bài này có ai cài bằng Python được không? Dùng Segment Tree Lazy Update cho C++ thì chạy ngon lành mà convert qua Python không sao qua được test cuối (dính TLE).


    • 0
      haoluu2009  commented on July 14, 2025, 2:42 p.m.

      minh thu cai ben python cung bi TLE a 😭


  • 2
    Anhngutoan  commented on Feb. 4, 2025, 1:07 a.m. edited

    bruh


  • -7
    huy_lovely  commented on July 25, 2024, 4:29 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -30
    AnonymousExe  commented on July 6, 2024, 2:00 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -58
    PPAP_1264589  commented on July 31, 2021, 1:43 p.m. edited

    This comment is hidden due to too much negative feedback. Show it anyway.


    • -35
      nictysine1  commented on April 8, 2022, 12:47 a.m.

      This comment is hidden due to too much negative feedback. Show it anyway.


  • 20
    leduykhongngu  commented on May 22, 2021, 8:39 a.m.

    Time limit bài này đã được giảm xuống 0.5s nhé :3