이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<stdio.h>
#include<stdint.h>
#include<iomanip>
#include<algorithm>
#include<utility>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<deque>
#include<math.h>
#include<assert.h>
#include<string.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
const int64_t INF = (int64_t) 1e18 + 100;
const int64_t mINF = (int64_t) 1e9 + 100;
const int64_t MOD = 1e9 + 19972207;
const int64_t MOD2 = 998244353;
const int nbit = 31;
const int ndig = 10;
const int nchar = 26;
const int D = 4;
int dr[D] = {-1, 0, 1, 0};
int dc[D] = {0, 1, 0, -1};
const int Dk = 8;
int drk[Dk] = {2, 2, -2, -2, 1, 1, -1, -1};
int dck[Dk] = {1, -1, 1, -1, 2, -2, 2, -2};
struct Query
{
int t;
int k;
int id;
Query(int t_, int k_, int id_) : t(t_), k(k_), id(id_) {}
bool operator < (const Query& o) const
{
if(t != o.t) return t < o.t;
if(k != o.k) return k < o.k;
return id < o.id;
}
};
struct Thing
{
int val;
int l;
int r;
Thing(int val_, int l_, int r_) : val(val_), l(l_), r(r_) {}
bool operator < (const Thing& o) const
{
if(val == o.val)
{
if(l == o.l) return r < o.r;
return l < o.l;
}
return val < o.val;
}
};
struct Solution
{
int n;
int m;
int q;
int sz;
set<Thing> st;
vector<int> a;
vector<int> nx;
vector<Query> v;
vector<int> tree;
vector<int> ans;
Solution() {}
void solve()
{
cin >> n >> q;
a.resize(n, 0);
nx.resize(n, n - 1);
vector<int> pos(n, -1);
for(int i = 0; i < n; i++)
{
cin >> a[i];
a[i]--;
pos[a[i]] = i;
}
for(int i = 0; i < q; i++)
{
int t;
int k;
cin >> t >> k;
t = min(t, n);
v.push_back(Query(t, k, i));
}
sort(v.begin(), v.end());
m = n;
sz = 0;
while(__builtin_popcount(m) != 1) m++;
tree.resize(2 * m, 0);
stack<int> s;
for(int i = 0; i < n; i++)
{
while(!s.empty() && a[s.top()] < a[i])
{
nx[s.top()] = i - 1;
s.pop();
}
s.push(i);
}
int j = 0;
ans.resize(n, -1);
addBlocks(0, n - 1);
for(int i = 0; i <= n; i++)
{
while(j < q && v[j].t == i)
{
int p = n;
int x = getAns(1, 0, m - 1, v[j].k, p);
ans[v[j].id] = a[pos[x] + v[j].k - p - 1] + 1;
j++;
}
deleteRight();
if(sz > n / 2)
{
Thing mid = (*prev(st.end()));
st.erase(mid);
int x = mid.r - (sz - n / 2);
update(mid.val, x - mid.l + 1);
st.insert(Thing(mid.val, mid.l, x));
addBlocks(x + 1, mid.r);
sz -= (mid.r - mid.l + 1);
sz += (x - mid.l + 1);
}
}
for(int i = 0; i < q; i++)
{
cout << ans[i] << "\n";
}
}
void deleteRight()
{
set<Thing>::iterator it = prev(st.end());
do
{
int len = (*it).r - (*it).l + 1;
if(sz - len < n / 2) break;
st.erase(it);
it = prev(st.end());
sz -= len;
} while(it != st.begin());
}
void addBlocks(int l, int r)
{
while(l <= r)
{
int e = min(nx[l], r);
int len = e - l + 1;
sz += len;
st.insert(Thing(a[l], l, e));
update(a[l], len);
l = nx[l] + 1;
}
}
int getAns(int node, int nleft, int nright, int id, int& p)
{
if(nleft == nright)
{
p -= tree[node];
return nleft;
}
int mid = nleft + (nright - nleft) / 2;
if(p - tree[node * 2 + 1] >= id)
{
p -= tree[node * 2 + 1];
return getAns(node * 2, nleft, mid, id, p);
}
return getAns(node * 2 + 1, mid + 1, nright, id, p);
}
void update(int id, int val)
{
tree[m + id] = val;
for(int i = (m + id) / 2; i >= 1; i /= 2)
{
tree[i] = tree[i * 2] + tree[i * 2 + 1];
}
}
int modadd(int t1, int t2)
{
int res = t1 + t2;
if(res >= MOD) res -= MOD;
return res;
}
int modsub(int t1, int t2)
{
int res = t1 - t2;
if(res < 0) res += MOD;
return res;
}
int modmul(int t1, int t2)
{
int64_t res = 1LL * t1 * t2;
return res % MOD;
}
int Abs(int tmp)
{
if(tmp < 0) return -tmp;
return tmp;
}
int MASK(int j)
{
return (1 << j);
}
bool BIT(int tmp, int j)
{
return (tmp & MASK(j));
}
};
void __setup()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
}
int main()
{
__setup();
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
Solution().solve();
}
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |