#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace std;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
int n,m;
string s;
struct seg
{
int l,r,k;
bool operator<(const seg& other) const
{
if(l != other.l) return l < other.l;
return r < other.r;
}
};
struct node
{
vector<seg> segs;
node()
{
segs.pb({0,n,0});
}
void def()
{
segs = {{0,n,0}};
}
void default_a(int a)
{
segs = {};
if(a*2 < n)
{
segs.pb({0,a-1,a+1});
}
else
{
if(n - (a+1) >= 0) segs.pb({0,n-(a+1),a+1});
segs.pb({n-(a+1)+1,a-1,0});
}
int s = (a+a) % (n+1);
if(s + (n - a) <= n)
{
segs.pb({a,n,s});
}
else
{
segs.pb({a,a+n-s,s});
segs.pb({a+n-s+1,n,0});
}
sort(all(segs));
}
node operator+(const node& other)
{
node res;
res.segs = {};
forall(it,segs)
{
seg w = {it.k,(int)1e9,-1};
auto iter = lower_bound(other.segs.begin(),other.segs.end(), w);
iter--;
int cur = it.k;
int cur_poz = it.l;
int r = it.k + (it.r - it.l);
while(cur <= r)
{
if((*iter).r >= r)
{
res.segs.pb({cur_poz,it.r,(*iter).k + cur - (*iter).l});
break;
}
else
{
int d = (*iter).r - cur;
res.segs.pb({cur_poz,cur_poz + d,(*iter).k + cur - (*iter).l});
cur = (*iter).r+1;
cur_poz += d+1;
iter++;
}
}
}
sort(all(res.segs));
return res;
}
int get_val(int k)
{
auto it = lower_bound(segs.begin(),segs.end(),(seg){k,(int)1e9,-1});
it--;
return (*it).k + (k - (*it).l);
}
};
const int tree_siz = 1024*256-1;
node drzewo[tree_siz];
int get_pref(int akt, int p1, int p2, int s1, int s2, int cur)
{
if(p2 < s1 || p1 > s2) return cur;
if(p1 >= s1 && p2 <= s2)
{
return drzewo[akt-1].get_val(cur);
}
cur = get_pref(akt*2,p1,(p1+p2)/2,s1,s2,cur);
cur = get_pref(akt*2+1,(p1+p2)/2+1,p2,s1,s2,cur);
return cur;
}
void build_tree(vi& a)
{
rep(i,m)
{
drzewo[tree_siz/2+i].default_a(a[i]);
}
rep2(i,tree_siz/2+m,tree_siz-1) drzewo[i].def();
for(int i = tree_siz/2-1; i >= 0; i--)
{
drzewo[i] = drzewo[i*2+1] + drzewo[i*2+2];
}
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random_start();
cin >> n >> m >> s;
int pref = 0;
rep(i,n)
{
if(s[i] == 'R') pref++;
else break;
}
vi A(m);
rep(i,m) cin >> A[i];
build_tree(A);
int q;
cin >> q;
rep(i,q)
{
int l,r;
cin >> l >> r;
l--;
r--;
cout << get_pref(1,0,tree_siz/2,l,r,pref) << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |