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. Oracle数据库创建表ID字段的自动递增

    转载地址:http://blog.itpub.net/22880668/viewspace-1117343/ 将表t_uaer的字段ID设置为自增:(用序列sequence的方法来实现) ----创建 ...

  2. C#图片增加水印

    给图片增加水印 1.引用 using System.Drawing; 2.代码实现 string ImagePath = @"C:\Users\RAPOO\Pictures\Camera R ...

  3. Django学习笔记之Queryset详解

    Django ORM用到三个类:Manager.QuerySet.Model.Manager定义表级方法(表级方法就是影响一条或多条记录的方法),我们可以以models.Manager为父类,定义自己 ...

  4. IP查找所属网段

    最近同学接到阿里面试题 package io.guangsoft.analysis; /* 数据文件: 1.1.1.0/24,123 1.1.2.0/28,345 1.2.0.0/16,789 */ ...

  5. feather mac 问题小结

    feater 依赖php及jdk 1.自带的php没有cgi ,索性直接装个新的 修改环境变量,并使其生效,验证方式是 打印版本信息: php -v PHP 7.1.13 (cli) (built: ...

  6. xtrabackup备份脚本

    背景:现网环境全备份脚本:基于xtrabackup命令 #!/bin/sh # MySQL端口 PORT=' # 备份用户 USER='bkpuser' PAWD='bkpuser' Time=`da ...

  7. 20135320赵瀚青LINUX内核分析第四周学习笔记

    赵瀚青原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 概述 本周的内容主要是讲解系 ...

  8. Ubuntu 下安装Beyond Compare【转】

    本文转载自:https://blog.csdn.net/bingyu9875/article/details/52856675 官网下载安装包:http://www.scootersoftware.c ...

  9. SQL Server-深入剖析统计信息

    转自: http://www.cnblogs.com/zhijianliutang/p/4190669.html   概念理解 关于SQL Server中的统计信息,在联机丛书中是这样解释的 查询优化 ...

  10. QT 样式表实例

    目标:实现button的圆角效果及背景颜色,鼠标滑过颜色变亮,鼠标点击颜色变重. 总体思路首,先根据需要及样式规则新建.qss文件,然后在代码中将文件引用并应用样式. 具体过程如下: 1在项目当前目录 ...