比赛 26暑假集训模拟赛2 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 丹钓战 最终得分 100
用户昵称 RpUtl 运行时间 4.246 s
代码语言 C++ 内存使用 18.49 MiB
提交时间 2026-07-02 09:10:29
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10; 
int n, q, a[N], b[N], stk[N], top, pre[N];
int ans[N], l[N], r[N], m, c[N];
struct node{int x, lim, v, id; }Q[N << 1];
bool chk(int x, int y) {
    return !(a[x] != a[y] && b[x] < b[y]);
}
void add(int x, int y) {
    for (x++; x <= n + 1; x += (x & -x)) c[x] += y;
    return;
}
int ask(int x, int y = 0) {
    for (x++; x > 0; x -= (x & -x)) y += c[x];
    return y;
}
int main() {
    freopen("stack.in", "r", stdin);
    freopen("stack.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n >> q;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> b[i];
    for (int i = 1; i <= n; i++) {
        while (top && chk(i, stk[top])) top--;
        pre[i] = stk[top], stk[++top] = i;
    }
    int l, r;
    for (int i = 1; i <= q; i++) {
        cin >> l >> r;
        if (l > 1) Q[++m] = node{l - 1, l - 1, -1, i};
        Q[++m] = node{r, l - 1, 1, i};
    }
    sort(Q + 1, Q + 1 + m, [&](node u, node v) {
        return u.x < v.x;
    });
    for(int i = 1, j = 1; i <= n; i++) {
//        cout << i << " " << pre[i] << '\n';
        add(pre[i], 1);
        while (j <= m && Q[j].x <= i) {
//            cout << i << " " << Q[j].id << " " << Q[j].v << " " << Q[j].lim << '\n'; 
            ans[Q[j].id] += Q[j].v * ask(Q[j].lim);
            j++;
        }
    }
    for (int i = 1; i <= q; i++) {
        cout << ans[i] << '\n';
    }
    return 0;
}