【CodeForces】915 E. Physical Education Lessons 线段树

【题目】E. Physical Education Lessons

【题意】10^9范围的区间覆盖,至多3*10^5次区间询问。

【算法】线段树

【题解】每次询问至多增加两段区间,提前括号分段后线段树。

#include<cstdio>
#include<cctype>
#include<set>
#include<algorithm>
using namespace std;
int read(){
    char c;int s=0,t=1;
    while(!isdigit(c=getchar()))if(c==‘-‘)t=-1;
    do{s=s*10+c-‘0‘;}while(isdigit(c=getchar()));
    return s*t;
}
const int maxn=1000010;
struct tree{int l,r,delta,sum,p;}t[maxn*4];//
int au[maxn],av[maxn],k[maxn],a[maxn],n,q,tot;
set<int>s;
void build(int k,int l,int r){
    t[k].l=l;t[k].r=r;t[k].delta=-1;
    if(l==r){t[k].p=t[k].sum=a[l]-a[l-1];return;}
    int mid=(l+r)>>1;
    build(k<<1,l,mid);build(k<<1|1,mid+1,r);
    t[k].p=t[k<<1].p+t[k<<1|1].p;
    t[k].sum=t[k<<1].sum+t[k<<1|1].sum;
}
void modify(int k,int x){t[k].sum=t[k].p*x;t[k].delta=x;}
void down(int k){
    if(~t[k].delta){
        modify(k<<1,t[k].delta);modify(k<<1|1,t[k].delta);
        t[k].delta=-1;
    }
}
void up(int k){t[k].sum=t[k<<1].sum+t[k<<1|1].sum;}
void cover(int k,int l,int r,int x){
    if(l<=t[k].l&&t[k].r<=r){modify(k,x);return;}
    down(k);
    int mid=(t[k].l+t[k].r)>>1;
    if(l<=mid)cover(k<<1,l,r,x);
    if(r>mid)cover(k<<1|1,l,r,x);
    up(k);
}
int main(){
    n=read();q=read();
    for(int i=1;i<=q;i++){
        au[i]=read();av[i]=read();k[i]=read();
        s.insert(au[i]-1);s.insert(av[i]);
    }
    if(*s.begin()==0)s.erase(s.begin());
    s.insert(n);
    for(set<int>::iterator it=s.begin();it!=s.end();it++){
        a[++tot]=*it;
    }
    for(int i=1;i<=q;i++){
        au[i]=lower_bound(a+1,a+tot+1,au[i])-a;
        av[i]=lower_bound(a+1,a+tot+1,av[i])-a;
    }
    n=tot;
    build(1,1,n);
    for(int i=1;i<=q;i++){
        cover(1,au[i],av[i],k[i]-1);
        printf("%d\n",t[1].sum);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/onioncyc/p/8287658.html

时间: 2024-08-03 19:53:19

【CodeForces】915 E. Physical Education Lessons 线段树的相关文章

Codeforces 915 E Physical Education Lessons

题目描述 This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to attend physical education lessons. The end of the term i

Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons(动态开点线段树)

链接: https://codeforces.com/problemset/problem/915/E 题意: This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to atten

Physical Education Lessons CodeForces - 915E (动态开点线段树)

Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to attend physical

[Educational Codeforces Round 36]E. Physical Education Lessons

[Educational Codeforces Round 36]E. Physical Education Lessons <题意概括> 给定一个长度为$N\left(1\leqslant N\leqslant10^{9}\right)$的区间 $Q\left(1\leqslant Q\leqslant3\cdot10^{5}\right)$次将区间$\left[L,R\right]$Set为0或1,每次Set后输出区间总和 <做法> $10_{9}$的范围显然不可能是朴素线段树

CF915E Physical Education Lessons

题意: Alex高中毕业了,他现在是大学新生.虽然他学习编程,但他还是要上体育课,这对他来说完全是一个意外.快要期末了,但是不幸的Alex的体育学分还是零蛋! Alex可不希望被开除,他想知道到期末还有多少天的工作日,这样他就能在这些日子里修体育学分.但是在这里计算工作日可不是件容易的事情: 从现在到学期结束还有 n 天(从 1 到 n 编号),他们一开始都是工作日.接下来学校的工作人员会依次发出 q 个指令,每个指令可以用三个参数 l,r,k 描述: 如果 k=1,那么从 l 到 r (包含端

codeforces 444 C. DZY Loves Colors(线段树)

题目大意: 1 l r x操作 讲 [l,r]上的节点涂成x颜色,并且每个节点的值都加上 |y-x| y为涂之前的颜色 2 l r  操作,求出[l,r]上的和. 思路分析: 如果一个区间为相同的颜色.那么我们才可以合并操作. 所以我们之前找相同的区间就好. 但是问题是如何合并操作. 那么我们定义一个val  表示这个区间每个位置上应该加上的值. pushdown 的时候这个值是可以相加的. #include <cstdio> #include <iostream> #includ

codeforces 446C DZY Loves Fibonacci Numbers 线段树

假如F[1] = a, F[2] = B, F[n] = F[n - 1] + F[n - 2]. 写成矩阵表示形式可以很快发现F[n] = f[n - 1] * b + f[n - 2] * a. f[n] 是斐波那契数列 也就是我们如果知道一段区间的前两个数增加了多少,可以很快计算出这段区间的第k个数增加了多少 通过简单的公式叠加也能求和 F[n]  = f[n - 1] * b + f[n - 2] * a F[n - 1] = f[n - 2] * b + f[n - 3] * a ..

Codeforces 444C DZY Loves Colors 水线段树

题目链接:点击打开链接 水.. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <math.h> #include <set> #include <vector> #include <map> using namespace std; #define ll long long #defi

CodeForces - 383C Propagating tree(dfs + 线段树)

题目大意: 给出一棵树,树上每个节点都有权值,然后有两个操作. 1 x val 在结点x上加上一个值val,x的儿子加上 -val,x的儿子的儿子加上 - (-val),以此类推. 2 x 问x节点的值. 思路分析: 每个节点上加值都是给自己的儿子节点加,而且这个是颗树. 比如样例上的,如果你给node 1加一个值,那么五个节点都加. 再给node 2加个值,2的儿子节点也加了,之前给1加的值也要加到2号节点的儿子. 所以你会发现节点的儿子会存在一个从属的关系. 这样的话,我们可以把所有节点从新