結果
| 問題 | No.3411 Range Clamp Sum |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-12-18 02:19:57 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 1,510 ms / 10,000 ms |
| コード長 | 6,399 bytes |
| 記録 | |
| コンパイル時間 | 3,847 ms |
| コンパイル使用メモリ | 313,684 KB |
| 実行使用メモリ | 200,548 KB |
| 最終ジャッジ日時 | 2025-12-18 02:20:25 |
| 合計ジャッジ時間 | 27,682 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 28 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//2次元セグ木(自動座圧)
template <class S, S (*op)(S, S), S (*e)(), class C = long long int> struct segtree2D_lite {
int H, logH;
std::vector<int> logW, W;
std::vector<C> Yc;
std::vector<std::vector<int>> parent;
std::vector<std::vector<std::pair<int, int>>> child;
std::vector<std::vector<C>> Xc;
std::vector<std::pair<C, C>> preorder;
std::vector<std::vector<S>> d;
segtree2D_lite() {}
void insert(C y, C x){
preorder.emplace_back(y, x);
}
void build(){
std::sort(preorder.begin(), preorder.end());
Yc.resize(preorder.size());
for(int i = 0; i < (int)(preorder.size()); i++)Yc[i] = preorder[i].first;
Yc.erase(std::unique(Yc.begin(), Yc.end()), Yc.end());
logH = ceil_pow2(Yc.size());
H = 1 << logH;
Xc.resize(2 * H), d.resize(2 * H);
logW.resize(2 * H), W.resize(2 * H);
parent.resize(2 * H), child.resize(H);
for(int i = 0, y_id = 0; i < (int)(preorder.size()); i++){
while(Yc[y_id] < preorder[i].first)y_id++;
Xc[y_id + H].emplace_back(preorder[i].second);
}
for(int y = 2 * H - 1; y >= H; y--){
Xc[y].erase(std::unique(Xc[y].begin(), Xc[y].end()), Xc[y].end());
parent[y].resize(Xc[y].size());
logW[y] = ceil_pow2(Xc[y].size());
W[y] = 1 << logW[y];
d[y].resize(2 * W[y], e());
}
for(int y = H - 1; y >= 1; y--){
int Ly_id = 2 * y, Ry_id = 2 * y + 1;
int Lx_id = 0, Rx_id = 0;
while(Lx_id < Xc[Ly_id].size() && Rx_id < Xc[Ry_id].size()){
if(Xc[Ly_id][Lx_id] == Xc[Ry_id][Rx_id]){
parent[Ly_id][Lx_id] = parent[Ry_id][Rx_id] = Xc[y].size();
Xc[y].emplace_back(Xc[Ly_id][Lx_id]);
child[y].emplace_back(Lx_id + W[Ly_id], Rx_id + W[Ry_id]);
Lx_id++, Rx_id++;
}else if(Xc[Ly_id][Lx_id] < Xc[Ry_id][Rx_id]){
parent[Ly_id][Lx_id] = Xc[y].size();
Xc[y].emplace_back(Xc[Ly_id][Lx_id]);
child[y].emplace_back(Lx_id + W[Ly_id], 0);
Lx_id++;
}else{
parent[Ry_id][Rx_id] = Xc[y].size();
Xc[y].emplace_back(Xc[Ry_id][Rx_id]);
child[y].emplace_back(0, Rx_id + W[Ry_id]);
Rx_id++;
}
}
while(Lx_id < Xc[Ly_id].size()){
parent[Ly_id][Lx_id] = Xc[y].size();
Xc[y].emplace_back(Xc[Ly_id][Lx_id]);
child[y].emplace_back(Lx_id + W[Ly_id], 0);
Lx_id++;
}
while(Rx_id < Xc[Ry_id].size()){
parent[Ry_id][Rx_id] = Xc[y].size();
Xc[y].emplace_back(Xc[Ry_id][Rx_id]);
child[y].emplace_back(0, Rx_id + W[Ry_id]);
Rx_id++;
}
parent[y].resize(Xc[y].size());
logW[y] = ceil_pow2(Xc[y].size());
W[y] = 1 << logW[y];
d[y].resize(2 * W[y], e());
}
}
void set(C py, C px, S v){
int y = std::lower_bound(Yc.begin(), Yc.end(), py) - Yc.begin();
y += H;
int x = std::lower_bound(Xc[y].begin(), Xc[y].end(), px) - Xc[y].begin(), nxt_x = parent[y][x];
x += W[y];
d[y][x] = v;
while(x >>= 1)updateX(y, x);
while(y >>= 1){
x = nxt_x, nxt_x = parent[y][x];
d[y][x + W[y]] = op(d[2 * y][child[y][x].first], d[2 * y + 1][child[y][x].second]);
x += W[y];
while(x >>= 1)updateX(y, x);
}
}
S get(C py, C px){
int y = std::lower_bound(Yc.begin(), Yc.end(), py) - Yc.begin();
y += H;
int x = std::lower_bound(Xc[y].begin(), Xc[y].end(), px) - Xc[y].begin();
return d[y][x + W[y]];
}
S prod(C lpy, C lpx, C rpy, C rpx){
int ly = std::lower_bound(Yc.begin(), Yc.end(), lpy) - Yc.begin();
int ry = std::lower_bound(Yc.begin(), Yc.end(), rpy) - Yc.begin();
S sml = e(), smr = e();
ly += H, ry += H ;
while (ly < ry) {
if (ly & 1) sml = op(sml, yline_prod(lpx, rpx , ly++));
if (ry & 1) smr = op(yline_prod(lpx, rpx , --ry), smr);
ly >>= 1, ry >>= 1;
}
return op(sml, smr);
}
S yline_prod(C lpx, C rpx, int y){
int lx = std::lower_bound(Xc[y].begin(), Xc[y].end(), lpx) - Xc[y].begin();
int rx = std::lower_bound(Xc[y].begin(), Xc[y].end(), rpx) - Xc[y].begin();
S sml = e(), smr = e();
lx += W[y], rx += W[y] ;
while (lx < rx) {
if (lx & 1) sml = op(sml, d[y][lx++]);
if (rx & 1) smr = op(d[y][--rx], smr);
lx >>= 1, rx >>= 1;
}
return op(sml, smr);
}
private:
int ceil_pow2(int n) {
int x = 0;
while ((1U << x) < (unsigned int)(n)) x++;
return x;
}
void updateX(int yi, int xi) { d[yi][xi] = op(d[yi][2 * xi], d[yi][2 * xi + 1]); }
};
using S = pair<ll,int>;
S op(S lhs, S rhs){return make_pair(lhs.first + rhs.first, lhs.second + rhs.second);}
constexpr S e(){return make_pair(0, 0);}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, Q, cmd;
cin >> N >> Q;
segtree2D_lite<S, op, e, int> seg;
vector<int> a(N);
for(int i = 0; i < N; i++){
cin >> a[i];
seg.insert(i, a[i]);
}
vector<tuple<int,int,int,int>> query(Q, make_tuple(0, 0, -1, -1));
for(auto &&[a, b, c, d] : query){
cin >> cmd;
if(cmd == 1){
cin >> a >> b;
a--;
seg.insert(a, b);
}else{
cin >> a >> b >> c >> d;
a--;
}
}
seg.build();
for(int i = 0; i < N; i++){
seg.set(i, a[i], make_pair(a[i], 1));
}
for(auto &&[l, r, L, R] : query){
if(L == -1){
seg.set(l, a[l], e());
seg.set(l, r, make_pair(r, 1));
a[l] = r;
}else{
auto [sv0, cnt0] = seg.prod(l, L, r, R);
auto [sv1, cnt1] = seg.prod(l, 0, r, L);
cout << sv0 + (ll)(cnt1) * L + (ll)(r - l - cnt0 - cnt1) * R << '\n';
}
}
}