比赛 ?板子大赛 评测结果 AAATTTTTTT
题目名称 接竹竿 最终得分 30
用户昵称 2_16鸡扒拌面 运行时间 7.714 s
代码语言 C++ 内存使用 3.45 MiB
提交时间 2026-01-17 13:06:27
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 15005;
int a[MAXN];

int asd(int l, int r) {
    stack<pair<int, int>> stk;
    int last[14]; // 1..13
    memset(last, -1, sizeof(last));
    for (int i = l; i <= r; i++) {
        int v = a[i];
        if (last[v] != -1) {
            // 需要消除
            while (!stk.empty()) {
                auto p = stk.top();
                if (p.second == last[v]) {
                    // 消除到这张牌为止
                    stk.pop();
                    last[p.first] = -1;
                    break;
                }
                stk.pop();
                last[p.first] = -1;
            }
        } else {
            last[v] = i;
            stk.push({v, i});
        }
    }
    return stk.size();
}

int main() {
    freopen("bamboo.in","r",stdin);
	freopen("bamboo.out","w",stdout);
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        int q;
        cin >> q;
        while (q--) {
            int l, r;
            cin >> l >> r;
            cout << asd(l, r) << "\n";
        }
    }
    
    return 0;
}