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

题解:线段树区间最大值问题,判断该区间最大值是否大于等于K,判断是否可以乘坐

AC代码为:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=1e6+10;
int k,q;

struct node{
int l,r,num,tag;
} tree[maxn<<2];

int max(int a,int b)
{
return a>b? a:b;
}

void build(int k,int l,int r)
{
tree[k].l=l,tree[k].r=r;
tree[k].num=0,tree[k].tag=0;
if(l==r) return ;
int mid=(l+r)>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
}

void pushup(int k)
{
tree[k].num=max(tree[k<<1].num,tree[k<<1|1].num);
}

void pushdown(int k)
{
tree[k<<1].tag+=tree[k].tag;
tree[k<<1|1].tag+=tree[k].tag;
tree[k<<1].num+=tree[k].tag;
tree[k<<1|1].num+=tree[k].tag;
tree[k].tag=0;
}

void update(int k,int l,int r,int x)
{
if(tree[k].l==l && tree[k].r==r)
{
tree[k].num+=x;
tree[k].tag+=x;
return ;
}
if(tree[k].tag) pushdown(k);
int mid=(tree[k].r+tree[k].l)>>1;
if(r<=mid) update(k<<1,l,r,x);
else if(l>=mid+1) update(k<<1|1,l,r,x);
else
update(k<<1,l,mid,x),update(k<<1|1,mid+1,r,x);
pushup(k);
}

int query(int k,int l,int r)
{
if(tree[k].l==l&&tree[k].r==r)
return tree[k].num;
if(tree[k].tag) pushdown(k);
int mid=(tree[k].l+tree[k].r)>>1;
if(r<=mid) return query(k<<1,l,r);
else if(l>=mid+1) return query(k<<1|1,l,r);
else
return max(query(k<<1,l,mid),query(k<<1|1,mid+1,r));
}

int main()
{
int t,x,y,len=0,flag[maxn],cas=1;
scanf("%d",&t);
while(t--)
{
len=0;
memset(flag,0,sizeof(flag));
scanf("%d%d",&k,&q);
build(1,1,1000000);

for(int i=1;i<=q;i++)
{
scanf("%d%d",&x,&y);
y--;
if(query(1,x,y)<k) 
{
flag[len++]=i;
update(1,x,y,1);
}      
}
printf("Case %d:\n",cas++);
for(int i=0;i<len;i++)
printf("%d ",flag[i]);
printf("\n\n");
}

return 0; 
}

最新文章

  1. /var/log/messages
  2. 偷懒小工具 - SSO单点登录通用类(可跨域)
  3. AMD and CMD are dead之KMDjs内核之依赖分析
  4. String PK StringBuilder,传说就是传说,只有动手实验,才能得出确定的答案
  5. Java 导出EXCEL
  6. 用c#开发微信 (12) 微统计 - 阅读分享统计系统 2 业务逻辑实现
  7. 烂泥:KVM虚拟机的关机与开启
  8. eclipse workspace 共享配置文件
  9. Struts中&lt;s:checkboxlist&gt;的用法
  10. uva 825
  11. Apache CXF 例子
  12. DDD的好文章
  13. Scala函数字面量简化写法
  14. TDirectory.Copy复制文件
  15. Eclipse用法和技巧八:自动添加try/catch块1
  16. 3个人一起写的EI论文可以检索到啦~ --&gt; Exploring the use of a 3D Virtual Environment in Chinese Cultural Transmission
  17. SmartSql 介绍
  18. memory_target
  19. 关于ie6出现的问题的原因归结
  20. 微信小程序学习资料整理

热门文章

  1. Laravel 5 - 文件上传
  2. [HTML] 学HTML写的第一第二个网页
  3. VS 使用 :新建项目
  4. 力扣(LeetCode)按奇偶排序数组II 个人题解
  5. Yum —— CentOS 下包管理工具 学习笔记
  6. IE6下CSS常见兼容性问题及解决方案
  7. Three.js - 走进3D的奇妙世界
  8. odoo12 修行基础篇之 添加明细字段 (二)
  9. 【集训Day2】字符串
  10. Linux I/O复用 —— epoll 部分源码剖析