QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#442275 | #8749. 贸易 | Mindeveloped | WA | 140ms | 349668kb | C++14 | 1.6kb | 2024-06-15 10:47:55 | 2024-06-15 10:48:01 |
Judging History
answer
#include<bits/stdc++.h>
#define F(i, x, y) for (int (i) = (x);(i) <= (y);(i)++)
using namespace std;
const int N = 5e5 + 5;
int n, q, a[N], c[N], lp[N];
stack<int> lb[N];
namespace st {
int root[N];
int sum[40 * N], ls[40 * N], rs[40 * N], cnt;
int build (int l, int r) {
int p = ++cnt;
if (l != r) {
int mid = (l + r) >> 1;
ls[p] = build (l, mid);
rs[p] = build (mid + 1, r);
}
return p;
}
int modify (int p, int l, int r, int k) {
int np = ++cnt;
ls[np] = ls[p]; rs[np] = rs[p];
sum[np] = sum[p];
if (l == r) sum[np]++;
else {
int mid = (l + r) >> 1;
if (k <= mid) ls[np] = modify (ls[p], l, mid, k);
else rs[np] = modify (rs[p], mid + 1, r, k);
sum[np] = sum[ls[np]] + sum[rs[np]];
}
return np;
}
int query (int p, int tl, int tr, int ql, int qr) {
if (ql <= tl && tr <= qr) return sum[p];
else {
int mid = (tl + tr) >> 1;
int ans = 0;
if (ql <= mid) ans += query (ls[p], tl, mid, ql, qr);
if (qr > mid) ans += query (rs[p], mid + 1, tr, ql, qr);
return ans;
}
}
}
int main () {
ios::sync_with_stdio (false);
cin.tie (0);
cin >> n >> q;
F (i, 1, n) cin >> a[i];
F (i, 1, n) cin >> c[i];
F (i, 1, n) {
if (a[i] == 0) lb[c[i]].push (i);
else {
if (lb[c[i]].size ()) {
lp[i] = lb[c[i]].top ();
lb[c[i]].pop ();
}
}
}
st::root[0] = st::build (0, n);
F (i, 1, n) {
st::root[i] = st::modify (st::root[i - 1], 0, n, lp[i]);
}
int l, r;
F (i, 1, q) {
cin >> l >> r;
cout << st::query (st::root[r], 1, n, l, r) - st::query (st::root[l - 1], 1, n, l, r) << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 64ms
memory: 349668kb
input:
10 5 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 4 6 2 4 2 6 7 10 4 7
output:
0 0 0 1 0
result:
ok 5 lines
Test #2:
score: -100
Wrong Answer
time: 140ms
memory: 349664kb
input:
20 500000 1 0 0 1 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 1 2 1 2 2 1 1 2 1 1 1 2 2 1 2 2 2 1 1 2 13 20 8 9 1 7 5 13 3 10 3 19 14 15 1 5 9 17 7 10 6 6 8 20 1 17 13 20 4 6 16 20 7 14 2 16 3 17 11 12 1 1 15 20 11 15 2 12 2 15 8 16 9 12 9 13 10 19 12 19 9 13 4 8 2 2 19 19 9 17 4 20 4 14 4 8 6 13 13 17 15 16 13...
output:
1 0 7 3 2 6 0 5 3 1 0 4 17 1 0 1 3 5 6 0 1 1 1 3 5 3 1 2 3 2 2 0 0 0 3 5 3 0 3 1 0 1 1 4 2 2 2 1 0 0 2 3 1 0 3 8 0 1 4 0 1 0 5 3 2 0 1 1 1 1 0 5 3 16 5 5 2 3 2 2 2 0 4 2 0 1 6 5 1 2 2 5 4 1 1 1 1 3 1 2 5 1 2 2 0 0 5 0 1 13 3 5 1 13 2 3 0 2 3 1 2 1 1 15 1 2 3 1 6 1 1 6 0 1 4 2 0 19 3 1 0 1 2 0 2 1 0 ...
result:
wrong answer 3rd lines differ - expected: '1', found: '7'