CF 921 div2 e

// Problem: E. Space Harbour
// Contest: Codeforces - Codeforces Round 921 (Div. 2)
// URL: https://codeforces.com/contest/1925/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<int>;
using PII = pair<ll,ll>;
using VVI = vector<vector<int>>;
const ll mod = 998244353;
const ll INF = 1e18;
const int N = 3e5 + 10;
int n,m,q;


struct segtree{
    ll sum[N << 2];
    void pushup(int p){
        sum[p] = sum[p << 1] + sum[p << 1 | 1];
    }
    void change(int p , int left , int right , int x , ll val){
        if(left == right){
            sum[p] = val;
            return ;
        }
        int mid = (left + right) >> 1;
        if(x <= mid) change(p << 1 , left , mid , x , val);
        else change(p << 1 | 1 , mid + 1 , right , x , val);
        pushup(p);
    }
    ll query(int p ,int left , int right , int ql , int qr){
    	if(ql > qr) return 0;
    	
        if(ql <= left && right <= qr){
            return sum[p];
        }
        int mid = (left + right) >> 1;
        ll res = 0;
        if(ql <= mid) res += query(p << 1 , left , mid , ql , qr);
        if(qr > mid) res += query(p << 1 | 1 , mid + 1 , right , ql , qr);
        return res;
    }




}t;

ll calc(ll p , ll u){
	if(u <= p) return 0;
    return (u - p) * (u - p + 1) / 2;
}
PII p[N];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m>>q;
    VI x(n + 2 , 0);
    VI v(n + 2 , 0);
    //vector<PII> p(n + 2 , 0);
    map<int,int> mp;
    for(int i = 1 ; i <= m ; i++)cin>>x[i];
    for(int i = 1 ; i <= m ; i++){
    	cin>>v[i];
    	p[i] = {x[i] , v[i]};
    }
    sort(p + 1 , p + 1 + m);
    
    for(int i = 1 ; i <= m ; i++){
    	x[i] = p[i].first , v[i] = p[i].second;
        mp.insert({x[i] , v[i]});
        //cout<<x[i]<<" "<<v[i]<<"\n";
        //cout<<v[i - 1] * calc(x[i - 1]  + 1 , x[i])<<"\n";
        t.change(1 , 1 , n , x[i] , v[i - 1] * calc(x[i - 1]  + 1 , x[i]));
    }
  /*  cout<<endl;
    cout<<t.query(1 , 1 , n , 1 , n)<<'\n';
    */
    while(q--){
        int op;
        cin>>op;
        if(op == 1){
            int xx,vv;
            cin>>xx>>vv;
            auto it = mp.upper_bound(xx);
            PII nex = *it;
            PII pre = *(--it);
            //if(nex.first == n) pre
            //cout<<pre.first<<" "<<pre.second<<"\n";
            //cout<<pre.second * calc(pre.first + 1 , xx)<<"\n";
            t.change(1 , 1 , n , xx , pre.second * calc(pre.first + 1 , xx));
            t.change(1 , 1 , n , nex.first , vv * calc(xx + 1 , nex.first));
            mp.insert({xx , vv});
        }else{
            int l,r;
            cin>>l>>r;

            // l             r
            //    ql    qr
            ll res = 0;
            int ql,qr;
            auto it = mp.lower_bound(l);
            PII nex = *it;
            PII pre = *(--it);
            //cout<<pre.second * calc(l , nex.first)<<"\n";
            res += pre.second * calc(l , nex.first);
            ql = nex.first + 1;
            
            
            it = mp.lower_bound(r);
            nex = *it;
            pre = *(--it);
            qr = nex.first;
            res -= pre.second * calc(r + 1  , nex.first);
           /* cout << pre.second * calc(r + 1  , nex.first)<<'\n';
            cout<<ql<<" "<<qr<<"\n";*/
           /* cout<<t.query(1 , 1 , n , ql , qr)<<'\n';*/
            res += t.query(1 , 1 , n , ql , qr);
            cout<<res<<"\n";
        }
        ///cout<<endl;
    }


}


//5 4 3 2 1
//5 4 3 2 1

Problem - E - Codeforces

本质上不是一个很困难的题目,只要是我调代码能力太拉了

通过map实现查询的大于和小于的点,迭代器要先--

采用类似于分块的做法处理 2 查询  ,中间的利用线段树直接计算,两边少算的部分利用等差数列公式计算。但是注意在线段树上做查询的时候左区间要 +1 , 因为左区间上的val并不代表这个点的值,而是一段区间的值放在了这个港口上,这个点本上的值是0,要选的是从港口 + 1开始

右边多算的是从[r + 1 , nex]

左边少算的是从[l , nex] 

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<div id="wea_rich_text_default_font" style="font-family:微软雅黑;font-size:12;"><p><img alt="" src="/weaver/weaver.file.FileDownload?fileid=aaa9aee4717d33272bd7ea028fa03118b693919f23b18febf9f6cee1158e8f4cf027542c71c8cf98d63770ccdf3bd1750e6b92e28c43dccd4" /></p><div class="ckeditor-html5-video" data-widget="html5video" style="text-align:left"><video controls="controls" src="/weaver/weaver.file.FileDownload?fileid=aad6f413f83191673980c5ee24b412880d6b9e8703caca411faec3276fe8133f5fa7e34630ca89ace63770ccdf3bd175071362141037cfb4e&download=1" style="max-width:100%"> </video></div><table border="1" cellpadding="1" style="width:500px;"> <tbody> <tr> <td style="padding: 1px;">1</td> <td style="padding: 1px;">1</td> </tr> <tr> <td style="padding: 1px;">2</td> <td style="padding: 1px;">2</td> </tr> <tr> <td style="padding: 1px;">3</td> <td style="padding: 1px;">3<a href="http://localhost:8080/wui/index.html#/main/portal/portal-1-1?menuIds=0,1&menuPathIds=0,1&_key=zq8830" target="_blank">http://localhost:8080/wui/index.html#/main/portal/portal-1-1?menuIds=0,1&menuPathIds=0,1&_key=zq8830</a></td> </tr> </tbody></table><p>测试<a href="http://localhost:8080/wui/index.html#/main/portal/portal-1-1?menuIds=0,1&menuPathIds=0,1&_key=zq8830" target="_blank">http://localhost:8080/wui/index.html#/main/portal/portal-1-1?menuIds=0,1&menuPathIds=0,1&_key=zq8830</a></p><p> </p><p>修改一下吧 qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq<img alt="" src="/weaver/weaver.file.FileDownload?fileid=a7617945ec5f52ec80aaa43ee8504de0a1b14d5eca4a98834494c85349762c626dec7ba8d0da277106ee600d27743f4e44f710fbddd167603" /></p></div>
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值