牛客多校第6场I题 Team Rocket

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

题目描述

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

一开始的思路是将x 保存下来,然后去离线的处理,但是TMD竟然是强制在线,然后就菜的不会做了,然后去看了看别人的博客,发现可以通过排序+线段树 来写,但是看了思路之后第一个蹦出来的实现方法竟然是并查集去写,然后用并查集写了一发,改到最后80% 竟然T了。可能有那种一个也炸不到,但是会不断地扫数组的数据,所以并查集才会T,然后就写了发线段树过的。

代码:

#include<bits/stdc++.h>
#define lson (i<<1)
#define rson (i<<1|1)

using namespace std;
typedef long long ll;
const int N =2e5+5;
const ll inf=1e18+5;
const ll mod=998244353;

struct node
{
    ll l,r;
    ll maxr;
    int inde;
    int be;
}a[N],tr[N<<2];
int tot;
ll cnt;
ll res;
ll L[N];

void push_up(int i)
{
    tr[i].maxr=max(tr[lson].maxr,tr[rson].maxr);
}

void build(int i,int l,int r)
{
    tr[i].l=l; tr[i].r=r; tr[i].be=0;
    if(l==r){
        tr[i].maxr=a[l].r;
        tr[i].inde=a[l].inde;
        return ;
    }
    int mid=(l+r)>>1;
    build(lson,l,mid);
    build(rson,mid+1,r);
    push_up(i);
}

void query(int i,int l,int r,ll x)
{
    //cout<<"*** "<<tr[i].l<<" "<<tr[i].r<<" "<<tr[i].maxr<<endl;
    if(tr[i].maxr<x) return ;
    if(tr[i].l==l&&tr[i].r==r&&l==r){
        //cout<<"lalalla"<<endl;
        tr[i].maxr=-inf;
        cnt++;
        ll tmp=1ll*a[l].inde;
        res=res*tmp%mod;
        a[l].be=tot;
        return ;
    }
    int mid=(tr[i].l+tr[i].r)>>1;
    if(r<=mid){
        //cout<<"lson "<<endl;
        query(lson,l,r,x);
    }
    else if(l>mid){
        //cout<<"rson "<<endl;
        query(rson,l,r,x);
    }
    else{
        //cout<<"lson rson"<<endl;
        query(lson,l,mid,x);
        query(rson,mid+1,r,x);
    }
    push_up(i);
}

bool cmp1(node a,node b)
{
    if(a.l==b.l) return a.r<b.r;
    return a.l<b.l;
}

bool cmp2(node a,node b)
{
    return a.inde<b.inde;
}

int n,m;

int main()
{
    int T;
    scanf("%d",&T);
    int kk=0;
    while(T--)
    {
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%lld %lld",&a[i].l,&a[i].r);
            a[i].inde=i; a[i].be=0;
        }

        sort(a+1,a+n+1,cmp1);
        for(int i=1;i<=n;i++) L[i]=a[i].l;
        build(1,1,n);

        printf("Case #%d:\n",++kk);
        tot=0;
        ll x;
        res=0;
        while(m--){
            ++tot;
            scanf("%lld",&x);
            x=x^res;
            res=1;
            int inde=upper_bound(L+1,L+n+1,x)-L;
            //cout<<"index "<<inde<<endl;
            inde--;
            cnt=0;
            if(inde>=1) query(1,1,inde,x);
            printf("%lld\n",cnt);
            if(cnt==0) res=0;
        }

        sort(a+1,a+n+1,cmp2);
        printf("%d",a[1].be);
        for(int i=2;i<=n;i++) printf(" %d",a[i].be);
        printf("\n");
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值