Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- #define N 100005
- #define oo 999999999
- #define ll long long
- ll t[4*N], Lazy[4*N]; // one-based
- void push_down(int Node, int a, int b){
- if (Lazy[Node] != -oo) {
- if (a!=b){
- Lazy[Node*2] = Lazy[Node];
- Lazy[Node*2+1] = Lazy[Node];
- }
- t[Node] = Lazy[Node] * (b-a+1);
- Lazy[Node] = -oo;
- }
- }
- void update(int Node, int a, int b, int i, int j, ll Value){
- push_down(Node, a, b);
- if (i>j || a>j || i>b) return ;
- if (i<=a && b<=j) {
- Lazy[Node] = Value;
- push_down(Node, a, b);
- return ;
- }
- update(Node*2, a, (a+b)/2, i, j, Value);
- update(Node*2+1, (a+b)/2+1, b, i, j, Value);
- t[Node] = t[Node*2] + t[Node*2+1];
- }
- ll query(int Node, int a, int b, int i, int j){
- push_down(Node, a, b);
- if (i>j || a>j || i>b) return 0;
- if (i<=a && b<=j) return t[Node];
- ll q1 = query(Node*2, a, (a+b)/2, i, j);
- ll q2 = query(Node*2+1, (a+b)/2+1, b, i, j);
- return q1 + q2;
- }
- main(){
- int n = 4;
- for (int i=0; i<n; i++) update(1, 0, n-1, i, i, i+1); // build
- update(1, 0, n-1, 0, 3, 2);
- cout << query(1, 0, n-1, 0, 3) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment