关于线段树

1.原文链接:点击打开链接

线段树的入门级 总结


      线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点。
      对于线段树中的每一个非叶子节点[a,b],它的左儿子表示的区间为[a,(a+b)/2],右儿子表示的区间为[(a+b)/2+1,b]。因此线段树是平衡二叉树,最后的子节点数目为N,即整个线段区间的长度。
      使用线段树可以快速的查找某一个节点在若干条线段中出现的次数,时间复杂度为O(logN)。而未优化的空间复杂度为2N,因此有时需要离散化让空间压缩。

----来自百度百科
【以下以 求区间最大值为例】
先看声明:

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <math.h>  
  3. const int MAXNODE = 2097152;  
  4. const int MAX = 1000003;  
  5. struct NODE{  
  6.     int value;        // 结点对应区间的权值  
  7.     int left,right;   // 区间 [left,right]  
  8. }node[MAXNODE];  
  9. int father[MAX];     // 每个点(当区间长度为0时,对应一个点)对应的结构体数组下标  


【创建线段树(初始化)】:

       由于线段树是用二叉树结构储存的,而且是近乎完全二叉树的,所以在这里我使用了数组来代替链表上图中区间上面的红色数字表示了结构体数组中对应的下标。

在完全二叉树中假如一个结点的序号(数组下标)为 I ,那么 (二叉树基本关系)

I 的父亲为 I/2,

I 的另一个兄弟为 I/2*2 或 I/2*2+1

I 的两个孩子为 I*2 (左)   I*2+1(右)

有了这样的关系之后,我们便能很方便的写出创建线段树的代码了。

[cpp] view plain copy
  1. void BuildTree(int i,int left,int right){ // 为区间[left,right]建立一个以i为祖先的线段树,i为数组下标,我称作结点序号  
  2.     node[i].left = left;    // 写入第i个结点中的 左区间  
  3.     node[i].right = right;  // 写入第i个结点中的 右区间  
  4.     node[i].value = 0;      // 每个区间初始化为 0  
  5.     if (left == right){ // 当区间长度为 0 时,结束递归  
  6.         father[left] = i; // 能知道某个点对应的序号,为了更新的时候从下往上一直到顶  
  7.         return;  
  8.     }  
  9.     // 该结点往 左孩子的方向 继续建立线段树,线段的划分是二分思想,如果写过二分查找的话这里很容易接受  
  10.     // 这里将 区间[left,right] 一分为二了  
  11.     BuildTree(i<<1, left, (int)floor( (right+left) / 2.0));  
  12.     // 该结点往 右孩子的方向 继续建立线段树  
  13.     BuildTree((i<<1) + 1, (int)floor( (right+left) / 2.0) + 1, right);  
  14. }  

【单点更新线段树】:

       由于我事先用 father[ ] 数组保存过 每单个结点 对应的下标了,因此我只需要知道第几个点,就能知道这个点在结构体中的位置(即下标)了,这样的话,根据之前已知的基本关系,就只需要直接一路更新上去即可。

[cpp] view plain copy
  1. void UpdataTree(int ri){ // 从下往上更新(注:这个点本身已经在函数外更新过了)  
  2.   
  3.     if (ri == 1)return// 向上已经找到了祖先(整个线段树的祖先结点 对应的下标为1)  
  4.     int fi = ri / 2;        // ri 的父结点  
  5.     int a = node[fi<<1].value; // 该父结点的两个孩子结点(左)  
  6.     int b = node[(fi<<1)+1].value; // 右  
  7.     node[fi].value = (a > b)?(a):(b);    // 更新这个父结点(从两个孩子结点中挑个大的)  
  8.     UpdataTree(ri/2);       // 递归更新,由父结点往上找  
  9.  
【查询区间最大值】:
       将一段区间按照建立的线段树从上往下一直拆开,直到存在有完全重合的区间停止。对照图例建立的树,假如查询区间为 [2,5] 

红色的区间为完全重合的区间,因为在这个具体问题中我们只需要比较这 三个区间的值 找出 最大值 即可。
[cpp] view plain copy
  1. int Max = -1<<20;  
  2. void Query(int i,int l,int r){ // i为区间的序号(对应的区间是最大范围的那个区间,也是第一个图最顶端的区间,一般初始是 1 啦)  
  3.     if (node[i].left == l && node[i].right == r){ // 找到了一个完全重合的区间  
  4.         Max = (Max < node[i].value)?node[i].value:(Max);  
  5.         return ;  
  6.     }  
  7.     i = i << 1; // get the left child of the tree node  
  8.     if (l <= node[i].right){ // 左区间有涉及  
  9.         if (r <= node[i].right) // 全包含于左区间,则查询区间形态不变  
  10.             Query(i, l, r);  
  11.         else // 半包含于左区间,则查询区间拆分,左端点不变,右端点变为左孩子的右区间端点  
  12.             Query(i, l, node[i].right);  
  13.     }  
  14.     i += 1; // right child of the tree  
  15.     if (r >= node[i].left){ // 右区间有涉及  
  16.         if (l >= node[i].left) // 全包含于右区间,则查询区间形态不变  
  17.             Query(i, l, r);  
  18.         else // 半包含于左区间,则查询区间拆分,与上同理  
  19.             Query(i, node[i].left, r);  
  20.     }  
  21. }  

2.原文链接:点击打开链接

风格:

maxn是题目给的最大区间,而节点数要开4倍,确切的说……

lson和rson辨别表示结点的左孩子和右孩子。

PushUp(int rt)是把当前结点的信息更新到父节点

PushDown(int rt)是把当前结点的信息更新给孩子结点。

rt表示当前子树的根(root),也就是当前所在的结点。


思想:

对于每个非叶节点所标示的结点 [a,b],其做孩子表示的区间是[a,(a+b)/2],其右孩子表示[(a+b)/2,b].

构造:



离散化和线段树:

题目:x轴上有若干个线段,求线段覆盖的总长度。

普通解法:设置坐标范围[min,max],初始化为0,然后每一段分别染色为1,最后统计1的个数,适用于线段数目少,区间范围小。

离散化的解法:离散化就是一一映射的关系,即将一个大坐标和小坐标进行一一映射,适用于线段数目少,区间范围大。

例如:[10000,22000],[30300,55000],[44000,60000],[55000,60000].

第一步:排序 10000 22000 30300 44000 55000 60000

第二部:编号 1        2        3         4       5         6

第三部:用编号来代替原数,即小数代大数 。

[10000,22000]~[1,2]

[30300,55000]~[3,5]

[44000,60000]~[4,6]

[55000,60000]~[5,6]

然后再用小数进行普通解法的步骤,最后代换回去。

线段树的解法:线段树通过建立线段,将原来染色O(n)的复杂度减小到 log(n),适用于线段数目多,区间范围小的情况。

离散化的线段树:适用于线段数目多,区间范围大的情况。


构造:

动态数据结构:

struct node{

 node* left;

 node* right;

……

}

静态全局数组模拟(完全二叉树):

struct node{

  int left;

  int right;

……

}Tree[MAXN]

例如:



线段树与点树:

线段树的每一个结点表示一个点,成为点树,比如说用于求第k小数的线段树。

点树结构体:

struct node{

int l, r;

int c;//用于存放次结点的值,默认为0

}T[3*MAXN];

创建:

创建顺序为先序遍历,即先构造根节点,再构造左孩子,再构造右孩子。

[cpp]  view plain copy
  1. void construct(int l, int r, int k){  
  2.     T[k].l = l;  
  3.     T[k].r = r;  
  4.     T[k].c = 0;  
  5.     if(l == r) return ;  
  6.     int m = (l + r) >> 1;  
  7.     construct(l, m, k << 1);  
  8.     construct(m + 1, r, (k << 1) + 1);  
  9.     return ;  
  10. }  
[cpp]  view plain  copy
  1. void construct(int l, int r, int k){  
  2.     T[k].l = l;  
  3.     T[k].r = r;  
  4.     T[k].c = 0;  
  5.     if(l == r) return ;  
  6.     int m = (l + r) >> 1;  
  7.     construct(l, m, k << 1);  
  8.     construct(m + 1, r, (k << 1) + 1);  
  9.     return ;  
  10. }  
 


[A,B,C]:A表示左值,B表示右值,C表示在静态数组中的位置,由此可知,n个点的话大约共有2*n个结点,因此开3*n的结构体一定是够的。


更新值:

[cpp]  view plain copy
  1. void insert(int d, int k){  
  2.     //如果找到了就c值+1返回。   
  3.     if(T[k].l == T[k].r && d == T[k].l){  
  4.         T[k].c += 1;  
  5.         return ;  
  6.     }  
  7.     int m = (T[k].l + T[k].r) >> 1;  
  8.     if(d <= m) insert(d, k << 1);  
  9.     else insert(d, (k << 1) + 1);  
  10.     //更新每一个c,向上更新   
  11.     T[k].c = T[k << 1].c + T[(k << 1) + 1].c;  
  12. }  
[cpp]  view plain  copy
  1. void insert(int d, int k){  
  2.     //如果找到了就c值+1返回。  
  3.     if(T[k].l == T[k].r && d == T[k].l){  
  4.         T[k].c += 1;  
  5.         return ;  
  6.     }  
  7.     int m = (T[k].l + T[k].r) >> 1;  
  8.     if(d <= m) insert(d, k << 1);  
  9.     else insert(d, (k << 1) + 1);  
  10.     //更新每一个c,向上更新  
  11.     T[k].c = T[k << 1].c + T[(k << 1) + 1].c;  
  12. }  

查找值:

[cpp]  view plain copy
  1. //k表示树根,d表示要查找的值   
  2. void search(int d, int k, int& ans)  
  3. {  
  4.     if(T[k].l == T[k].r){  
  5.         ans = T[k].l;  
  6.         ans = T[k].l;  
  7.     }  
  8.     int m = (T[k].l + T[k].r) >> 1;  
  9.     //不懂   
  10.     if(d > T[(k << 1)].c) search(d - T[k << 1].c, (k << 1) + 1, ans);  
  11.     else search(d, k << 1, ans);  
  12. }  
[cpp]  view plain  copy
  1. //k表示树根,d表示要查找的值  
  2. void search(int d, int k, int& ans)  
  3. {  
  4.     if(T[k].l == T[k].r){  
  5.         ans = T[k].l;  
  6.         ans = T[k].l;  
  7.     }  
  8.     int m = (T[k].l + T[k].r) >> 1;  
  9.     //不懂  
  10.     if(d > T[(k << 1)].c) search(d - T[k << 1].c, (k << 1) + 1, ans);  
  11.     else search(d, k << 1, ans);  
  12. }  

search函数的用法不太懂。

例题解:

(待更新)


四类题型:

1.单点更新   只更新叶子结点,然后把信息用PushUp(int r)这个函数更新上来。

hdu1166:敌兵布阵

线段树功能:update:单点替换 query:区间最值




poj2828

树状数组:

[cpp]  view plain copy
  1. #include <iostream>   
  2. #include <cstdio>   
  3. #include <string>   
  4. #include <cstring>   
  5. using namespace std;  
  6.   
  7. typedef pair<intint> PII;  
  8.   
  9. const int maxn = 200000;  
  10.   
  11. int C[maxn + 100];  
  12. int B[maxn + 100];  
  13. int n;  
  14. PII arr[maxn + 100];  
  15.   
  16. int lowbit(int k) { return k & (-k); }  
  17.   
  18. void init() {  
  19.     for(int i = 1; i <= n; i++) C[i] = lowbit(i);  
  20.     memset(B, -1, n + 10);  
  21. }  
  22.   
  23. void update(int i) {  
  24.     while(i <= n) {  
  25.         C[i]--;  
  26.         i += lowbit(i);  
  27.     }  
  28. }  
  29.   
  30.   
  31. int query(int i) {  
  32.     int ret = 0;  
  33.     while(i > 0) {  
  34.         ret += C[i];  
  35.         i -= lowbit(i);  
  36.     }  
  37.     return ret;  
  38. }  
  39.   
  40. void debug() {  
  41.     for(int i = 1; i <= n; i++) cout << i << " " << query(i) << endl;  
  42. }  
  43.   
  44.   
  45. void fun(int a, int v) {  
  46.     int l = 1, r = n;  
  47.     while(l < r) {  
  48.         int m = (l + r) >> 1;  
  49.         if(query(m) >= a) r = m;  
  50.         else l = m + 1;  
  51.     }  
  52.     //cout << "here  " << l << endl;   
  53.     update(l);  
  54.     //cout << "here2 " << endl;   
  55.     //debug();   
  56.     B[l] = v;  
  57.     //return l;   
  58. }  
  59.   
  60.   
  61.   
  62.   
  63. int main() {  
  64.     while(~scanf("%d", &n)) {  
  65.         init();  
  66.         int a, b;  
  67.         for(int i = 1; i <= n; i++) {  
  68.             scanf("%d%d", &a, &b);  
  69.             a++;  
  70.             arr[i].first = a;  
  71.             arr[i].second = b;  
  72.         }  
  73.         for(int i = n; i > 0; i--) fun(arr[i].first, arr[i].second);  
  74.         //debug2();   
  75.         //bool flag = false;   
  76.         for(int i = 1; i <= n; i++) {  
  77.             i == 1 ? printf("%d", B[i]) : printf(" %d", B[i]);  
  78.             //if(B[i] != -1 && !flag) { printf("%d", B[i]); flag = true; }  
  79.             //else if(B[i] != -1) printf(" %d", B[i]);  
  80.         }  
  81.         puts("");  
  82.     }  
  83.     return 0;  
  84. }  
[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <string>  
  4. #include <cstring>  
  5. using namespace std;  
  6.   
  7. typedef pair<intint> PII;  
  8.   
  9. const int maxn = 200000;  
  10.   
  11. int C[maxn + 100];  
  12. int B[maxn + 100];  
  13. int n;  
  14. PII arr[maxn + 100];  
  15.   
  16. int lowbit(int k) { return k & (-k); }  
  17.   
  18. void init() {  
  19.     for(int i = 1; i <= n; i++) C[i] = lowbit(i);  
  20.     memset(B, -1, n + 10);  
  21. }  
  22.   
  23. void update(int i) {  
  24.     while(i <= n) {  
  25.         C[i]--;  
  26.         i += lowbit(i);  
  27.     }  
  28. }  
  29.   
  30.   
  31. int query(int i) {  
  32.     int ret = 0;  
  33.     while(i > 0) {  
  34.         ret += C[i];  
  35.         i -= lowbit(i);  
  36.     }  
  37.     return ret;  
  38. }  
  39.   
  40. void debug() {  
  41.     for(int i = 1; i <= n; i++) cout << i << " " << query(i) << endl;  
  42. }  
  43.   
  44.   
  45. void fun(int a, int v) {  
  46.     int l = 1, r = n;  
  47.     while(l < r) {  
  48.         int m = (l + r) >> 1;  
  49.         if(query(m) >= a) r = m;  
  50.         else l = m + 1;  
  51.     }  
  52.     //cout << "here  " << l << endl;  
  53.     update(l);  
  54.     //cout << "here2 " << endl;  
  55.     //debug();  
  56.     B[l] = v;  
  57.     //return l;  
  58. }  
  59.   
  60.   
  61.   
  62.   
  63. int main() {  
  64.     while(~scanf("%d", &n)) {  
  65.         init();  
  66.         int a, b;  
  67.         for(int i = 1; i <= n; i++) {  
  68.             scanf("%d%d", &a, &b);  
  69.             a++;  
  70.             arr[i].first = a;  
  71.             arr[i].second = b;  
  72.         }  
  73.         for(int i = n; i > 0; i--) fun(arr[i].first, arr[i].second);  
  74.         //debug2();  
  75.         //bool flag = false;  
  76.         for(int i = 1; i <= n; i++) {  
  77.             i == 1 ? printf("%d", B[i]) : printf(" %d", B[i]);  
  78.             //if(B[i] != -1 && !flag) { printf("%d", B[i]); flag = true; }  
  79.             //else if(B[i] != -1) printf(" %d", B[i]);  
  80.         }  
  81.         puts("");  
  82.     }  
  83.     return 0;  
  84. }  

poj-3468

[cpp]  view plain copy
  1. #include <cstdio>   
  2. #include <cstring>   
  3. #include <iostream>   
  4. using namespace std;  
  5.   
  6. #define lson l, m, rt<<1   
  7. #define rson m+1, r, rt<<1|1  
  8.   
  9. typedef long long LL;  
  10.   
  11. const int maxn = 111111;  
  12.   
  13. LL col[maxn<<2];  
  14. LL sum[maxn<<2];  
  15.   
  16. void PushUp(LL rt) {  
  17.     sum[rt] = sum[rt<<1] + sum[rt<<1|1];  
  18. }  
  19.   
  20. //pushdown的作用是如果此点可以更新。   
  21. //也就是更新到下一层   
  22. //如果是底层,那么是不用pushdown的。   
  23. void PushDown(LL rt, LL m) {  
  24.     if(col[rt]) {  
  25.         //col[rt<<1] = col[rt<<1|1] = col[rt];  
  26.         col[rt<<1] += col[rt];  
  27.         col[rt<<1|1] += col[rt];  
  28.         sum[rt<<1] += col[rt] * (m - (m>>1));  
  29.         sum[rt<<1|1] += col[rt] * (m>>1);  
  30.         col[rt] = 0;  
  31.     }  
  32. }  
  33.   
  34. void build(LL l, LL r, LL rt) {  
  35.     col[rt] = 0;  
  36.     //cout << l << " " << r << endl;   
  37.     if(l == r) {  
  38.         scanf("%I64d", &sum[rt]);  
  39.         //cout << rt << " " << sum[rt] << endl;  
  40.         return ;  
  41.     }  
  42.     int m = (l + r) >> 1;  
  43.     build(lson);  
  44.     build(rson);  
  45.     PushUp(rt);  
  46. }  
  47.   
  48. LL query(LL L, LL R, LL l, LL r, LL rt) {  
  49.     LL ret = 0;  
  50.     if(L <= l && r <= R) {  
  51.         //if(col[rt]) return sum[rt] + (r - l + 1) * col[rt];  
  52.         return sum[rt];  
  53.     }  
  54.     PushDown(rt, r - l + 1);  
  55.     int m = (l + r) >> 1;  
  56.     if(L <= m) ret += query(L, R, lson);  
  57.     if(R > m) ret += query(L, R, rson);  
  58.     return ret;  
  59. }  
  60.   
  61. void update(LL L, LL R, LL c, LL l, LL r, LL rt) {  
  62.     if(L <= l && r <= R) {  
  63.         sum[rt] += c * (r - l + 1);  
  64.         col[rt] += c;//子节点没有更新   
  65.         return ;  
  66.     }  
  67.     PushDown(rt, r - l + 1);  
  68.     int m = (l + r) >> 1;  
  69.     if(L <= m) update(L, R, c, lson);  
  70.     if(R > m) update(L, R, c, rson);  
  71.     PushUp(rt);  
  72. }  
  73.   
  74. void debug(int n) {  
  75.     for(int i = 1; i <= (n*3); i++) {  
  76.         cout << i << " ";  
  77.     }  
  78.     cout << endl;  
  79.     for(int i = 1; i <= (n*3); i++) {  
  80.         cout << col[i] << " ";  
  81.     }  
  82.     cout << endl << endl;  
  83.     for(int i = 1; i <= (n*3); i++) {  
  84.         cout << i << " ";  
  85.     }  
  86.     cout << endl;  
  87.     for(int i = 1; i <= (n*3); i++) {  
  88.         cout << sum[i] << " ";  
  89.     }  
  90.     cout << endl;  
  91. }  
  92.   
  93. int main() {  
  94.     LL N, Q;  
  95.     while(~scanf("%I64d%I64d", &N, &Q)) {  
  96.         //cout << "N = " << N << endl;   
  97.         memset(sum, 0, sizeof(sum));  
  98.         memset(col, 0, sizeof(col));  
  99.         build(1, N, 1);  
  100.         //debug(N);   
  101.         for(int i = 0; i < Q; i++) {  
  102.             char ch[3];  
  103.             LL a, b, c;  
  104.             scanf("%s", ch);  
  105.             if(ch[0] == 'Q') {  
  106.                 scanf("%I64d%I64d", &a, &b);  
  107.                 printf("%I64d\n", query(a, b, 1, N, 1));  
  108.             }  
  109.             else {  
  110.                 scanf("%I64d%I64d%I64d", &a, &b, &c);  
  111.                 update(a, b, c, 1, N, 1);  
  112.             }  
  113.             //debug(N);   
  114.         }  
  115.     }  
  116.     return 0;  
  117. }  
[cpp]  view plain  copy
  1. #include <cstdio>  
  2. #include <cstring>  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. #define lson l, m, rt<<1  
  7. #define rson m+1, r, rt<<1|1  
  8.   
  9. typedef long long LL;  
  10.   
  11. const int maxn = 111111;  
  12.   
  13. LL col[maxn<<2];  
  14. LL sum[maxn<<2];  
  15.   
  16. void PushUp(LL rt) {  
  17.     sum[rt] = sum[rt<<1] + sum[rt<<1|1];  
  18. }  
  19.   
  20. //pushdown的作用是如果此点可以更新。  
  21. //也就是更新到下一层  
  22. //如果是底层,那么是不用pushdown的。  
  23. void PushDown(LL rt, LL m) {  
  24.     if(col[rt]) {  
  25.         //col[rt<<1] = col[rt<<1|1] = col[rt];  
  26.         col[rt<<1] += col[rt];  
  27.         col[rt<<1|1] += col[rt];  
  28.         sum[rt<<1] += col[rt] * (m - (m>>1));  
  29.         sum[rt<<1|1] += col[rt] * (m>>1);  
  30.         col[rt] = 0;  
  31.     }  
  32. }  
  33.   
  34. void build(LL l, LL r, LL rt) {  
  35.     col[rt] = 0;  
  36.     //cout << l << " " << r << endl;  
  37.     if(l == r) {  
  38.         scanf("%I64d", &sum[rt]);  
  39.         //cout << rt << " " << sum[rt] << endl;  
  40.         return ;  
  41.     }  
  42.     int m = (l + r) >> 1;  
  43.     build(lson);  
  44.     build(rson);  
  45.     PushUp(rt);  
  46. }  
  47.   
  48. LL query(LL L, LL R, LL l, LL r, LL rt) {  
  49.     LL ret = 0;  
  50.     if(L <= l && r <= R) {  
  51.         //if(col[rt]) return sum[rt] + (r - l + 1) * col[rt];  
  52.         return sum[rt];  
  53.     }  
  54.     PushDown(rt, r - l + 1);  
  55.     int m = (l + r) >> 1;  
  56.     if(L <= m) ret += query(L, R, lson);  
  57.     if(R > m) ret += query(L, R, rson);  
  58.     return ret;  
  59. }  
  60.   
  61. void update(LL L, LL R, LL c, LL l, LL r, LL rt) {  
  62.     if(L <= l && r <= R) {  
  63.         sum[rt] += c * (r - l + 1);  
  64.         col[rt] += c;//子节点没有更新  
  65.         return ;  
  66.     }  
  67.     PushDown(rt, r - l + 1);  
  68.     int m = (l + r) >> 1;  
  69.     if(L <= m) update(L, R, c, lson);  
  70.     if(R > m) update(L, R, c, rson);  
  71.     PushUp(rt);  
  72. }  
  73.   
  74. void debug(int n) {  
  75.     for(int i = 1; i <= (n*3); i++) {  
  76.         cout << i << " ";  
  77.     }  
  78.     cout << endl;  
  79.     for(int i = 1; i <= (n*3); i++) {  
  80.         cout << col[i] << " ";  
  81.     }  
  82.     cout << endl << endl;  
  83.     for(int i = 1; i <= (n*3); i++) {  
  84.         cout << i << " ";  
  85.     }  
  86.     cout << endl;  
  87.     for(int i = 1; i <= (n*3); i++) {  
  88.         cout << sum[i] << " ";  
  89.     }  
  90.     cout << endl;  
  91. }  
  92.   
  93. int main() {  
  94.     LL N, Q;  
  95.     while(~scanf("%I64d%I64d", &N, &Q)) {  
  96.         //cout << "N = " << N << endl;  
  97.         memset(sum, 0, sizeof(sum));  
  98.         memset(col, 0, sizeof(col));  
  99.         build(1, N, 1);  
  100.         //debug(N);  
  101.         for(int i = 0; i < Q; i++) {  
  102.             char ch[3];  
  103.             LL a, b, c;  
  104.             scanf("%s", ch);  
  105.             if(ch[0] == 'Q') {  
  106.                 scanf("%I64d%I64d", &a, &b);  
  107.                 printf("%I64d\n", query(a, b, 1, N, 1));  
  108.             }  
  109.             else {  
  110.                 scanf("%I64d%I64d%I64d", &a, &b, &c);  
  111.                 update(a, b, c, 1, N, 1);  
  112.             }  
  113.             //debug(N);  
  114.         }  
  115.     }  
  116.     return 0;  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值