1. 程式人生 > >Fast Arrangement (線段樹,延遲標誌)

Fast Arrangement (線段樹,延遲標誌)

down station its pan tick 完全 ket one ger

個人心得:線段樹的延遲標誌確實是減少了很多時間,思想比較簡單,但是實現得時候和建立延遲的時候比較麻煩。

按照我的一些理解,就是更新時找到完全覆蓋的區間時,更新延遲標誌,不再往下更新,但此時父節點啥的都會更新,但是

遞歸思想到了這裏還是會回去,所以在程序末尾進行往上更新就好了,同時,在查詢的時候有延遲標誌時要下放,

但是註意此時不會影響父節點的值,因為在更新延遲標誌的時候就已經回溯把父節點更新了。

題目:

Chinese always have the railway tickets problem because of its‘ huge amount of passangers and stations. Now goverment need you to develop a new tickets query system.
One train can just take k passangers. And each passanger can just buy one ticket from station a to station b. Each train cannot take more passangers any time. The one who buy the ticket earlier which can be sold will always get the ticket.

InputThe input contains servel test cases. The first line is the case number. In each test case:
The first line contains just one number k( 1 ≤ k ≤ 1000 ) and Q( 1 ≤ Q ≤ 100000 )
The following lines, each line contains two integers a and b, ( 1 ≤ a < b ≤ 1000000 ), indicate a query.
Huge Input, scanf recommanded.OutputFor each test case, output three lines:
Output the case number in the first line.
If the ith query can be satisfied, output i. i starting from 1. output an blank-space after each number.
Output a blank line after each test case.Sample Input

1
3 6
1 6
1 6
3 4
1 5
1 2
2 4

Sample Output

Case 1:
1 2 3 5
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<iomanip>
  6 #include<algorithm>
  7 using namespace std;
  8 #define inf 1<<29
  9 #define nu 4000005
 10
#define maxnum 1000000 11 int n,k; 12 int maxelem; 13 int book[100005]; 14 int flag=0; 15 typedef struct 16 { 17 int left,right; 18 int sum; 19 int jf; 20 int mid(){ 21 return (left+right)/2; 22 } 23 24 }Tree; 25 Tree tree[nu]; 26 void buildtree(int root,int l,int r){
27 tree[root].left=l,tree[root].right=r; 28 tree[root].sum=tree[root].jf=0; 29 if(l!=r){ 30 buildtree(root*2+1,l,(l+r)/2); 31 buildtree(root*2+2,(l+r)/2+1,r); 32 } 33 } 34 void updown(int root) 35 { 36 37 tree[root*2+1].jf+=tree[root].jf; 38 tree[root*2+2].jf+=tree[root].jf; 39 tree[root*2+1].sum+=tree[root].jf; 40 tree[root*2+2].sum+=tree[root].jf; 41 tree[root].jf=0; 42 } 43 void upset(int root) 44 { 45 tree[root].sum=max(tree[root*2+1].sum,tree[root*2+2].sum); 46 } 47 int checktree(int root,int l,int r) 48 { 49 if(tree[root].left==l&&tree[root].right==r) 50 { 51 return tree[root].sum; 52 } 53 int mid=tree[root].mid(); 54 if(tree[root].jf) updown(root); 55 if(r<=mid) 56 return checktree(root*2+1,l,r); 57 else if(l>mid) 58 return checktree(root*2+2,l,r); 59 else 60 { 61 return max(checktree(root*2+1,l,mid),checktree(root*2+2,mid+1,r)); 62 } 63 } 64 void inserttree(int root,int l,int r){ 65 if(tree[root].left==l&&tree[root].right==r) 66 { 67 tree[root].jf+=1; 68 tree[root].sum+=1; 69 return ; 70 } 71 int mid=tree[root].mid(); 72 if(tree[root].jf) updown(root); 73 if(r<=mid) 74 inserttree(root*2+1,l,r); 75 else if(l>mid) 76 inserttree(root*2+2,l,r); 77 else 78 { 79 inserttree(root*2+1,l,mid); 80 inserttree(root*2+2,mid+1,r); 81 } 82 upset(root); 83 } 84 int main() 85 { 86 int t,j; 87 scanf("%d",&t); 88 for(j=1;j<=t;j++){ 89 scanf("%d%d",&k,&n); 90 buildtree(0,1,maxnum); 91 flag=0; 92 memset(book,0,sizeof(book)); 93 for(int i=1;i<=n;i++){ 94 int a,b; 95 scanf("%d%d",&a,&b); 96 b--; 97 if(checktree(0,a,b)<k){ 98 book[flag++]=i; 99 inserttree(0,a,b); 100 } 101 } 102 printf("Case %d:\n",j); 103 for(int p=0;p<flag;p++) 104 { 105 printf("%d ",book[p]); 106 } 107 printf("\n\n"); 108 } 109 return 0; 110 }



Fast Arrangement (線段樹,延遲標誌)