kien_coi_1997

2236.cpp

May 13th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. #define N 100005
  7. #define oo 999999999
  8. #define ll long long
  9.  
  10. ll t[4*N], Lazy[4*N]; // one-based
  11.  
  12. void push_down(int Node, int a, int b){
  13.     if (Lazy[Node] != -oo) {
  14.         if (a!=b){
  15.             Lazy[Node*2] = Lazy[Node];
  16.             Lazy[Node*2+1] = Lazy[Node];
  17.         }
  18.         t[Node] = Lazy[Node] * (b-a+1);
  19.         Lazy[Node] = -oo;
  20.     }
  21. }
  22.  
  23. void update(int Node, int a, int b, int i, int j, ll Value){
  24.     push_down(Node, a, b);
  25.     if (i>j || a>j || i>b) return ;
  26.     if (i<=a && b<=j) {
  27.         Lazy[Node] = Value;
  28.         push_down(Node, a, b);
  29.         return ;
  30.     }
  31.     update(Node*2, a, (a+b)/2, i, j, Value);
  32.     update(Node*2+1, (a+b)/2+1, b, i, j, Value);
  33.     t[Node] = t[Node*2] + t[Node*2+1];
  34. }
  35.  
  36. ll query(int Node, int a, int b, int i, int j){
  37.     push_down(Node, a, b);
  38.     if (i>j || a>j || i>b) return 0;
  39.     if (i<=a && b<=j) return t[Node];
  40.     ll q1 = query(Node*2, a, (a+b)/2, i, j);
  41.     ll q2 = query(Node*2+1, (a+b)/2+1, b, i, j);
  42.     return q1 + q2;
  43. }
  44.  
  45. main(){
  46.     int n = 4;
  47.     for (int i=0; i<n; i++) update(1, 0, n-1, i, i, i+1); // build
  48.     update(1, 0, n-1, 0, 3, 2);
  49.     cout << query(1, 0, n-1, 0, 3) << endl;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment