This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int n,q;
vector<int> v;
int sol[1000005];
struct date
{
int i,j,poz;
};
vector<date> qr;
vector<int> trans(vector<int> v)
{
vector<int> a,b;
for(int i=0;i<v.size();i++)
{
if(i*2<v.size())
a.push_back(v[i]);
else
b.push_back(v[i]);
}
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
vector<int> rez;
while(a.size()||b.size())
{
if(a.size()&&b.size())
{
int x=a.back();
int y=b.back();
rez.push_back(min(x,y));
if(x<y)
a.pop_back();
else
b.pop_back();
continue;
}
if(a.size())
{
int x=a.back();
a.pop_back();
rez.push_back(x);
}
else
{
int x=b.back();
b.pop_back();
rez.push_back(x);
}
}
return rez;
}
bool comp(date a, date b)
{
return a.i>b.i;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
v.push_back(x);
}
for(int i=1;i<=q;i++)
{
int x,y;
cin>>x>>y;
qr.push_back({x,y,i});
}
sort(qr.begin(),qr.end(),comp);
while(!qr.empty()&&qr.back().i==0)
{
int j=qr.back().j;
int poz=qr.back().poz;
sol[poz]=v[j-1];
qr.pop_back();
}
int cnt=0;
while(!qr.empty())
{
vector<int> aux=v;
v=trans(v);
cnt++;
bool iseq=1;
for(int i=0;i<v.size();i++)
if(v[i]!=aux[i])
iseq=0;
if(iseq)
{
for(int i=0;i<qr.size();i++)
{
int j=qr[i].j;
int poz=qr[i].poz;
sol[poz]=v[j-1];
}
break;
}
while(!qr.empty()&&cnt==qr.back().i)
{
int j=qr.back().j;
int poz=qr.back().poz;
sol[poz]=v[j-1];
qr.pop_back();
}
}
assert(cnt<=n);
for(int i=1;i<=q;i++)
cout<<sol[i]<<'\n';
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'std::vector<int> trans(std::vector<int>)':
Main.cpp:15:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for(int i=0;i<v.size();i++)
| ~^~~~~~~~~
Main.cpp:17:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | if(i*2<v.size())
| ~~~^~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:89:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for(int i=0;i<v.size();i++)
| ~^~~~~~~~~
Main.cpp:94:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<date>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for(int i=0;i<qr.size();i++)
| ~^~~~~~~~~~
# | 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... |