Two Hundred Twenty One (hard version)

This is the hard version of the problem. The difference between the versions is that the hard version does require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.

Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.

The main element of this machine are n rods arranged along one straight line and numbered from 1 to n inclusive. Each of these rods must carry an electric charge quantitatively equal to either 1 or −1 (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.

More formally, the rods can be represented as an array of n numbers characterizing the charge: either 1 or −1. Then the condition must hold: a1−a2+a3−a4+…=0, or ∑i=1n(−1)i−1⋅ai=0.

Sparky charged all n rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has q questions. In the ith question Sparky asks: if the machine consisted only of rods with numbers li to ri inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Also Sparky wants to know the numbers of these rods. Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.

If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.

Help your friends and answer all of Sparky's questions!

Input

Each test contains multiple test cases.

The first line contains one positive integer t (1≤t≤103), denoting the number of test cases. Description of the test cases follows.

The first line of each test case contains two positive integers n and q (1≤n,q≤3⋅105) — the number of rods and the number of questions.

The second line of each test case contains a non-empty string s of length n, where the charge of the i-th rod is 1 if si is the "+" symbol, or −1 if si is the "-" symbol.

Each next line from the next q lines contains two positive integers li ans ri (1≤li≤ri≤n) — numbers, describing Sparky's questions.

It is guaranteed that the sum of n over all test cases does not exceed 3⋅105, and the sum of q over all test cases does not exceed 3⋅105.

It is guaranteed that the sum of the answers (minimal number of rods that can be removed) over all test cases does not exceed 106.

Output

For each test case, print the answer in the following format:

In the first line print a single integer k — the minimal number of rods that can be removed.

In the second line print k numbers separated by a space — the numbers of rods to be removed.

If there is more than one correct answer, you can print any.

Example

input

Copy

3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4

output

Copy

2
5 8
2
1 11
1
9
0
1
1
2
1 2
1
2
2
1 3
1
2
2
2 3
1
3
1
3
2
3 4
1
4

Note

In the first test case for the first query you can remove the rods numbered 5 and 8, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.

In the second test case:

  • For the first query, we can remove the rods numbered 1 and 11, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
  • For the second query we can remove the rod numbered 9, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
  • For the third query we can not remove the rods at all.

思路:已经写出easy版本,在easy版本基础上进行改动:思路1:因为easy版本我们由数学公式推到出来的前半部分:即 f[R] 与 f[L] 有对称性,现在继续在公式基础上推到,f[i]的含义是什么?:去掉i点后剩下部分的权值,还记得这点的话,之后很好理解,我们要去点时(这时是能的)那么就会有f[i]=0,那么由f[i]的公式可以得到:              sum[r]+sum[l-1]=sum[i]+sum[i-1],那么我们只要在L之后找到i就可以。

#include<bits/stdc++.h>

using namespace std;

const int N=3e5+10;
int sum[N];
char s[N];
const int B=(N<<1);
vector<int>v[N<<2];

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int t;
    cin>>t;
    while(t--)
    {
        int n,q;
        cin>>n>>q;
        cin>>s+1;
        for(int i=B-2*n;i<=B+2*n;i++)
        {
            v[i].clear();
        }
        for(int i=1;i<=n;i++)
        {
            if(i&1) sum[i]=sum[i-1]+(s[i]=='+'?1:-1);
            else sum[i]=sum[i-1]+(s[i]=='-'?1:-1);
            v[sum[i]+sum[i-1]+B].push_back(i);
        }
        while(q--)
        {
            int l,r;
            cin>>l>>r;
            int dif=sum[r]-sum[l-1];
            if(dif==0)
            {
                cout<<"0"<<endl;
                continue;
            }
            else if(dif&1)
            {
                cout<<"1"<<endl;
            }
            else
            {
                cout<<"2"<<endl;
                cout<<l<<" ";
                l++;
            }
            int x=sum[r]+sum[l-1]+B;
            int id=lower_bound(v[x].begin(),v[x].end(),l)-v[x].begin();
            cout<<v[x][id]<<endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值