#include <bits/stdc++.h>
using namespace std;
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define rf if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
#define bit(i, mask) (((mask) >> (i)) & 1)
#define ll long long
#define fi first
#define se second
#define pii pair<int, int>
#define vi vector<int>
#define all(a) (a).begin(), (a).end()
#define len(x) ((int)(x).size())
#define pb push_back
#define endl '\n'
#define name ""
template<typename T1, typename T2> bool mini(T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
const ll oo = 1e18l + 7;
const int M = 5e5 + 6;
const int N = 4e5 + 6;
const int LOG = 31 - __builtin_clz(N);
const int S = 632;
int n, q, a[N];
struct Block{
int st, en;
priority_queue<int> pq;
int m, vec[25005];
void build(){
if(m == 0) return;
priority_queue<int, vi, greater<int>> _pq(vec + 1, vec + m + 1);
for(int i = st; i <= en; i++){
int val = _pq.top();
if(a[i] > val){
_pq.pop(); _pq.push(a[i]);
a[i] = val;
}
}
m = 0;
}
int modify(int l, int r, int x){
build();
for(int i = l; i <= r; i++) if(a[i] > x) swap(a[i], x);
pq = priority_queue<int>(a + st, a + en + 1);
return x;
}
int update(int x){
vec[++m] = x;
int val = pq.top();
if(x < val){
pq.pop(); pq.push(x);
x = val;
}
return x;
}
} B[N / S + 2];
void inp(){
cin >> n >> q;
for(int i = 1; i <= n; i++) cin >> a[i];
}
#define idBlock(i) ((i - 1) / S + 1)
#define posBlock(i) ((i - 1) * S + 1)
int calc(int l, int r, int x){
int blockL = idBlock(l), blockR = idBlock(r);
if(blockL == blockR) return B[blockL].modify(l, r, x);
int enL = blockL * S, stR = posBlock(blockR);
x = B[blockL].modify(l, enL, x);
for(int i = blockL + 1; i < blockR; i++) x = B[i].update(x);
x = B[blockR].modify(stR, r, x);
return x;
}
void proc(){
for(int i = 1; i <= (n - 1) / S + 1; i++){
int l = (i - 1) * S + 1, r = min(n, i * S);
B[i].st = l, B[i].en = r;
B[i].pq = priority_queue<int>(a + l, a + r + 1);
}
while(q--){
int l, r, x; cin >> l >> r >> x;
if(l <= r) cout << calc(l, r, x) << endl;
else cout << calc(1, r, calc(l, n, x)) << endl;
}
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
rf
int test = 1;
//cin >> test;
while(test--){
inp();
proc();
}
cerr << "Time elapsed: " << TIME << "s" << endl;
return 0;
}
Compilation message (stderr)
sushi.cpp: In function 'int main()':
sushi.cpp:6:47: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
6 | #define rf if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sushi.cpp:106:5: note: in expansion of macro 'rf'
106 | rf
| ^~
sushi.cpp:6:80: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
6 | #define rf if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sushi.cpp:106:5: note: in expansion of macro 'rf'
106 | rf
| ^~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |