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. vue父子组件传值加例子

    例子:http://element-cn.eleme.io/#/zh-CN/component/form         上进行改的 父传子:用prop:子组件能够改变父组件的值,是共享的,和父操作是 ...

  2. 47. Permutations II (全排列有重复的元素)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. 62. Unique Paths (走棋盘多少种不同的走法 动态规划)

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  4. hdu6194 string string string

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6194 题目: string string string Time Limit: 2000/10 ...

  5. javascript 理解对象--- 定义多个属性和读取属性的特性

    一 定义多个属性 ECMAScript5 定义了一个Object.defineProperties()方法,用于定义多个属性.此方法接受两个对象参数: 第一个对象:要添加或修改其属性的对象 第二个对象 ...

  6. 服务器抓包命令:tcpdump详解

    官网地址:http://www.tcpdump.org/tcpdump_man.html 简介: tcpdump,就是:dump the traffic on a network,根据使用者的定义对网 ...

  7. saltstack常用模块

    介绍一些常用的saltstack模块,更多模块参考官方网站 1.跟安装包相关的模块:salt.states.pkg salt.states.pkg.downloaded(name, version=N ...

  8. 20145301Java课程总结

    20145301 Java课程总结 每周读书笔记链接汇总 第一周读书笔记: http://www.cnblogs.com/5301z/p/5248888.html 第二周读书笔记: http://ww ...

  9. 20145307第七周JAVA学习报告

    20145307<Java程序设计>第七周学习总结 教材学习内容总结 Lambda Lambda语法概述: Arrays的sort()方法可以用来排序,在使用sort()时,需要操作jav ...

  10. linux 块设备-整理(一)

    1. 基本概念: linux设备驱动开发详解(宋宝华): 字符设备与块设备 I/O 操作的不同如下. (1)块设备只能以块为单位接受输入和返回输出,而字符设备则以字节为单位. 大多数设备是字符设备,因 ...