2016 多校2 1008 乱搞

本文介绍了一种处理压缩二进制字符串的有效算法,该算法能够快速判断是否存在子串满足特定的0和1出现次数。通过预处理低界和高界的运行长度,并使用二分查找和栈来优化查询效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
A non-empty string s is called binary, if it consists only of characters “0” and “1”. A substring s[l…r] (1≤l≤r≤|s|) of string s=s1s2…s|s| (where |s| is the length of string s) is string slsl+1…sr.

Professor Zhang has got a long binary string s starting with “0”, and he wants to know whether there is a substring of the string s that the occurrences of “0” and “1” in the substring is exact a and b, respectively, where a and b are two given numbers.

Since the binary string is very long, we will compress it. The compression method is:
1. Split the string to runs of same characters.
2. Any two adjacent runs consist of different characters. Use the length of each run to represent the string.

For example, the runs of a binary string 00101100011110111101001111111 are {00, 1, 0, 11, 000, 1111, 0, 1111, 0, 1, 00, 1111111}, so it will be compressed into {2, 1, 1, 2, 3, 4, 1, 4, 1, 1, 2, 7}.

Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains two integers n and m (1≤n≤1000,1≤m≤5⋅105) - the number of runs and the number of queries. The next line contains n integers: x1,x2,…,xn (1≤xi≤106), indicating the length of the each run.

Each of the following m lines contains two integers ai and bi (0≤ai,bi≤|s|,1≤ai+bi≤|s|), which means Professor Zhang wants to know whether there is a substring of the string s that the occurrences of “0” and “1” in the substring is exact ai and bi.

Output
For each test case, a binary string of length n. The i-th number is “1” if the answer for the i-th query is yes, or “0” otherwise.

比赛的时候没看这题,后来差不多想到了,只是表示矩形区域时我tm居然想用二维树状数组,简直。只需要维护边界就好了。搞一个栈把上下界处理出来,每次二分查找得到上下界判一下就ok。

代码:

#include <bits/stdc++.h>
using namespace std;
struct node {
    int x, y;
};
bool cmpl(node a, node b)
{
    if(a.x!=b.x)return a.x<b.x;
    else return a.y>b.y;
}
bool cmph(node a, node b)
{
    if(a.x!=b.x)return a.x<b.x;
    else return a.y>b.y;
}
int n, m;
int rec[1050];
node low[1000500];
node high[1000500];
node stackh[100500];
node stackl[100500];
int stl, sth;

int posl, posh;
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &n, &m);
        for(int i=1;i<=n;i++)
            scanf("%d", &rec[i]);
        posl=0;
        for(int st=1;st<=n;st+=2)
        {
            int ed=st+2;
            node tmp;
            tmp.x=rec[st];
            tmp.y=0;
            low[++posl]=tmp;
            while(ed<=n)
            {
                tmp.x+=rec[ed];
                tmp.y+=rec[ed-1];
                low[++posl]=tmp;
                ed+=2;
            }
        }
        posh=0;
        for(int st=2;st<=n;st+=2)
        {
            int ed=st+2;
            node tmp;
            tmp.y=rec[st];
            tmp.x=0;
            high[++posh]=tmp;
            while(ed<=n)
            {
                tmp.y+=rec[ed];
                tmp.x+=rec[ed-1];
                high[++posh]=tmp;
                ed+=2;
            }
        }
        sort(low+1, low+1+posl,cmpl);
        sort(high+1, high+1+posh, cmph);
        /*printf("llllll\n");
        for(int i=1;i<=posl;i++)
        {
            printf("%d %d\n", low[i].x, low[i].y);
        }
        printf("hhhhhh\n");
        for(int i=1;i<=posh;i++)
        {
            printf("%d %d\n", high[i].x, high[i].y);
        }*/
        stl=sth=0;
        for(int i=1;i<=posl;i++)
        {
            node tmp=low[i];
            while(stl!=0&&stackl[stl].y>=tmp.y)
            {
                stl--;
            }
            stackl[++stl]=tmp;
        }
        for(int i=posh;i>=1;i--)
        {
            node tmp=high[i];
            while(sth!=0&&stackh[sth].y<=tmp.y)
            {
                sth--;
            }
            stackh[++sth]=tmp;
        }
        for(int i=1;i<=sth/2;i++)
        {
            swap(stackh[i], stackh[sth-i+1]);
        }
        /*printf("llllll\n");
        for(int i=1;i<=stl;i++)
        {
            printf("%d %d\n", stackl[i].x, stackl[i].y);
        }
        printf("hhhhhh\n");
        for(int i=1;i<=sth;i++)
        {
            printf("%d %d\n", stackh[i].x, stackh[i].y);
        }*/
        for(int i=1;i<=m;i++)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            if(stackl[stl].x<x){
                printf("0");
                continue;
            }
            int tagl, tagh;
            int l=1, r=stl;
            while(l<=r)
            {
                if(l==r)
                {
                    tagl=r;
                    break;
                }
                int m=(r+l)/2;
                if(stackl[m].x<x)l=m+1;
                else r=m;
            }
            l=1, r=sth;
            while(l<=r)
            {
                if(r-l<=1)
                {
                    if(stackh[r].x<=x)tagh=r;
                    else tagh=l;
                    break;
                }
                int m=(r+l)/2;
                if(stackh[m].x>x)r=m-1;
                else l=m;
            }
            if(y<=stackh[tagh].y&&y>=stackl[tagl].y)printf("1");
            else printf("0");
        }
        printf("\n");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值