Discrete segment Tree

Source: Internet
Author: User

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <ctime>6#include <climits>7#include <string>8#include <iterator>9#include <algorithm>Ten#include <cstdlib> One#include <queue> A#include <stack> -#include <list> -#include <map> the#include <vector> -#include <iterator> - using namespacestd; - #definePI ACOs (-1.0) + #defineINF 0x3f3f3f3f - #defineMAXN 400005 + #defineMAXM 400005 A #defineMOD 10000 at #defineEPS 1e-6 - #defineRST (A, B) memset (A,b,sizeof (a)) - #definePD (a) cout << "debug:" << a << ""; - #definePP (a) cout << a << ""; - #definePL (a) cout << a << Endl; - #defineFor (A, B, c) for (int a = B; a < C; a++) intypedefLong LongLL; -typedef unsignedLong LongULL; totypedef pair<int,int>PII; + structLine { -     intnum; the     intLi; * }LINE[MAXN]; $ structNode {Panax Notoginseng     intL, R; -     intcolor; the}tree[maxn*4]; + intpost[maxn][2]; A BOOLCMP (line A, line B) { the     returnA.li <B.li; + } - voidBuildintCintLintr) { $TREE[C].L =l; $TREE[C].R =R; -Tree[c].color =0; -     if(L = = r)return; the     Else { -         intMid = (L + r)/2;WuyiBuild2*C, L, mid); theBuild2*c+1, mid+1, R); -     } Wu } - voidUpdata (intCintLintRintcolor) { About     if(TREE[C].L = = L && TREE[C].R = =r) { $Tree[c].color =color; -         //printf ("(%d,%d)-%d\n", TREE[C].L, TREE[C].R, tree[c].color); -         return; -     } A     if(tree[c].color) { +tree[2*c].color =Tree[c].color; thetree[2*c+1].color =Tree[c].color; -Tree[c].color =0; $     } the     intMid = (TREE[C].L + TREE[C].R)/2; the     if(R <= Mid) Updata (2*C, L, r, color); the     Else if(L > Mid) Updata (2*c+1, L, r, color); the     Else { -Updata (2*C, L, Mid, color); inUpdata (2*c+1, mid+1, R, color); the     } the } About intans; the BOOLVIS[MAXN]; the voidQueryintc) { the     if(tree[c].color) { +         if(!Vis[tree[c].color]) { -             //printf ("(%d-%d)", TREE[C].L, TREE[C].R); theVis[tree[c].color] =1;Bayians++; the         } the         return; -     } -     if(TREE[C].L = = TREE[C].R)return; theQuery2*c); theQuery2*c+1); the } the intMain () { -     //freopen ("D:\\input.txt", "R", stdin); the     intN, L; thescanf"%d%d", &n, &l); the      for(inti =0; I < n; i++) {94scanf"%d%d", &post[i][0], &post[i][1]); theline[2*i].li = post[i][0]; theline[2*i].num =-(i+1); theline[2*i+1].li = post[i][1];98line[2*i+1].num = i+1; About     } -Sort (line, line+2*N, CMP);101     intTMP = line[0].li;102     intTP =1;103      for(inti =0; I <2*n; i++) {104         if(TMP! =line[i].li) { theTP + =2;106TMP =Line[i].li;107         }108         if(Line[i].num <0) post[-line[i].num-1][0] =TP;109         Elsepost[line[i].num-1][1] =TP; the     }111Build1,1, TP); the      for(inti =0; I < n; i++) {113         //printf ("(%d,%d) \ n", Post[i][0], post[i][1]); theUpdata (1, post[i][0], post[i][1], i+1); the     } theAns =0;117Query1);118printf"%d\n", ans);119     return 0; -}

Discrete segment Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.