【JZOJ5623】program

Description
Description

Input
Input

Output
Output

Sample

Input:
10 5
8>6<2<>54<
4 7
1 10
4 4
2 9
8 10
Output:
0 0 0 0 0 0 0 0 0 0
4 4 4 3 3 2 1 1 1 0
0 0 0 0 0 0 0 0 0 0
2 2 2 1 2 2 1 0 0 0
0 0 0 1 2 1 0 0 0 0

Data Constraint
Output


Analysis

假设从第一个位置开始走,走回第一处立刻折返,走到n位置,有一个很长的路径
那么在其中一个区间 [ l , r ] 中走,它只对应这条路径中的一个子段
那只用记录从1开始走第i个时间点,0~9的状态,第一次走到i的时间f[i],第一次从i走到之前的时间 g [ i ]
直接模拟即可

#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 1200000

using namespace std;

int f[N],g[N],num[N][10],n,q,dir=1,nxt[N];
struct link{int num,las,nex;}a[N];

void del(int x){
    if(!x)return;
    a[a[x].las].nex=a[x].nex;a[a[x].nex].las=a[x].las;
}

int main(){
    scanf("%d %d\n",&n,&q);a[0].nex=1;nxt[0]=1;
    for(int i=1;i<=n;i++){
        char c=getchar();nxt[i]=a[i].nex=i+1;a[i].las=i-1;
        if(c>='0' && c<='9')a[i].num=c-'0';else a[i].num=(c=='<')?10:11;
    }f[0]=1;
    for(int i=1,t=2,las=0;i<=n+1;++t,i=dir?a[i].nex:a[i].las){
        if(i==418){
            int yyy=1;
            yyy=0;
        }
        if(!f[i])f[i]=t;
        for(int j=nxt[i];j<=a[i].nex && f[j];j=nxt[j]){
            g[j]=t;nxt[i]=nxt[j];
        }
        for(int j=0;j<10;j++)num[t][j]=num[t-1][j];
        if(i>n)break;
        if(i==0){
            if(a[las].num>9)del(las);dir=1;las=0;
        }else
        if(a[i].num>9){
            if(a[las].num>9)del(las);dir=a[i].num-10;las=i;
        }else{
            las=0;
            if(a[i].num==0){
                del(i);++num[t][0];
            }else ++num[t][a[i].num--];
        }
    }
    for(int i=1;i<=q;i++){
        int l,r;scanf("%d %d",&l,&r);
        int h=f[l]-1,t=min((g[l]?g[l]:n*23),f[r+1])-1;
        for(int j=0;j<10;j++)printf("%d ",num[t][j]-num[h][j]);printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/white_elephant/article/details/80341762