标签:else 题目 cout build root bit lse names res
题目链接
维护一个区间最小值同时维护一个区间最小值的减法
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N];
int n,k;
struct node{
int l,r;
int v,add;
}tr[N * 4];
void pushup(int u)
{
tr[u].v = min(tr[u << 1].v , tr[u << 1 | 1].v);
}
void pushdown(int u)
{
auto &root = tr[u], &left = tr[u << 1], &right = tr[u << 1 | 1];
if(root.add){
left.add += root.add, left.v -= root.add;
right.add += root.add, right.v -= root.add;
root.add = 0;
}
}
void build(int u,int l,int r)
{
if(l == r) tr[u] = {r,r,a[r],0};
else{
tr[u] = {l,r};
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
}
void modify(int u,int l,int r,int d)
{
if(tr[u].l == l && tr[u].r == r){
tr[u].v -= d;
tr[u].add += d;
}
else{
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if(r <= mid) modify(u << 1, l, r, d);
else if(l > mid) modify(u << 1 | 1, l , r ,d);
else modify(u << 1, l , mid,d), modify(u << 1 | 1, mid + 1, r, d);
pushup(u);
}
}
int query(int u,int l,int r)
{
if(tr[u].l == l && tr[u].r == r) return tr[u].v;
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if(r <= mid) return query(u << 1,l , r);
else if(l > mid) return query(u << 1 | 1, l, r);
else return min(query(u << 1, l , mid), query(u << 1 | 1, mid + 1, r));
}
signed main()
{
int _; cin >> _;
while(_ --){
scanf("%lld%lld",&n,&k);
for(int i = 1; i <= n; i ++) scanf("%lld",&a[i]);
build(1,1,n);
int res = 0;
for(int i = k; i <= n; i ++){
int t = query(1,i - k + 1,i);
res += t;
modify(1,i - k + 1,i, t);
}
cout << res << endl;
}
}
标签:else 题目 cout build root bit lse names res
原文地址:https://www.cnblogs.com/acm-Patrick/p/14725874.html