이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int maxN = 4e5 + 5;
long long query[maxN];
long long get_l(const long long &num)
{
return num & ((1LL << 19) - 1LL);
}
long long get_r(const long long &num)
{
return (num >> 19) & ((1LL << 19) - 1LL);
}
long long get_idx(const long long &num)
{
return (num >> 38) & ((1LL << 19) - 1LL);
}
bool cmp_query(const long long &a, const long long &b)
{
return get_l(a) > get_l(b);
}
map<long long, int> helper;
map<long long, int>::iterator m_iter;
vector<int> S;
int val[maxN];
int last_l;
long long re[maxN];
int calc(const int &l, const int &r)
{
int tr = r;
while(l <= tr)
{
if(val[tr])last_l = get_r(re[tr]);
S.push_back(tr);
tr = get_r(re[tr]) - 1;
}
int pr = l - 1;
while(!S.empty())
{
val[S.back()] += val[pr]; //cout << "Calc " << l << ' ' << S.back() << ' ' << val[S.back()] << '\n';
re[S.back()] ^= (long long)((val[S.back()] ? last_l : l) ^ get_r(re[S.back()])) << 19;
pr = S.back();
S.pop_back();
}
return val[r];
}
int n, q;
int main()
{
cin >> n;
val[0] = 0;
for(int i = 1; i <= n; ++i)
{
cin >> re[i];
val[i] = 0;
}
cin >> q;
long long l, r, idx;
for(int i = 1; i <= q; ++i)
{
cin >> l >> r;
idx = i;
query[i] = l | (r << 19) | (idx << 38);
}
sort(query + 1, query + q + 1, cmp_query);
helper.clear();
helper.insert(make_pair(0LL, n + 1));
long long sum = 0;
for(int i = n; i > 0; --i)
{
sum += re[i];
re[i] = 0;
m_iter = helper.find(sum);
if(m_iter == helper.end())
{
helper.insert(make_pair(sum, i));
}
else
{
re[i] = m_iter->second - 1;
m_iter->second = i;
}
}
for(int i = 1; i <= n; ++i)
re[i] |= (long long)i << 19;
int itl = n;
for(int i = 1; i <= q; ++i)
{
while(itl >= get_l(query[i]))
{
if(get_l(re[itl]))
{
calc(itl, get_l(re[itl]));
if(!val[get_l(re[itl])])
{
val[get_l(re[itl])] = 1;
re[get_l(re[itl])] ^= (long long)(itl ^ get_r(re[get_l(re[itl])])) << 19;
for(int j = get_l(re[itl]) + 1; j <= n; ++j)
if(val[j])break;
else re[j] ^= (long long)((get_l(re[itl]) + 1) ^ get_r(re[j])) << 19;
}
}
itl--;
}
re[get_idx(query[i])] |= (long long)calc(get_l(query[i]), get_r(query[i])) << 38;
}
for(int i = 1; i <= q; ++i)
cout << get_idx(re[i]) << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |