2019牛客暑期多校訓練營(第七場)F Energy stones (樹狀數組 + set)

 題目:

一開始有 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

*/

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章