线段树区间更新 Fast Arrangement

Description

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.
 

Input

The 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.
 

Output

For 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.有人下车的时候,有人上车,这种情况下要先减去下车,再加上上车的,看有没有超载,所以b=b-1,让这个人在”入站“时下车
2.注意pushdown,如果前面这个车上有多少人上了,lazy,f 要push下去
3.pushup 要进行选择最大的操作,在这个乘客要求的路线区间中,前一个顾客已经“预定”了,这个人也上不去
 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int N=1e6+5;
int f[N<<2],lazy[N<<2];
int k,m,a,b;

void pushdown(int root)
{
int rt=root<<1;

if(lazy[root])
{
lazy[rt] += lazy[root];
lazy[rt+1] += lazy[root];

f[rt] += lazy[root];
f[rt+1] += lazy[root];

lazy[root] = 0;
}
}

void pushup(int root)
{
int rt=root<<1;

f[root] = max(f[rt],f[rt+1]);
}

void build(int root,int left,int right)
{
lazy[root] = 0;
f[root] = 0;

if(left==right)
{
return ;
}

int mid = (left+right)/2;
int rt=root<<1;

build(rt,left,mid);
build(rt+1,mid+1,right);

pushup(root);

}

void update(int root,int left,int right)
{
if(a<=left&&b>=right)
{
lazy[root]++;
f[root] ++;
return ;
}

pushdown(root);

int rt=root<<1;
int mid=(left+right)/2;

if(a<=mid){
update(rt,left,mid);
}

if(b>mid){
update(rt+1,mid+1,right);
}

pushup(root);
}


int query(int qleft,int qright,int root,int left,int right)
{
if(qleft<=left&&qright>=right)
{
return f[root];
}

int mid = (left+right)/2;
int ans = 0;
int rt=root<<1;

pushdown(root);

if(mid>=qleft)
{
ans = max(ans,query(qleft,qright,rt,left,mid));
}

if(mid<qright)
{
ans = max(ans,query(qleft,qright,rt+1,mid+1,right));
}
return ans;
}


int main()
{

int t=0;
int T,Case=1;
scanf("%d",&T);
while(T--)
{

scanf("%d %d",&k,&m);
build(1,1,1000000);

int n =0;

printf("Case %d:\n",Case++);

for(int i=1;i<=m;i++)
{
scanf("%d %d",&a,&b);

b=b-1;

t=query(a,b,1,1,1000000);

if(t<k)
{
update(1,1,1000000);
printf("%d ",i);
}
}

printf("\n\n");
}
}

 

 
 

 

posted @ 2022-07-13 16:03  killjoyskr  阅读(24)  评论(0)    收藏  举报