Nlogonia is fighting a ruthless war against the neighboring country of Cubiconia. The Chief General of Nlogonia's Army decided to attack the enemy with a linear formation of soldiers, that would advance together until conquering the neighboring country. Before the battle, the Chief General ordered that each soldier in the attack line, besides protecting himself and attacking, should also protect his two (nearest) neighbors in the line, one to his left and one to his right. The Chief General told the soldiers that for each of them, his ``buddies" would be these two neighbors, if such neighbors existed (because the leftmost soldier does not have a left neighbor and the rightmost soldier does not have a right neighbor). The Chief General also told the soldiers that protecting their buddies was very important to prevent the attack line from being broken. So important that, if the left or right buddy of a soldier is killed, then the next living neighbor to the left or to the right of the soldier, respectively, should become his buddy.

The battle is fierce, and many soldiers in the attack line are being killed by fire shots, grenades and bombs. But following the Chief General's orders, immediately after knowing about losses in the attack line, the Army's information systems division has to inform the soldiers who their new buddies are.

You are given the number of soldiers in the attack line, and a sequence of loss reports. Each loss report describes a group of contiguous soldiers in the attack line that were just killed in the battle. Write a program that, for each loss report, prints the new buddies formed.

Input

Each test case is described using several lines. The first input line contains two integers S and Brepresenting respectively the number of soldiers in the attack line, and the number of loss reports ( 1BS105). Soldiers are identified by different integers from 1 to S, according to their positions in the attack line, being 1 the leftmost soldier and S the rightmost soldier. Each of the next B input lines describes a loss report using two integers L (left) and R (right), meaning that soldiers from L to R were killed ( 1LRS). You may assume that until that moment those soldiers were alive and were just killed.

The last test case is followed by a line containing two zeros.

Output

For each test case output B + 1 lines. In the i-th output line write the new buddies formed by removing from the attack line the soldiers that were just killed according to the i-th loss report. That is, for the loss report `L R', print the first surviving soldier to the left of L, and the first surviving soldier to the right of R. For each direction, print the character `*' (asterisk) if there is no surviving soldier in that direction. Print a line containing a single character `-' (hyphen) after each test case.

Sample Input

1 1
1 1
10 4
2 5
6 9
1 1
10 10
5 1
1 1
0 0

Sample Output

* *
-
1 6
1 10
* 10
* *
-
* 2
-
这道题一开始想用线段树来做,然后改成了树状数组,结果发现就不应该用树状数组做,那个悬在超时边上
经验教训:不可以用dat[k]!=0判断第k位置的人没有被杀,这个值可能是比它小的人贡献的
思路: 每个位置都是1,被杀了就变成0,因为每个人只杀一次,所以顶多被杀10^5次,这个即使按人更新都可以承受,所以就干脆分开从L到R的闭区间的每个点杀了都更新成0;对于一整个区间,因为都是0,所以sum(0,i)(i属于区间中的下标)都是相等的;而左边的没被杀的人恰好是等于这个值的第一个序号,右边没被杀的人是等于这个值+1的第一个序号,用来求sum(0,i)当然是树状数组
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e5+2;
int s,b;
int dat[maxn<<1];
int lowbit(int k ){
return k&(-k);
}
void update(int k,int a){
if(k<0||dat[k]+a<0)return ;
while(k<=s){
dat[k]+=a;
k+=lowbit(k);
}
}
int query(int k){
int ans=0;
while(k>0){
ans+=dat[k];
k-=lowbit(k);
}
return ans;
}
int getk(int k){//找到和恰为k的第一个点,返回值小于等于n,大于等于1
if(k<0)return 0;
int ans=0,cnt=0;
for(int i=25;i>=0;i--){
ans+=(1<<i);
if(ans>=s||cnt+dat[ans]>=k)ans-=(1<<i);
else cnt+=dat[ans];
}
return ans+1;
}
int main(){
while(scanf("%d%d",&s,&b)==2&&s&&b){
//for(int i=1;i<=s;i++)update(i,-1);这个不行
     memset(dat,0,sizeof(int)*2*n);
   for(int i=1;i<=s;i++)update(i,1);//初始化
   for(int i=0;i<b;i++){
   int l,r;
   scanf("%d%d",&l,&r);
   for(int i=l;i<=r;i++)update(i,-1);//减人
   int tmp=query(r);//查询这个区间前面剩余人数
   int pos1=getk(tmp);//找到左端点
   int pos2=getk(tmp+1);//找到右端点
       if(query(pos2)!=tmp+1)pos2++;//因为这个算法不会超过n,为了判断最后一个人是否存在
   if(query(pos1)==0)putchar('*');
  else printf("%d",pos1);
   putchar(' ');
   if(pos2>s)putchar('*');
   else printf("%d",pos2);
  putchar('\n');
   }
   puts("-");//分割case
}
return 0;
}

  

uva 12356 Army Buddies 树状数组解法 树状数组求加和恰为k的最小项号 难度:1的更多相关文章

  1. uva 12356 Army Buddies

    简单的并查集应用. #include<stdio.h> #include<string.h> #include<math.h> #include<algori ...

  2. Permutation UVA - 11525(值域树状数组,树状数组区间第k大(离线),log方,log)(值域线段树第k大)

    Permutation UVA - 11525 看康托展开 题目给出的式子(n=s[1]*(k-1)!+s[2]*(k-2)!+...+s[k]*0!)非常像逆康托展开(将n个数的所有排列按字典序排序 ...

  3. 差分+树状数组 线段树【P2357】 守墓人

    题目描述-->p2357 守墓人 敲了一遍线段树,水过. 树状数组分析 主要思路: 差分 简单介绍一下差分(详细概念太麻烦,看下面. 给定一个数组 7 8 6 5 1 8 18 20 35 // ...

  4. hdu 1166 树状数组(线段树)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. [bzoj3196][Tyvj1730]二逼平衡树_树套树_位置线段树套非旋转Treap/树状数组套主席树/权值线段树套位置线段树

    二逼平衡树 bzoj-3196 Tyvj-1730 题目大意:请写出一个维护序列的数据结构支持:查询给定权值排名:查询区间k小值:单点修改:查询区间内定值前驱:查询区间内定值后继. 注释:$1\le ...

  6. P1972 [SDOI2009]HH的项链[离线+树状数组/主席树/分块/模拟]

    题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链 ...

  7. 【BZOJ-1452】Count 树状数组 套 树状数组

    1452: [JSOI2009]Count Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1769  Solved: 1059[Submit][Stat ...

  8. hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. 【BZOJ】1146: [CTSC2008]网络管理Network(树链剖分+线段树套平衡树+二分 / dfs序+树状数组+主席树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1146 第一种做法(时间太感人): 第二种做法(rank5,好开心) ================ ...

随机推荐

  1. python3.5实现购物车

    一.购物车实现: 购物车功能: 用户登录:密码错误三次锁定账户. 商品列表分页显示:输入页码查看指定页数商品信息. 已购买商品列表:显示已购买的物品列表:可以模糊查询已购买的商品并在终端打印. 充值: ...

  2. IOS开发-数据库总结

    关于数据存储概念: 数据结构: 基本对象:NSDictionary.NSArray和NSSet这些对象. 复杂对象:关系模型.对象图和属性列表多种结构等. 存储方式: 内存:内存存储是临时的,运行时有 ...

  3. spark[源码]-DAG调度器源码分析[二]

    前言 根据图片上的结构划分我们不难发现当rdd触发action操作之后,会调用SparkContext的runJob方法,最后调用的DAGScheduler.handleJobSubmitted方法完 ...

  4. 数据库中的B树和B+树

    B树与B+树 数据库中建立索引能加快数据的存取,但是当索引变得很大时,可能导致内存装不下.这时就需要使用多级索引来实现.而B树和B+树是实现多级索引的一种数据结构. B树 B树是多叉树,其树中每个节点 ...

  5. ELK日志系统

    ELK stack是又Elasticsearch,lostash,kibana 三个开源软件的组合而成,形成一款强大的实时日志收集分析展示系统. Logstash:日志收集工具,可以从本地磁盘,网络服 ...

  6. vux在ISO中异常 this.$vux.confirm.show

    在按钮事件中调用this.$vux.confirm.show,并且启用按钮的show-loading属性 安卓正常,ios中弹窗无法显示 经过排查,iso中设置按钮的loading后,要用异步setT ...

  7. NUMA架构的优缺点

    numa把一台计算机分成多个节点(node),每个节点内部拥有多个CPU,节点内部使用共有的内存控制器,节点之间是通过互联模块进行连接和信息交互.因此节点的所有内存对于本节点所有的CPU都是等同的,对 ...

  8. JS的魅力

    一.初探JavaScript魅力 基本知识: JavaScript是什么 网页特效原理 -JavaScript就是修改样式 编写JS流程 - 布局:HTML + CSS - 属性:确定修改哪些属性 - ...

  9. 如何配置IIS服务器?

    1, 先安装IIS 然后安装vs; 注: 顺序颠倒则执行cmd命令: 1,cd \ 2,cd Windows 3, cd  Microsoft.NET 4, dir 5,cd Framework 6, ...

  10. SQL学习笔记之SQL中INNER、LEFT、RIGHT JOIN的区别和用法详解

    0x00 建表准备 相信很多人在刚开始使用数据库的INNER JOIN.LEFT JOIN和RIGHT JOIN时,都不太能明确区分和正确使用这三种JOIN操作,本文通过一个简单的例子通俗易懂的讲解这 ...