牛客网暑期ACM多校训练营(第六场)I-Team Rocket

链接:https://www.nowcoder.com/acm/contest/144/I
来源:牛客网
 

Team Rocket

时间限制:C/C++ 4秒,其他语言8秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

There are n trains running between Kanto and Johto region. Assuming the railway is a number line, the i-th train travels from coordinate li to coordinate ri (both inclusive).

One day, m Team Rocket members invaded the railway system successfully. The i-th Team Rocket member was going to destroy the transportation hub with coordinate xi. Once a transportation hub within the driving range of a train is destroyed, the train's itinerary will be canceled immediately.

Giovanni wants to know how many train trips will be firstly canceled after each attack.

After all the attacks finished, for each train Giovanni needs to know that in which attack its itinerary was firstly canceled, or it was unaffected at all.

输入描述:

The input starts with one line containing exactly one integer T, which is the number of test cases.

For each test case, the first line contains two integers n and m, indicating the number of trains and the number of Team Rocket members.

Each of the next n lines contains 2 integers li and ri, indicating the driving range of the i-th train.

Each of the next m lines contains exactly one integer yi. , where xi is the transportation hub that Team Rocket members would destroy in the i-th attack, resi-1 is the product of the indexes of trips cancelled by the (i-1)-th attack and  means exclusive or. 

If no such trip exists, resi-1 is considered to be 0.

- 1 ≤ T ≤ 5.
- 1 ≤ n,m ≤ 2 x 105.
- -109 ≤ li ≤ ri ≤ 109.
- -109 ≤ xi ≤ 109.

输出描述:

For each test case, output one line "Case #x:" first, where x is the test case number (starting from 1).

Then output m lines, each line of which contains exactly one integer, indicating the number of train trips firstly canceled after the i-th attack.

Finally output one line, containing n integers, where the i-th integer is the time when the i-th train trip is firstly canceled or 0 if it is not affected.

 

示例1

输入

1
3 4
1 3
2 6
-999 1000000000
-1000
1
5
2

输出

Case #1:
0
2
1
0
2 3 2

 

题意:给你n个一维的区间两端坐标和m个炸弹,每个炸弹会炸在一个具体xi上,如果该段有未被炸毁的区间则该区间被炸毁(数量不限),被炸毁的区间则不会被其他炸弹炸毁,输出每个炸弹炸了几个区间(要输出yi 即与上一次炸完的个数对当前炸毁的个数进行异或操作),被个区间第一次被第几个炸弹炸毁(若未被炸毁则输出0)

 

做法:先将左端点排序,建立线段树,每个结点要保存当前维护范围内的最大区间右端点的值,当输入炸弹的坐标xi时,将左端点大于xi的第一个点找出,在其左边寻找点,若有右端点大于xi的,则记录并把最大值变为一个很小的值,则该区间不会在之后的查找中再出现,大大降低复杂度。


代码如下:

#include<bits/stdc++.h>
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int mod=998244353;
const int maxn=200005;
ll res,ans[maxn],cnt,nowid;
int n,m;
struct node{
    int l,r,id;
    node(){}
    node(int l,int r,int id):l(l),r(r),id(id){}
    bool operator < (const node &a) const {
        return l<a.l;
    }
}a[maxn];
struct Tree{
    int l,r,pos,big;
}e[maxn<<2];
void pushup(int rt){
    e[rt].big=max(e[lson].big,e[rson].big);
}
void build(int l,int r,int rt){
    e[rt].l=l,e[rt].r=r;
    if(l==r){
        e[rt].pos=a[l].id;
        e[rt].big=a[l].r;
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,lson);
    build(mid+1,r,rson);
    pushup(rt);
}
void update(int l,int r,int nowl,int R,int rt){
    if(e[rt].big<nowl||l>r) return ;
    if(l==r){
        res=((ll)res*e[rt].pos)%mod;
        ans[e[rt].pos]=nowid;
        e[rt].big=-inf;
        cnt++;
        return ;
    }
    int mid=(l+r)>>1;
    update(l,mid,nowl,R,lson);
    if(mid<R) update(mid+1,r,nowl,R,rson);
    pushup(rt);
}
int main(){
    int t,k,cas=0;
    cin>>t;
    while(t--){
        memset(ans,0,sizeof(ans));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%d%d",&a[i].l,&a[i].r);
            a[i].id=i;
        }
        sort(a+1,a+1+n);
        build(1,n,1);
        printf("Case #%d:\n",++cas);
        res=0;
        for(int i=1;i<=m;i++){
            scanf("%d",&k);
            k=k^res;
            nowid=i,res=1,cnt=0;
            int nowpos=upper_bound(a+1,a+1+n,node(k,0,0))-a-1;
            if(nowpos) update(1,n,k,nowpos,1);
            if(!cnt) res=0;
            printf("%d\n",cnt);
        }
        for(int i=1;i<=n;i++){
            printf("%d%c",ans[i],i==n?'\n':' ');
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值