POJ 2528 hash+interval tree(离散化+线段树)

#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; const int MAXN=10002; const int sumnode=65540; struct value { int left; int right; }val[MAXN]; int ans; bool flag; struct st { int index; int value; bool isleft; }store[MAXN*2]; int mymap[MAXN*2][2]; struct nod { int left; int right; bool cover; int lchild; int rchild; bool hasnext; }node[sumnode]; void build(int index,int s,int t)//build tree { node[index].left=s; node[index].right=t; node[index].cover=false; node[index].hasnext=false; if(s==t) { node[index].lchild=-1; node[index].rchild=-1; node[index].hasnext=false; return; } node[index].lchild=index*2; node[index].rchild=index*2+1; build(index*2,s,(s+t)>>1); build(index*2+1,((s+t)>>1)+1,t); } void insert_tree(int index,int s,int t,int from,int to) { int mid; mid=(s+t)>>1; if(s==from&&t==to&&node[index].cover==false) { flag=true; node[index].cover=true; return; } if(node[index].cover==true) { return; } if(to<=node[index<<1].right)//to at left child tree { insert_tree(index*2,s,mid,from,to);//search left child } else if(from>=node[(index<<1)+1].left)//from at right child tree { insert_tree(index*2+1,mid+1,t,from,to);//search right child } else { insert_tree(index*2,s,mid,from,node[index*2].right);// insert_tree(index*2+1,mid+1,t,node[index*2+1].left,to);//search left&&right child tree } node[index].cover=(node[index<<1].cover)&(node[(index<<1)+1].cover); } bool cmp(const st &a,const st &b) { return a.value<b.value? true:false; } int main() { int t; int n; int i; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&val[i].left); scanf("%d",&val[i].right); } for(i=1;i<=n;i++) { store[i*2-1].index=i; store[i*2].index=i; store[i*2-1].value=val[i].left; store[i*2-1].isleft=true; store[i*2].value=val[i].right; store[i*2].isleft=false; } store[0].value=0; sort(&store[0],&store[n*2+1],cmp); int temp=n*2; int m=0; for(i=1;i<=temp;i++) { if(store[i].value!=store[i-1].value) { m++; mymap[store[i].index][store[i].isleft]=m; } else { mymap[store[i].index][store[i].isleft]=m; } }//hash离散化 build(1,1,m);//build interval tree int from; int to; ans=0; for(i=n;i>=1;i--) { from=mymap[i][1]; to=mymap[i][0]; flag=false; insert_tree(1,1,m,from,to);//insert_tree(int index,int s,int t,int from,int to) if(flag==true) { ans++; } } printf("%d/n",ans); } return 0; }  

你可能感兴趣的:(struct,tree,insert,Build,include)