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", n-sum[1]);
    }

    return 0;
}

原文地址:https://www.cnblogs.com/YDDDD/p/12238018.html

时间: 2024-10-11 20:46:45

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

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Educational Codeforces Round 36 (Rated for Div. 2) ---d

D. Almost Acyclic Graph 首先判环可以用拓扑来实现. 暴力解法自然是枚举每一条边,删除,判断是否存在环. 解法一: 对于指向同一个点的边,在拓扑排序中看删除他们事实上是等价的,即那个点入度-1,那么我们枚举所有的点即可. 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int inf=1e5+10; 4 int n,m; 5 int tot,fi[inf],nxt[inf],to[inf],in[inf];

Educational Codeforces Round 36 (Rated for Div. 2) G. Coprime Arrays

求a_i 在 [1,k]范围内,gcd(a_1,a_2...,a_n) = 1的a的数组个数. F(x)表示gcd(a_1,a_2,...,a_n) = i的a的个数 f(x)表示gcd(a_1,a_2,...,a_n) = ki的a的个数(实际上就是i的倍数) f(x) = segma(x | d) F(d) F(x) = segma(x | d) mu(d / x) * f(d) F(1) = segma(d,1,k) mu(d) * f(d) f(d) = (k / d)^n 由于k变化时

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers

原文链接:https://www.cnblogs.com/xwl3109377858/p/11404050.html Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburg

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm