Codeforces Round #771 (Div. 2) E. Colorful Operations 珂朵莉树+树状数组

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
#define int long long
const int N=2e6+10;
namespace ct{
    struct node{
        int l, r;
        mutable int val;
        node (int lpos): l(lpos) {}
        node (int lpos, int rpos, int vall): l(lpos), r(rpos), val(vall) {}
        bool operator< (const node &a) const { return l < a.l; }
    };

    set<node> s;
    using sit = set<node>::iterator;

    inline void init(int l, int r){ s.insert(node(l, r, 1)); }

    //^ChthollyTree-SetSplit
    sit split(int pos){
        sit it = s.lower_bound(node(pos));
        if(it != s.end() && it -> l == pos) return it;
        --it;
        int l = it -> l, r = it -> r, val = it -> val;
        s.erase(it);
        if(l <= pos - 1) s.insert(node(l, pos - 1, val));
        if(r >= pos) return s.insert(node(pos, r, val)).first;
        return s.end();
    }

    //^ChthollyTree-PartValueAssign
    void assign(int l, int r, int val){
        sit itr = split(r + 1), itl = split(l);
        s.erase(itl, itr);
        s.insert(node(l, r, val));
    }

    //^ChthollyTree-PartValueAddition
    void add(int l, int r, int val){
        sit itr = split(r + 1), itl = split(l);
        //for(auto it = itl; it != itr; it++) it -> val += val;
        while(itl != itr) itl -> val += val, itl++;
    }

    //^ChthollyTree-SinglePointQuery
    int query(int pos){
        auto it = s.upper_bound(pos);
        --it;
        return it -> val;
    }
}//ChthollyTreeTemplate
struct BIT{
    int a[N],n;
    void init(int nn){n=nn;for(int i=1;i<n;i++) a[i]=0;}
    int lowbit(int x){
        return x&(-x);
    }
    int getsum(int x){
        int ans=a[x];
        while((x-=lowbit(x))>0){
            ans+=a[x];
        }
        return ans;
    }
    void add(int x,int y){
        a[x]+=y;
        while((x+=lowbit(x))<=n){
            a[x]+=y;
        }
    }
}bit1;
int n,q,cnt[N];char ch[N];
void changecol(int l,int r,int col){
    ct::sit posr=ct::split(r+1),posl=ct::split(l);
    for(auto it=posl;it!=posr;){
        bit1.add(it->r+1,-cnt[it->val]);
        bit1.add(it->l,cnt[it->val]);
        auto nxt=next(it);
        ct::s.erase(it);
        it=nxt;
    }
    ct::s.insert(ct::node(l,r,col));
    bit1.add(r+1,cnt[col]);
    bit1.add(l,-cnt[col]);
}

int query(int x){
    int sum=bit1.getsum(x);
    int col=ct::query(x);
    return sum+cnt[col];

}
//65864805
int32_t main(){
    scanf("%lld%lld",&n,&q);
    ct::init(1,n);bit1.init(N);
    while(q--){
        scanf("%s",ch+1);
        if(ch[1]=='C'){
            int l,r,col;scanf("%lld%lld%lld",&l,&r,&col);
            changecol(l,r,col);
        }else if(ch[1]=='A'){
            int x,y;scanf("%lld%lld",&x,&y);cnt[x]+=y;
        }else{
            int x;scanf("%lld",&x);
            printf("%lld\n",query(x));
        }
//        cout<<(ct::s.size())<<endl;
//        for(auto it=(ct::s.begin());it!=(ct::s.end());it++){
//            cout<<(it->l)<<" "<<(it->r)<<" "<<(it->val)<<endl;
//        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值