个人心得:线段树的延迟标志确实是减少了很多时间,思想比较简单,但是实现得时候和建立延迟的时候比较麻烦。

按照我的一些理解,就是更新时找到完全覆盖的区间时,更新延迟标志,不再往下更新,但此时父节点啥的都会更新,但是

递归思想到了这里还是会回去,所以在程序末尾进行往上更新就好了,同时,在查询的时候有延迟标志时要下放,

但是注意此时不会影响父节点的值,因为在更新延迟标志的时候就已经回溯把父节点更新了。

题目:

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
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
#define inf 1<<29
#define nu 4000005
#define maxnum 1000000
int n,k;
int maxelem;
int book[];
int flag=;
typedef struct
{
int left,right;
int sum;
int jf;
int mid(){
return (left+right)/;
} }Tree;
Tree tree[nu];
void buildtree(int root,int l,int r){
tree[root].left=l,tree[root].right=r;
tree[root].sum=tree[root].jf=;
if(l!=r){
buildtree(root*+,l,(l+r)/);
buildtree(root*+,(l+r)/+,r);
}
}
void updown(int root)
{ tree[root*+].jf+=tree[root].jf;
tree[root*+].jf+=tree[root].jf;
tree[root*+].sum+=tree[root].jf;
tree[root*+].sum+=tree[root].jf;
tree[root].jf=;
}
void upset(int root)
{
tree[root].sum=max(tree[root*+].sum,tree[root*+].sum);
}
int checktree(int root,int l,int r)
{
if(tree[root].left==l&&tree[root].right==r)
{
return tree[root].sum;
}
int mid=tree[root].mid();
if(tree[root].jf) updown(root);
if(r<=mid)
return checktree(root*+,l,r);
else if(l>mid)
return checktree(root*+,l,r);
else
{
return max(checktree(root*+,l,mid),checktree(root*+,mid+,r));
}
}
void inserttree(int root,int l,int r){
if(tree[root].left==l&&tree[root].right==r)
{
tree[root].jf+=;
tree[root].sum+=;
return ;
}
int mid=tree[root].mid();
if(tree[root].jf) updown(root);
if(r<=mid)
inserttree(root*+,l,r);
else if(l>mid)
inserttree(root*+,l,r);
else
{
inserttree(root*+,l,mid);
inserttree(root*+,mid+,r);
}
upset(root);
}
int main()
{
int t,j;
scanf("%d",&t);
for(j=;j<=t;j++){
scanf("%d%d",&k,&n);
buildtree(,,maxnum);
flag=;
memset(book,,sizeof(book));
for(int i=;i<=n;i++){
int a,b;
scanf("%d%d",&a,&b);
b--;
if(checktree(,a,b)<k){
book[flag++]=i;
inserttree(,a,b);
}
}
printf("Case %d:\n",j);
for(int p=;p<flag;p++)
{
printf("%d ",book[p]);
}
printf("\n\n");
}
return ;
}
05-13 10:20