Fast Arrangement (线段树,延迟标志)
Posted 余生漫漫浪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Fast Arrangement (线段树,延迟标志)相关的知识,希望对你有一定的参考价值。
个人心得:线段树的延迟标志确实是减少了很多时间,思想比较简单,但是实现得时候和建立延迟的时候比较麻烦。
按照我的一些理解,就是更新时找到完全覆盖的区间时,更新延迟标志,不再往下更新,但此时父节点啥的都会更新,但是
递归思想到了这里还是会回去,所以在程序末尾进行往上更新就好了,同时,在查询的时候有延迟标志时要下放,
但是注意此时不会影响父节点的值,因为在更新延迟标志的时候就已经回溯把父节点更新了。
题目:
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 (线段树,延迟标志)的主要内容,如果未能解决你的问题,请参考以下文章
[uva11992]Fast Matrix Operations(多延迟标记,二维线段树,区间更新)