Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- #define fastIO() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
- const int maxn = 1 << 18;
- vector<int> seg(2*maxn);
- void build(vector<int> a, int len) {
- for (int i = 0; i < len; ++i) {
- seg[maxn + i] = a[i];
- }
- for (int i = maxn-1; i >= 1; --i) {
- seg[i] = max(seg[i<<1], seg[i<<1|1]);
- }
- }
- int RMax(int L, int R, int now = 1, int nL = 0, int nR = maxn-1) {
- if (L > nR or R < nL) return 0;
- if (nR <= R and nL >= L) return seg[now];
- return max(RMax(L, R, now*2 , nL , (nL+nR)/2),
- RMax(L, R, now*2+1, (nL+nR)/2+1, nR ));
- }
- int32_t main() {
- fastIO();
- int n, m, ans(0);
- cin >> n >> m;
- vector<int> a(n), b(m), Ans;
- vector<bool> now(n, 1);
- for (auto &x : a) cin >> x;
- for (auto &x : b) cin >> x, now[--x] = 0;
- reverse(b.begin(), b.end());
- build(a, n);
- map<int, int> LtoR, RtoL;
- int nL = 0, nR;
- for (int i = 0; i < n; ++i) {
- while (i < n and now[i]) ++i;
- nR = i - 1;
- // cout << nL << ", " << nR << "\n";
- if (nL <= nR) LtoR[nL] = nR, RtoL[nR] = nL;
- nL = i + 1;
- }
- for (auto x : LtoR) {
- ans = max(ans, (x.second-x.first+1) * RMax(x.first, x.second));
- }
- int Lnum, Rnum;
- for (auto x : b) {
- Ans.push_back(ans);
- if (x == 0 and now[1]) {
- Lnum = 0;
- Rnum = LtoR[1];
- LtoR[Lnum] = Rnum;
- RtoL[Rnum] = Lnum;
- }
- else if (x == n - 1 and now[n - 2]) {
- Lnum = RtoL[n - 2];
- Rnum = n - 1;
- LtoR[Lnum] = Rnum;
- RtoL[Rnum] = Lnum;
- }
- else if ((x == 0 or x == n - 1) or (!now[x - 1] and !now[x + 1])) {
- Lnum = x;
- Rnum = x;
- LtoR[x] = x;
- RtoL[x] = x;
- }
- else if (now[x - 1] and now[x + 1]) {
- Lnum = RtoL[x - 1];
- Rnum = LtoR[x + 1];
- LtoR[Lnum] = Rnum;
- RtoL[Rnum] = Lnum;
- }
- else if (now[x - 1]) {
- Lnum = RtoL[x - 1];
- Rnum = x;
- LtoR[Lnum] = Rnum;
- RtoL[Rnum] = Lnum;
- }
- else {
- Lnum = x;
- Rnum = LtoR[x + 1];
- LtoR[Lnum] = Rnum;
- RtoL[Rnum] = Lnum;
- }
- ans = max(ans, (Rnum-Lnum+1) * RMax(Lnum, Rnum));
- now[x] = 1;
- }
- reverse(Ans.begin(), Ans.end());
- for (auto x : Ans) cout << x << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement