Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1004103
  • 博文数量: 150
  • 博客积分: 3017
  • 博客等级: 少校
  • 技术积分: 3829
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-19 14:40
个人简介

Now in Baidu WISE team

文章分类

全部博文(150)

文章存档

2014年(8)

2013年(31)

2012年(111)

分类: C/C++

2013-01-13 23:53:00

自己写的线段树的模板。codepad.org已测试。
使用树形结构存储;动态根据需要建立结点;使用延迟标记更新段;查询时实时计算值。


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>

  4. typedef struct _Node{
  5.     int mid; //mid value , can be removed
  6.     int low;
  7.     int high;
  8.     int sign; // value of the segment
  9.     struct _Node* left;
  10.     struct _Node* right;
  11. }Node;
  12. Node* createNode(int low, int high){
  13.     assert(low<=high);

  14.     Node* ret = (Node*)malloc(sizeof(Node));
  15.     ret->mid = ((high+low)>>1);
  16.     ret->low = low;
  17.     ret->high = high;
  18.     ret->sign = 0;
  19.     ret->left = NULL;
  20.     ret->right = NULL;
  21.     return ret;
  22. }

  23. Node* InitialTree(int low, int high){
  24.     assert(low<=high);

  25.     Node* ret = (Node*)malloc(sizeof(Node));
  26.     ret->mid = ((high+low)>>1);
  27.     ret->low = low;
  28.     ret->high = high;
  29.     ret->left = ret->right = NULL;
  30.     ret->sign = 0;
  31.     return ret;
  32. }

  33. void Paint(Node* root, int low, int high){    
  34.     low = low<root->low?root->low: low;
  35.     high = high>root->high?root->high: high;
  36.     printf("paint %d %d into segment %d %d \n", low, high, root->low, root->high);
  37.     if(root->low == low && root->high == high){
  38.     //if(root->low == root->high){
  39.         root->sign++;
  40.         printf("value %d-%d = %d\n", root->low, root->high, root->sign);
  41.         return root;
  42.     }
  43.     
  44.     if(root->left == NULL)
  45.         root->left = createNode(root->low, root->mid);
  46.     if(root->right == NULL)
  47.         root->right = createNode(root->mid+1, root->high);
  48.     if(low <= root->mid)
  49.         Paint(root->left, low, high);
  50.     if(high > root->mid)
  51.         Paint(root->right, low, high);
  52. }
  53. int getValue(Node*root, int position){
  54.     if(position<root->low || position>root->high)
  55.         return -1;
  56.     if(position<=root->mid){
  57.         if(root->left!=NULL)
  58.             return root->sign+getValue(root->left, position);
  59.         return root->sign;
  60.     }
  61.     if(root->right!=NULL)
  62.         return root->sign + getValue(root->right, position);
  63.     return root->sign;
  64. }

  65. int main(){
  66.     Node* root = InitialTree(0,10);
  67.     printf("------------1~5--------------\n");
  68.     Paint(root, 1,5);
  69.     printf("------------2~6--------------\n");
  70.     Paint(root, 2,6);
  71.     printf("------------4~8---------------\n");
  72.     Paint(root, 4,8);
  73.     int i = 0;
  74.     for(;i<=10;i++){
  75.         printf("postion %d = %d \n",i, getValue(root, i));
  76.     }
  77. }

阅读(1861) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~