线段树约对是神马的东西,自己的想法又失败了,果断超时~~~~~~~~
#include<stdio.h> #include<string.h> #define N 100005 #define Exchange(a,b) (a)^=(b); (b)^=(a); (a)^=(b) struct node{ int l; int r; int col; }; node tree[3*N]; node *(p[N]); int color[35]; void buid_tree(int k,int left,int right) { int mid; int L,R; tree[k].l = left; tree[k].r = right; tree[k].col = 1; if(tree[k].r-tree[k].l == 1){ p[tree[k].l] = &tree[k]; return ; } mid = (tree[k].l+tree[k].r)/2; L = 2*k; R = L+1; buid_tree(L,left,mid); buid_tree(R,mid,right); return ; } void tree_insert(int k,int left,int right,int data) { int mid; int L,R; if(left <= tree[k].l && right >= tree[k].r){ tree[k].col = data; return ; } mid = (tree[k].l+tree[k].r)/2; L = 2*k; R = L+1; if(tree[k].col){ tree[L].col = tree[R].col = tree[k].col; tree[k].col = 0; } if(left < mid) tree_insert(L,left,right,data); if(right > mid) tree_insert(R,left,right,data); return ; } void tree_search(int k,int left,int right,int &count) { int mid; int L,R; if(tree[k].col){ if(!color[tree[k].col]) count++; color[tree[k].col] = 1; return ; } mid = (tree[k].l+tree[k].r)/2; L = 2*k; R = L+1; if(tree[k].col){ tree[L].col = tree[R].col = tree[k].col; tree[k].col = 0; } if(left < mid) tree_search(L,left,right,count); if(right > mid) tree_search(R,left,right,count); //下面这种遍历和上面这种是一样的,小作分析就明白了,不过我才发现的说 //if(right <= mid){ //tree_search(L,left,right,count); //} //else if(left >= mid){ // tree_search(R,left,right,count); //} //else{ // tree_search(L,left,mid,count); // tree_search(R,mid,right,count); //} return ; } //void insert_comp(int k) //{ // int L,R; // L = k<<1; R = L+1; // if(tree[k].r-tree[k].l == 1) // return ; // if(tree[k].col){ // tree[L].col = tree[R].col = tree[k].col; // tree[k].col = 0; // } // insert_comp(L); // insert_comp(R); //} // //int count(int left,int right) //{ // int i; // int sum = 0; // memset(color,0,sizeof(color)); // for(i = left;i < right;i++){ // color[p[i]->col] = 1; // } // for(i = 1;i <= 30;i++){ // if(color[i]) // sum++; // } // return sum; //} int main() { int l; int t; int o; int a,b,c; int count; char ch; while(scanf("%d %d %d",&l,&t,&o) == 3){ buid_tree(1,1,l+1); while(o--){ getchar(); scanf("%c",&ch); if(ch == 'C'){ scanf("%d %d %d",&a,&b,&c); if(a > b) {Exchange(a,b);} tree_insert(1,a,b+1,c); } if(ch == 'P'){ scanf("%d %d",&a,&b); /*insert_comp(1);*/ if(a > b) {Exchange(a,b);} count = 0; memset(color,0,sizeof(color)); tree_search(1,a,b+1,count); printf("%d/n",count); } } } return 0; }