[二分]Mark and His Unfinished Essay CF1705C

23 篇文章 0 订阅

One night, Mark realized that there is an essay due tomorrow. He hasn't written anything yet, so Mark decided to randomly copy-paste substrings from the prompt to make the essay.

More formally, the prompt is a string ss of initial length nn. Mark will perform the copy-pasting operation cc times. Each operation is described by two integers ll and rr, which means that Mark will append letters slsl+1…srslsl+1…sr to the end of string ss. Note that the length of ss increases after this operation.

Of course, Mark needs to be able to see what has been written. After copying, Mark will ask qq queries: given an integer kk, determine the kk-th letter of the final string ss.

Input

The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

The first line of each test case contains three integers nn, cc, and qq (1≤n≤2⋅1051≤n≤2⋅105, 1≤c≤401≤c≤40, and 1≤q≤1041≤q≤104) — the length of the initial string ss, the number of copy-pasting operations, and the number of queries, respectively.

The second line of each test case contains a single string ss of length nn. It is guaranteed that ss only contains lowercase English letters.

The following cc lines describe the copy-pasting operation. Each line contains two integers ll and rr (1≤l≤r≤10181≤l≤r≤1018). It is also guaranteed that rr does not exceed the current length of ss.

The last qq lines of each test case describe the queries. Each line contains a single integer kk (1≤k≤10181≤k≤1018). It is also guaranteed that kk does not exceed the final length of ss.

It is guaranteed that the sum of nn and qq across all test cases does not exceed 2⋅1052⋅105 and 104104, respectively.

Output

For each query, print the kk-th letter of the final string ss.

Example

input

2
4 3 3
mark
1 4
5 7
3 8
1
10
12
7 3 3
creamii
2 3
3 4
2 9
9
11
12

output

m
a
r
e
a
r

Note

In the first test case, the copy-paste process is as follows.

  • The first step is pasting string markmark at the end, yielding the string markmarkmarkmark.
  • The second step is pasting string marmar at the end, yielding the string markmarkmarmarkmarkmar.
  • The third step is pasting string rkmarkrkmark at the end, yielding the string markmarkmarrkmarkmarkmarkmarrkmark.

In the second test case, the copy-paste process is as follows.

  • The first step is pasting string rere at the end, yielding the string creamiirecreamiire.
  • The second step is pasting string eaea at the end, yielding the string creamiireeacreamiireea.
  • The third step is pasting string reamiirereamiire at the end, yielding the string creamiireeareamiirecreamiireeareamiire.

题意: 有一个字符串s,以及c次copy操作,q次询问,每次copy操作给出一个区间[l, r],将s[l, r]这个子串拼接到原串后面构成新串s,c此操作后有q次询问,每次询问输出指定位置的字符。

分析: 由于这个字符串最终会非常长,所以不能直接保存下来,所以对于每次询问都需要向前找到它最初的位置,找之前可以先维护前i次操作后字符串的长度sum[i],这样找的过程其实就是个二分的过程,二分找到当前位置是从第几次copy得到的,之后计算出它向前的位置,重复这个过程直到当前位置落入1~n。

具体代码如下: 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#define int long long
using namespace std;

char s[200005];
int sum[55];
struct node{
	int l, r;
}cpy[55];

signed main()
{
	int T;
	cin >> T;
	while(T--){
		int n, c, q;
		scanf("%lld%lld%lld", &n, &c, &q);
		scanf("%s", s+1);
		sum[0] = n;
		for(int i = 1; i <= c; i++){
			scanf("%lld%lld", &cpy[i].l, &cpy[i].r);
			sum[i] = sum[i-1]+cpy[i].r-cpy[i].l+1;
		}
		cpy[0].l = 1, cpy[0].r = n;
		for(int i = 1; i <= q; i++){
			int t;
			scanf("%lld", &t);
			while(t > n){
				int l = 0, r = c, ans = -1;
				while(l <= r){
					int mid = l+r>>1;
					if(sum[mid] >= t){
						ans = mid;
						r = mid-1;
					}
					else l = mid+1;
				}
				t = cpy[ans].l+t-sum[ans-1]-1;
			}
			printf("%c\n", s[t]);
		}
	}
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值