题目:
一开始有 n 个石头, 每个石头都有一个初始的 能量 e[i] , 然后时候的能量以每秒 l[i] 的速度增加, 每个石头的能量有一个上限 c[i].
现在有 m 个询问, t, l, r, 代表在 t 秒, 询问 区间 [l,r] 的石头能量之和, 然后询问之后区间的石头能量变成 0.
最后输出一个答案, 所有能量之和.
思路:
考虑每个石头对答案的贡献, 首先要算出来每个石头能量集满要多长时间 d.
然后要统计每个石头询问的时间差,
时间差 >= d, 加上 c. 统计个数 直接 * c,
时间差 < d, 加上 时间差*l[i].统计所有的时间和 * l.
然后还要计算一下初始状态. e.随便搞搞就好了.
然后我们如何维护时间差呢?
用 set + 树状数组维护,
每次把时间点加入到 set 中, 就会有可能减少一个时间段,加上两个小的时间段,
然后我们用树状数组维护时间段.
两个树状数组分别维护
1.每个时间段的个数. 求 >= d 一共有多少个时间段.
2.每个时间段一共有多少时间. 求 < d 一共有多少时间,
要注意除0 的情况.
#include<bits/stdc++.h>
#define low(x) (x & (-x))
using namespace std;
const int N= 1e5+100;
const int M = 1e6+10;
set<int>s;
vector<int>f[N],g[N];
int e[N],l[N],c[N],d[N],n,m;
int t,x,y;
int mx;
namespace BIT{
long long c[M],d[M];
void init(){
for (int i = 0; i <= mx; ++i)
c[i] = 0, d[i] = 0;
}
void add1(int x, long long y){
for (; x <= mx; x += low(x)) c[x] += y;
}
void add2(int x, long long y){
for (; x <= mx; x += low(x)) d[x] += y;
}
long long ask1(int x){
long long ans = 0;
for (; x > 0; x -= low(x)) ans += c[x];
return ans;
}
long long ask2(int x){
long long ans = 0;
for (; x > 0; x -= low(x)) ans += d[x];
return ans;
}
};
int main(){
int T,cas = 1; scanf("%d",&T);
while(T--){
scanf("%d",&n);
mx = 0;
for (int i = 1; i<= n; ++i){
scanf("%d%d%d",&e[i],&l[i],&c[i]);
if (l[i] == 0 || c[i] == 0) d[i] = 0; else d[i] = (c[i] - 1) / l[i] + 1;
mx = max(mx,c[i]);
}
scanf("%d",&m);
for (int i = 1; i<= m; ++i){
scanf("%d%d%d",&t,&x,&y);
f[x].push_back(t);
g[y].push_back(t);
mx = max(mx,t);
}
BIT::init();
s.clear();
int now,now1;
set<int>::iterator it;
long long ans = 0,tot = 0;
for (int i = 1; i <= n; ++i){
for (int j = 0; j < (int)f[i].size(); ++j){
it = s.lower_bound(f[i][j]);
tot++;
if (s.size() == 0){
s.insert(f[i][j]);
continue;
}
if (it == s.begin()){
now = *it - f[i][j];
BIT::add1(now,now);
BIT::add2(now,1);
} else if (it == s.end()){
it--;
now = f[i][j] - *it;
BIT::add1(now,now); BIT::add2(now,1);
} else {
now = *it; it--; now1 = *it;
BIT::add1(now - now1, -(now - now1));
BIT::add2(now-now1,-1);
BIT::add1(now-f[i][j], now - f[i][j]);
BIT::add2(now-f[i][j], 1);
BIT::add1(f[i][j] - now1, f[i][j] - now1);
BIT::add2(f[i][j] - now1, 1);
}
s.insert(f[i][j]);
}
long long ans1 = (tot - BIT::ask2(d[i]-1) -1) * c[i];
// printf("%d %lld\n",d[i]-1,BIT::ask2(d[i]-1));
long long ans2 = BIT::ask1(d[i]-1) * l[i];
long long ans3 = min(1ll*c[i],1ll*e[i] + 1ll*l[i]*(*s.begin()));
if (tot){
if (d[i] == 0) ans += e[i]; else ans += ans1 + ans2 + ans3;
}
// printf("%d %lld %lld %lld %lld\n",i,ans1,ans2,ans3,ans);
for (int j = 0; j < (int)g[i].size(); ++j){
tot--;
if (s.size() == 1){
s.erase(g[i][j]);
continue;
}
it = s.find(g[i][j]);
if (it == s.begin()){
it++;
now = *it - g[i][j];
BIT::add1(now,-now);
BIT::add2(now,-1);
} else{
it++;
if (it == s.end()){
it--; it--;
now = g[i][j] - *it;
BIT::add1(now, -now);
BIT::add2(now,-1);
} else{
now = *it; it--; it--;
now1 = *it;
BIT::add1(now-g[i][j],-(now - g[i][j]));
BIT::add2(now-g[i][j], -1);
BIT::add1(g[i][j] - now1, -(g[i][j] - now1));
BIT::add2(g[i][j] - now1, -1);
BIT::add1(now - now1, now-now1);
BIT::add2(now-now1,1);
}
}
s.erase(g[i][j]);
}
}
printf("Case #%d: %lld\n",cas++,ans);
for (int i = 1; i<= n; ++i)
g[i].clear(),f[i].clear();
}
return 0;
}
/*
1
4
2 1 4
2 2 5
2 1 10
2 3 2
2
1 1 4
4 2 3
*/