Codeforces Round #449 (Div. 2) Codeforces Round #449 (Div. 2) 二叉树、回溯、分类讨论

C. Nephren gives a riddle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
What are you doing at the end of the world? Are you busy? Will you save us?

Nephren is playing a game with little leprechauns.

She gives them an infinite array of strings, f0... ∞.

f0 is "What are you doing at the end of the world? Are you busy? Will you save us?".

She wants to let more people know about it, so she defines fi =  "What are you doing while sending "fi - 1"? Are you busy? Will you send "fi - 1"?" for all i ≥ 1.

For example, f1 is

"What are you doing while sending "What are you doing at the end of the world? Are you busy? Will you save us?"? Are you busy? Will you send "What are you doing at the end of the world? Are you busy? Will you save us?"?". Note that the quotes in the very beginning and in the very end are for clarity and are not a part of f1.

It can be seen that the characters in fi are letters, question marks, (possibly) quotation marks and spaces.

Nephren will ask the little leprechauns q times. Each time she will let them find the k-th character of fn. The characters are indexed starting from 1. If fn consists of less than k characters, output '.' (without quotes).

Can you answer her queries?

Input

The first line contains one integer q (1 ≤ q ≤ 10) — the number of Nephren's questions.

Each of the next q lines describes Nephren's question and contains two integers n and k (0 ≤ n ≤ 105, 1 ≤ k ≤ 1018).

Output

One line containing q characters. The i-th character in it should be the answer for the i-th query.

Examples
input
3
1 1
1 2
1 111111111111
output
Wh.
input
5
0 69
1 194
1 139
0 47
1 66
output
abdef
input
10
4 1825
3 75
3 530
4 1829
4 1651
3 187
4 584
4 255
4 774
2 474
output
Areyoubusy
Note

For the first two examples, refer to f0 and f1 given in the legend.


Source

Codeforces Round #449 (Div. 2)


My Solution

题意:用一个前缀s1,中间部分s2,后缀s3,fi = s1 + fi-1 + s2 + fi-1 + s3来构造字符串 fi,q个询问(n, k),每次询问第n个字符串的第k个字符。


二叉树、回溯、分类讨论

这样构造出的字符串相当于一颗二叉树,从叶子开始回溯,回溯的时候根据k的情况,判断是从左子树向根回溯,还是从右子树向根回溯。

有点想主席树的一些操作,先预处理出fi的长度,然后每次对于k和当前fi的情况,回溯到i-1,

回溯的时候 如果是左边 k -= 34(前缀),如果是右边 k -= 34(前缀) + 32(中间),然后用新的k和新的n(n-1)进行进行计算。

具体的分类讨论情况,见源码。

时间复杂度 O(n*q)

空间复杂度 O(64)

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
typedef long long LL;
const int MAXN = 1e6 + 8;

string s, s0, s1, s2, s3, s4;
LL f[64];
int main()
{
    #ifdef LOCAL
    freopen("c.txt", "r", stdin);
    //freopen("c.out", "w", stdout);
    int T = 3;
    while(T--){
    #endif // LOCAL
    ios::sync_with_stdio(false); cin.tie(0);


    s0 = "What are you doing at the end of the world? Are you busy? Will you save us?";
    s1 = "What are you doing while sending \"";
    s2 = "\"? Are you busy? Will you send \"";
    s3 = "\"?";
    //cout << s0.size() << " " << s1.size() << " " << s2.size() << " " << s3.size() << endl;

    int i, maxi;
    f[0] = 75;
    for(i = 1; i < 64; i++){
        f[i] = f[i-1]*2 + 34 + 32 + 2;
        if(f[i] > (LL)1e18){
            maxi = i;
            break;
        }
    }
    //cout << s0[31]<<endl;
    LL q, n, k;
    cin >> q;
    while(q--){
        cin >> n >> k;

        while(n && n--){
            n++;
            if(n <= maxi && f[n] < k){
                cout << ".";
                k = 0;
                break;
            }
            if(n - 1 > maxi){
                if(k > 34) k -= 34;
                else{
                    cout << s1[k-1];
                    k = 0;
                    break;
                }
            }
            else{
                if(34 + f[n-1] < k){
                    if(34 + f[n-1] + 32 + f[n-1] < k){
                        k -= 34 + f[n-1] + 32 + f[n-1];
                        cout << s3[k-1];
                        k = 0;
                        break;
                    }
                    else{
                        k -= 34 + f[n-1];
                        if(k > 32) k -= 32;
                        else{
                            cout << s2[k-1];
                            k = 0;
                            break;
                        }
                    }

                }
                else{
                    //cout << "??" << n << " " << k << "??" ;
                    if(k > 34) k -= 34;
                    else{
                        cout << s1[k-1];
                        k = 0;
                        break;
                    }
                }
            }
            n--;

        }
         //cout << n << " " << k << endl;
         if(n == 0 && k != 0){
            //cout << "??";
            if(k <= f[0]) cout << s0[k-1];
            else cout << ".";
         }
    }



    #ifdef LOCAL
    cout << endl;
    }
    #endif // LOCAL
    return 0;
}

  Thank you!

                                                                                                                                             ------from ProLights

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值