zoukankan      html  css  js  c++  java
  • 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 attend physical education lessons. The end of the term is very soon, but, unfortunately, Alex still hasn't attended a single lesson!

    Since Alex doesn't want to get expelled, he wants to know the number of working days left until the end of the term, so he can attend physical education lessons during these days. But in BSU calculating the number of working days is a complicated matter:

    There are n days left before the end of the term (numbered from 1 to n), and initially all of them are working days. Then the university staff sequentially publishes q orders, one after another. Each order is characterised by three numbers l, r and k:

    If k = 1, then all days from l to r (inclusive) become non-working days. If some of these days are made working days by some previous order, then these days still become non-working days;
    If k = 2, then all days from l to r (inclusive) become working days. If some of these days are made non-working days by some previous order, then these days still become working days.
    Help Alex to determine the number of working days left after each order!

    思路:

    求1-n里的0的个数。
    动态开点线段树,对于每次更新,每个节点先判断是不是新的,是新的就重新建立即可。

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int MAXN = 4e5+10;
    const int MOD = 1e9+7;
    const int INF = 1e9;
    
    int n, q;
    int add[MAXN*40], sum[MAXN*40], ln[MAXN*40], rn[MAXN*40];
    int rot = 0, cnt = 0;
    
    void pushdown(int root, int l, int r)
    {
        if (add[root] != -1)
        {
            if (!ln[root]) ln[root] = ++cnt;
            if (!rn[root]) rn[root] = ++cnt;
            int mid = (l+r)/2;
            add[ln[root]] = add[root];
            add[rn[root]] = add[root];
            sum[ln[root]] = add[root]*(mid-l+1);
            sum[rn[root]] = add[root]*(r-mid);
            add[root] = -1;
        }
    }
    
    void update(int &root, int l, int r, int ql, int qr, int v)
    {
        if (!root) root = ++cnt;
        if (ql <= l && r <= qr)
        {
            add[root] = v;
            sum[root] = v*(r-l+1);
            return;
        }
        pushdown(root, l, r);
        int mid = (l+r)/2;
        if (ql <= mid) update(ln[root], l, mid, ql, qr, v);
        if (qr > mid) update(rn[root], mid+1, r, ql, qr, v);
        sum[root] = sum[ln[root]]+sum[rn[root]];
    }
    
    int main()
    {
        //dsu on tree 启发式合并
        ios::sync_with_stdio(stdin);
        cin.tie(0), cout.tie(0);
        scanf("%d%d", &n, &q);
        memset(add, -1, sizeof(add));
        int l, r, k;
        while(q--)
        {
            scanf("%d%d%d", &l, &r, &k);
            update(rot, 1, INF, l, r, k == 2 ? 0 : 1);
            printf("%d
    ", n-sum[1]);
        }
    
        return 0;
    }
    
  • 相关阅读:
    多线程 wait和sleep区别
    什么是分布式系统,如何学习分布式系统
    Mybatis传多个参数(三种解决方案)
    UML中类之间的几种关系
    前缀、中缀、后缀表达式
    数据库连接池c3p0和dbcp
    代码收藏
    spark教程(12)-生态与原理
    spark教程(10)-sparkSQL
    spark教程(九)-操作数据库
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12238018.html
Copyright © 2011-2022 走看看