Atcoder ABC158 F - Removing Robots segment tree is selected from collections + dp

Atcoder ABC158 F - Removing Robots tree line + dp

The meaning of problems

A straight line with a robot, each robot has a traveling activation value D [i], when it is activated, it will go D [i] from the x-axis direction. It will leave after axis. Activated in two ways, one is manually activated, is when the robot travels a distance when the active state [x [i], x [i] + D [i]) Note that the right open interval, met other robot, a robot that was hit will be activated. If you hit something else and it will activate another chain reaction. Q. You can choose any of the robot is activated, the remaining set of robots activated how many

Thinking

This complex chain of physical processes questions movements. 1 is to see there is no similar elastic collision of exchange rate considered to proceed. 2 is to look at the final state can be calculated directly determined. Here Since the robot moves in only one direction, so we can enumerate back to front along the axis. A robot can activate the farthest robot is which? Course [x [i], x [i] + D [i]) can be activated in the robot activated furthest range matter so that it can be a a similar dp calculated. Since most requested interval value, we can look at the presence of L [i] in a segment tree or bit maintenance. This distance is calculated what use is it.

For a robot, it is likely there are two options to take and not take, if you can take it to the farthest robot to affect it must be in a set, that is, i --- L [i] if i chose this had in the collection interval, plus L [i + 1] -N set type, and if it is not selected, that is, i + 1 - N a set of species. That we set dp [i] n represents the number of i --- different collections, then dp [i] = dp [i + 1] + dp [L [i] +1] That answer is dp [1 ] a.

This dp are generally selected from the set provided [i ... N] on the line state transition type, usually divided into two cases i and i no

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define F first
#define S second
#define mkp make_pair
#define pii pair<int,int>
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=2e5+5;
const int mod= 998244353;
ll tree[maxn<<2];
void build(int o,int l,int r){
    tree[o]=0;
    if(l==r){
        return ;
    }
    else {
        int mid=l+r>>1;
        build(o<<1,l,mid);
        build(o<<1|1,mid+1,r);
        tree[o]=max(tree[o<<1],tree[o<<1|1]);
    }
}
ll query(int o,int l,int r,int x,int y){
    if(x<=l&&y>=r){
        return tree[o];
    }
    int mid=l+r>>1;
    ll ans=0;
    if(mid>=x)ans=max(ans,query(o<<1,l,mid,x,y));
    if(mid<y)ans=max(ans,query(o<<1|1,mid+1,r,x,y));
    return ans;
}
void update(int o,int l,int r,int p,ll v){
    if(l==r){
        tree[o]=v;
    }
    else {
        int mid=l+r>>1;
        if(mid>=p)update(o<<1,l,mid,p,v);
        else if(mid<p)update(o<<1|1,mid+1,r,p,v);
        tree[o]=max(tree[o<<1],tree[o<<1|1]);   
    }
}
pair<ll ,ll>a[maxn];
int L[maxn];
ll b[maxn];
ll dp[maxn];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%lld%lld",&a[i].first,&a[i].second);
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)b[i]=a[i].first;
    build(1,1,n);
    for(int i=n;i>=1;i--){
        int tmp=upper_bound(b+1,b+1+n,a[i].first+a[i].second-1)-b;
        if(tmp==i+1)L[i]=i;
        else L[i]=query(1,1,n,i+1,tmp-1);
        update(1,1,n,i,L[i]);
    }
//  for(int i=1;i<=n;i++)cout<<L[i]<<" ";cout<<endl;
    dp[n+1]=1;
    for(int i=n;i>=1;i--){
        dp[i]=dp[i+1]+dp[L[i]+1];
        dp[i]%=mod;
    }
    printf("%lld\n",dp[1]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/ttttttttrx/p/12440545.html