QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#726073 | #6533. Traveling in Cells | He_ng | WA | 725ms | 514008kb | C++20 | 5.0kb | 2024-11-08 21:30:51 | 2024-11-08 21:30:52 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
const int N = 1e5 + 10;
const int M = 2e3;
const int MAXA = 1e5 + 5;
#define endl '\n'
struct DynamicSegtree {
struct node {
int data, tag;
node *lc, *rc;
node(int data_ = 0) : data(data_), tag(0), lc(nullptr), rc(nullptr) {}
void up() {
data = 0;
if (lc != nullptr)
data += lc->data;
if (rc != nullptr)
data += rc->data;
return;
}
void down(int l, int r) {
if (lc == nullptr)
lc = new node;
if (rc == nullptr)
rc = new node;
int mid = (l + r) >> 1;
lc->data += (mid - l + 1) * tag;
lc->tag += tag;
rc->data += (r - mid) * tag;
rc->tag += tag;
tag = 0;
}
} *rt = new node(0);
void modify(node *rt, int L, int R, int p, int val) {
if (L == R) {
rt->data = val; // 更新单点的值
return;
}
rt->down(L, R); // 确保将标记传递下去
int mid = (L + R) >> 1;
if (p <= mid)
modify(rt->lc, L, mid, p, val); // 如果p在左子树,递归更新左子树
else
modify(rt->rc, mid + 1, R, p, val); // 如果p在右子树,递归更新右子树
rt->up(); // 更新当前节点数据
return;
}
int query(node *rt, int L, int R, int l, int r) {
if (l <= L && R <= r) {
return rt->data;
}
rt->down(L, R);
int mid = (L + R) >> 1;
int ret = 0;
if (l <= mid)
ret += query(rt->lc, L, mid, l, r);
if (r > mid)
ret += query(rt->rc, mid + 1, R, l, r);
return ret;
}
};
int n, q;
int val[N];
int lowbit(int x) { return x & -x; }
void update1(int r, int k) {
for (int i = r; i <= n; i += lowbit(i))
val[i] += k;
}
int get_sum(int l, int r) {
int ans = 0;
for (int i = r; i; i -= lowbit(i))
ans += val[i];
for (int i = l - 1; i; i -= lowbit(i))
ans -= val[i];
return ans;
}
void solve() {
cin >> n >> q;
vector<DynamicSegtree> tree(n + 1);
vector<int> c(n + 1);
vector<int> v(n + 1);
for (int i = 1; i <= n; i++) {
cin >> c[i];
tree[c[i]].modify(tree[c[i]].rt, 1, MAXA,i, 1);
val[i] = 0;
}
for (int i = 1; i <= n; i++) {
cin >> v[i];
update1(i, v[i]);
}
while (q--) {
int x;
cin >> x;
if (x == 1) {
int p, y;
cin >> p >> y;
tree[c[p]].modify(tree[c[p]].rt, 1, MAXA, p, -1);
tree[y].modify(tree[y].rt, 1, MAXA, p, 1);
c[p] = y;
} else if (x == 2) {
int p, y;
cin >> p >> y;
update1(p, y - v[p]);
v[p] = y;
} else if (x == 3) {
int st, k;
cin >> st >> k;
vector<int> a(k + 1);
for (int i = 1; i <= k; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
a.erase(unique(a.begin()+1, a.end()), a.end());
k = a.size()-1;
int l = 1;
int r = st;
auto check1 = [&](int mid, const vector<int> &a, int st) -> bool {
int sum = 0;
for (int i = 1; i <= k; i++) {
int cnt = tree[a[i]].query(tree[a[i]].rt, 1, MAXA, mid, st);
sum += cnt;
}
return sum == st - mid + 1;
};
auto check2 = [&](int mid, const vector<int> &a, int st) -> bool {
int sum = 0;
for (int i = 1; i <= k; i++) {
int cnt = tree[a[i]].query(tree[a[i]].rt, 1, MAXA, st, mid);
sum += cnt;
}
return sum == mid - st + 1;
};
while (l <= r) {
int mid = (l + r) >> 1;
if (check1(mid, a, st))
r = mid-1;
else
l = mid + 1;
}
int ansl = l;
l = st;
r = n;
while (l <= r) {
int mid = (l + r + 1) >> 1;
if (check2(mid, a, st))
l = mid+1;
else
r = mid - 1;
}
int ansr = r;
cout << get_sum(ansl, ansr) << endl;
}
}
return;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}
/*
1
5 10
1 2 3 1 2
1 10 100 1000 10000
3 3 1 3
3 3 2 2 3
2 5 20000
2 3 200
3 3 2 1 3
3 3 3 1 2 3
1 3 4
2 1 100000
1 2 2
3 1 2 1 2
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3632kb
input:
2 5 10 1 2 3 1 2 1 10 100 1000 10000 3 3 1 3 3 3 2 2 3 2 5 20000 2 3 200 3 3 2 1 3 3 3 3 1 2 3 1 3 4 2 1 100000 1 2 2 3 1 2 1 2 4 1 1 2 3 4 1000000 1000000 1000000 1000000 3 4 4 1 2 3 4
output:
100 110 1200 21211 100010 4000000
result:
ok 6 numbers
Test #2:
score: -100
Wrong Answer
time: 725ms
memory: 514008kb
input:
20000 15 15 1 1 3 3 2 3 3 3 3 3 2 3 2 3 3 634593973 158136379 707704004 998369647 831633445 263092797 937841412 451774682 552617416 483763379 50360475 794662797 74247465 537217773 901809831 3 6 4 1 3 5 10 3 5 7 1 2 3 4 5 9 10 3 4 3 3 8 9 2 13 608033129 3 15 2 3 5 1 9 3 3 8 4 1 3 7 10 2 6 727472865 3...
output:
2689089686 8377825475 1706073651 1439027604 2689089686 792730352 8904867081 8904867081 8270273108 831633445 692051585 2782432626 697783016 883944422 184057757 287523250 184057757 287523250 184057757 1266593842 2667693025 2614711258 4006992373 1767091974 5348541057 5348541057 390631780 2290216252 942...
result:
wrong answer 18th numbers differ - expected: '696388752', found: '287523250'