P17543 [JAG 2026 Summer Camp #2] Deque Bracket Optimization
发现
- 每次把两个数放到大根堆里,取堆顶计入答案。最后把答案加上
w_1 。
接下来考虑带修,我们先考虑
对于
对于
对于
时间复杂度线性对数。
struct info {
ll ans;
int fir, lst;
priority_queue<int> A;
priority_queue<int, vector<int>, greater<int>> B;
void clr() {
ans = 0, fir = lst = -2e9;
while(!A.empty()) A.pop();
while(!B.empty()) B.pop();
return;
}
void front(int x, int y) {
if(fir == -2e9) {
fir = ans = y, lst = x;
return;
}
ans -= fir, ans += y;
swap(y, fir);
if(x > y) swap(x, y);
if(B.empty()) {
ans += y;
A.push(x), B.push(y);
return;
}
int mn = B.top();
ans -= mn, B.pop();
if(mn < x) ans += x + y, B.push(x), B.push(y), A.push(mn);
else ans += mn + y, B.push(mn), B.push(y), A.push(x);
return;
}
void back(int x, int y) {
if(fir == -2e9) {
fir = ans = x, lst = y;
return;
}
swap(lst, y);
A.push(x), A.push(y);
int mx = A.top();
A.pop();
ans += mx, B.push(mx);
return;
}
} X, Y;
int q;
void TEST() {
cin>>q;
deque<int> dq;
X.clr(), Y.clr();
while(q --) {
int op, x, y;
cin>>op>>x>>y;
if(op == 1) {
if(dq.empty()) X.front(x, y);
else X.front(x, y), Y.front(dq.front(), x);
dq.push_front(x), dq.push_front(y);
} else if(op == 3) {
if(dq.empty()) X.back(x, y);
else X.back(x, y), Y.back(dq.back(), x);
dq.push_back(x), dq.push_back(y);
} else if(op == 2) {
if(dq.empty()) X.front(y, x);
else swap(X, Y), X.front(dq.front(), x), X.back(dq.back(), y);
dq.push_front(x), dq.push_back(y);
}
cout<<X.ans<<endl;
}
return;
}