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>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sortvec(x) sort(all(x))
#define compress(x) x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<int, LL> pil;
typedef pair<LL, int> pli;
const LL llinf=1987654321987654321;
const int inf=2000000000;
int n, q, sq;
int arr[400010];
priority_queue<int> pq[1010];
vector<int> temp[1010];
int query2(int a, int b, int c){
priority_queue<int, vector<int>, greater<int> > tpq;
int buca=(a-1)/sq;
for(auto i:temp[buca])tpq.push(i);
temp[buca].clear();
for(int i=buca*sq+1; i<=(buca+1)*sq; i++){
tpq.push(arr[i]);
arr[i]=tpq.top();
tpq.pop();
}
for(int i=a; i<=b; i++){
if(c<arr[i])swap(c, arr[i]);
}
while(!pq[buca].empty())pq[buca].pop();
for(int i=buca*sq+1; i<=(buca+1)*sq; i++)pq[buca].push(arr[i]);
return c;
}
int query(int a, int b, int c){
int buca=(a-1)/sq+1, bucb=(b-1)/sq;
if(bucb>buca)return query2(a, b, c);
c=query2(a, buca*sq, c);
for(int i=buca; i<bucb; i++){
temp[i].pb(c);
pq[i].push(c);
c=pq[i].top();
pq[i].pop();
}
c=query2(bucb*sq+1, b, c);
return c;
}
int main(){
scanf("%d %d", &n, &q);
sq=sqrt(n);
for(int i=1; i<=n; i++){
scanf("%d", &arr[i]);
pq[(i-1)/sq].push(arr[i]);
}
for(int i=1; i<=q; i++){
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if(a<=b)printf("%d\n", query(a, b, c));
else printf("%d\n", query(1, b, query(a, n, c)));
}
}
Compilation message (stderr)
sushi.cpp: In function 'int main()':
sushi.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &q);
~~~~~^~~~~~~~~~~~~~~~~
sushi.cpp:55:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &arr[i]);
~~~~~^~~~~~~~~~~~~~~
sushi.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &a, &b, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |