题干:
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.
First, he splits the Banh-mi into nn parts, places them on a row and numbers them from 11 through nn. For each part ii, he defines the deliciousness of the part as xi∈{0,1}xi∈{0,1}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the ii-th part then his enjoyment of the Banh-mi will increase by xixi and the deliciousness of all the remaining parts will also increase by xixi. The initial enjoyment of JATC is equal to 00.
For example, suppose the deliciousness of 33 parts are [0,1,0][0,1,0]. If JATC eats the second part then his enjoyment will become 11 and the deliciousness of remaining parts will become [1,_,1][1,_,1]. Next, if he eats the first part then his enjoyment will become 22 and the remaining parts will become [_,_,2][_,_,2]. After eating the last part, JATC's enjoyment will become 44.
However, JATC doesn't want to eat all the parts but to save some for later. He gives you qq queries, each of them consisting of two integers lili and riri. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [li,ri][li,ri] in some order.
All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 109+7109+7.
Input
The first line contains two integers nn and qq (1≤n,q≤1000001≤n,q≤100000).
The second line contains a string of nn characters, each character is either '0' or '1'. The ii-th character defines the deliciousness of the ii-th part.
Each of the following qq lines contains two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n) — the segment of the corresponding query.
Output
Print qq lines, where ii-th of them contains a single integer — the answer to the ii-th query modulo 109+7109+7.
Examples
Input
4 2
1011
1 4
3 4
Output
14
3
Input
3 2
111
1 2
3 3
Output
3
1
Note
In the first example:
- For query 11: One of the best ways for JATC to eats those parts is in this order: 11, 44, 33, 22.
- For query 22: Both 33, 44 and 44, 33 ordering give the same answer.
In the second example, any order of eating parts leads to the same answer.
中文题意:
Glory喜欢吃冰激凌,有一天,他买了一箱冰激凌
首先, 他把这些冰激凌分成 n 份, 把它们从1 到n排成一排 . 对每一部分都有一个 美味系数 xi 属于 {0, 1}. Glory打算一个个吃掉它们. 每一次,Glory可以随意选一部分吃掉,假设这部分是第i部分,然后 Glory的开心值 将会增加xi剩下来的每一份冰激凌的美味系数将会增加xi. Glory最初的开心值为 0.
举个例子说明这个过程,假设冰激凌被分成有 3份,美味系数为 [0, 1, 0]. 如果Glory先吃第二块,那么他的开心值将变为 1 ,然后三份的美味系数变为 [1, _, 1]. 然后Glory再吃第三块,那么他的开心值将变为2 ,然后三份的美味系数变为[_, _, 2].在吃最后一块后, Glory的开心值变为 4.
然而, Glory想利益最大化. 他给你 q 询问,询问包含 l_i and r_i. 对每次询问,你需要告诉他,他吃完[l_i, r_i]之间的所有冰激凌最多能获得多少开心值(不要求输出吃的顺序,只要求输出最大收益).
答案可能会非常大,请mod 10^9+7输出.
解题报告:
首先看第一步,肯定有1选1,如果同时有多个1,那选哪个1都不影响答案,一次类推我们不需要关注位置,只需要关注在当前查询的 ( l , r )区间内有多少1,有多少0。通过贪心,我们知道我们每次需要选择最大的进行贪心。
找几个例子就画出结论了。这里给一个别人的题解:
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const ll mod = 1e9+7;
const int MAX = 2e5 + 5;
char s[MAX];
int sum0[MAX];
int bit[MAX];
int main()
{ int n,q;cin>>n>>q;cin>>(s+1);for(int i = 1; i<=n; i++) {sum0[i] = sum0[i-1];if(s[i] == '0') sum0[i]++;}int l,r;bit[0]=1;for(int i = 1; i<=n; i++) bit[i] = (bit[i-1]*2)%mod;while(q--) {scanf("%d%d",&l,&r);int len = r-l+1;ll ans = bit[len] - bit[sum0[r] - sum0[l-1] ];if(ans<=0) ans += mod;ans %=mod;printf("%lld\n",ans);}return 0 ;}