首页 > 其他分享 >Codeforces Round #771 E

Codeforces Round #771 E

时间:2022-09-04 21:14:31浏览次数:52  
标签:771 int tree Codeforces cin color op Round define

E. Colorful Operations

对于一个序列 有三种操作:
1.将[l,r]区间颜色修改为c
2.将所有c颜色的+x
3.单点查询
操作1好说是个区间修改 操作2就有点逆天了 那我们考虑简化操作2 我们可以列一个数组color 每次操作2 就相当于color[c]+=x;
那么我们操作1 自然在修改的时候就要 加上当前c的再减去改后c的color里面的值即可
但是这个区间修改1 可以想到不是普通的线段树区间修改 他要是颜色不同的话 会递归到最下面 而且会有很多区间
但是他最多的区间产生就是m个 这样想岂不是qmlogm 其实不然 他每次到达m后 要是做一次就会回归1 所以总和不会超过2m 时间复杂度是O(q+qlogm)
注意我们修改时必须要修改same==1的区间

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
const int M = 998244353;
const int mod = 998244353;
#define int long long
#define endl '\n'
#define Endl '\n'
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define _ 0
#define inf 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);
#define ls (2*rt)
#define rs (2*rt + 1)
struct node{
    int l,r,same,lz,col;
}tree[N<<2];
int color[1000010],ans;
void pushup(node &u,node &l,node &r){
    if(l.col==r.col&&l.same&&r.same){
        u.same=1;
        u.col=l.col;
    }else u.same=0;
}
inline void pushup(int u){
    pushup(tree[u],tree[u<<1],tree[u<<1|1]);
}
void pushdown(int i){
    if(tree[i].same!=0){
        tree[i<<1].lz+=tree[i].lz,tree[i<<1|1].lz+=tree[i].lz;//我们只用单点查询 所以不用保证中间节点的正确性
        tree[i<<1].col=tree[i].col,tree[i<<1|1].col=tree[i].col;
        tree[i].lz=0;
    }
}
inline void build(int i,int l,int r){
    tree[i].l=l;tree[i].r=r;
    if(l==r){
        tree[i].col=tree[i].same=1;
        return ;
    }
    int mid=l+r>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    pushup(i);
}
void modify(int i,int l,int r,int k){
    if(tree[i].r<=r && tree[i].l>=l&&tree[i].same){
        tree[i].lz += color[tree[i].col];
        tree[i].col = k;
        tree[i].lz -= color[tree[i].col];
        return ;
    }
    pushdown(i);
    if(tree[i<<1].r>=l)modify(i<<1,l,r,k);
    if(tree[i<<1|1].l<=r)modify(i<<1|1,l,r,k);
    pushup(i);
}

inline node query(int i,int l,int r){
    //if(l>r)return{0};
    node s;
    if(tree[i].l>=l && tree[i].r<=r){
        ans=color[tree[i].col];
        return tree[i];
    }
    pushdown(i);
    if (tree[i<<1].r>=r)  s= query(i<<1,l,r);
    else if(tree[i<<1|1].l<=l) s= query(i<<1|1,l,r);
    else{
        auto left = query(i<<1,l,r);
        auto right= query(i<<1|1,l,r);
        node res;
        pushup(res,left,right);
        return res;
    }
    return s;
}
void solve() {
    int n,m;cin>>n>>m;
    build(1,1,n);
    while(m--){
        char op[10];cin>>op;
        if(op[0]=='C'){
            int l,r,k;cin>>l>>r>>k;
            modify(1,l,r,k);
        }else if(op[0]=='A'){
            int x,k;cin>>x>>k;
            color[x]+=k;
        }else{
            int x;cin>>x;
            cout<<query(1,x,x).lz+ans<<endl;
        }
    }
}
signed main(){
    fast
    int T;T=1;
    while(T--) {
        solve();
    }
    return ~~(0^_^0);
}

标签:771,int,tree,Codeforces,cin,color,op,Round,define
From: https://www.cnblogs.com/ycllz/p/16656080.html

相关文章

  • Educational Codeforces Round 122 E
    E.SpanningTreeQueries纯暴力做法t了我们考虑如何优化我们可以发现要是所有绝对值曲线单调性不变我们MST的答案是可以O(1)转移的res+=(x-prex)*(num1-num2)单调性改变......
  • Codeforces Round #818 (Div. 2) CF1717 解题报告
    CodeforcesRound#818(Div.2)CF1717解题报告ADescription求出满足\(1\lea,b\leN,\frac{\operatorname{lcm}(a,b)}{\gcd(a,b)}\le3\)的二元组\((a,b)\)的数目。......
  • [数论] Codeforces 1717E Madoka and The Best University
    题目大意求\[\sum_{a>0,b>0,c>0,a+b+c=n}\mathrm{lcm}(c,\gcd(a,b))\]\((3\leqn\leq10^5)\)题解\[ans=\sum_{a}\sum_{b}\mathrm{lcm}(n-a-b,\gcd(a,b))\\=\sum_{d......
  • codeforces#818(Div.2)
    算了,不摆烂了,事情太多,没摆烂的时间了。在我研究出如何把某平台上多年积累的流量变现前,就继续用这个博客记录日常吧。之后所有内容基于时间,就懒得设置标签分类之类的了。昨......
  • Codeforces Round #818 (Div. 2) D
      D:题意:由2^n个人进行锦标赛,编号1~2^n,每一场输的人失去比赛资格,赢的人继续。你可以选择他们进行的顺序,以及决定哪一边赢得比赛。你的目标是尽量让编号小的人赢得最......
  • Codeforces Round #818 (Div. 2) D Madoka and The Corruption Scheme
    MadokaandTheCorruptionScheme组合数+思维+贪心首先要思考一开始要如何摆放才是最优秀的按照完全二叉树(根就是最后赢的那个),给所有的点赋予权值,代表需要转换多少......
  • Codeforces Round #818 (Div. 2)
    CodeforcesRound#818(Div.2)D.MadokaandTheCorruptionScheme题目大意给定一场比赛,有\(2^n\)个参赛者。赞助商有k次机会可以调整某一局的结果。而我们想要知道......
  • Codeforces Round #719 (Div. 3) E. Arranging The Sheep(字符串)
    https://codeforces.com/contest/1520你在玩“放羊”游戏。这个游戏的目标是让羊排好队。游戏中的关卡由一个长度为n的字符串描述,由字符“.”组成(空格)和'*'(羊)。......
  • Codeforces Round #818 (Div. 2) C Madoka and Formal Statement
    MadokaandFormalStatement思维如果合法,说明\(a_i\leb_i\),因此也可以认为\(b_i\)就是\(a_i\)最后能变成的最大值根据题意操作,只有\(a_i\lea_{i+1}\)的情况......
  • Codeforces Round #818 (Div. 2) B Madoka and Underground Competitions
    MadokaandUndergroundCompetitions构造在一行里,如果选定了其中一个位置是\(X\),接下来就直接往左和往右每\(k\)个放置一个\(X\)就行了每一行的初始位置根据一开......