标签:
线段树是一棵二叉树,记为T(a, b),参数a,b表示区间[a,b],其中b-a称为区间的长度,记为L。
struct Node { int left,right; //区间左右值 Node *leftchild; Node *rightchild; };
Node *build(int l , int r ) //建立二叉树 { Node *root = new Node; root->left = l; root->right = r; //设置结点区间 root->leftchild = NULL; root->rightchild = NULL; if ( l +1< r ) //L>1的情况,即不是叶子结点 { int mid = (r+l) >>1; root->leftchild = build ( l , mid ) ; root->rightchild = build ( mid , r) ; } return root; }
void Insert(int c, int d , Node *root ) { if(c<= root->left&&d>= root->right) root-> cover++; else { //比较下界与左子树的上界 if(c < (root->left+ root->right)/2 ) Insert (c,d, root->leftchild ); //比较上界与右子树的下界 if(d > (root->left+ root->right)/2 ) Insert (c,d, root->rightchild ); //注意,如果一个区间横跨左右儿子,那么不用担心,必定会匹配左儿子、右儿子中各一个节点 } }
删除一条线段[c,d]:
void Delete (int c , int d , Node *root ) { if(c<= root->left&&d>= root->right) root-> cover= root-> cover-1; else { if(c < (root->left+ root->right)/2 ) Delete ( c,d, root->leftchild ); if(d > (root->left+ root->right)/2 ) Delete ( c,d, root->rightchild ); } }
int a = 45, b = 35,tmp; while(b!=0){ a = a%b; tmp = a; a = b; b = tmp; } cout << a << endl;
标签:
原文地址:http://www.cnblogs.com/qionglouyuyu/p/4850774.html