codeforces 915 E 896 C 珂朵莉树(ODT)

思路:
这题必有平推操作,所以用珂朵莉写,极其方便,也不用考虑数据是否随机,根本卡不了,只需要平推后光速降复杂度即可,然后对贡献,只需暴力加减贡献即可。

参考代码:

/*
 * @Author: vain
 * @Date: 2020
 * @LastEditTime: 2020-09-23 19:00:14
 * @LastEditors: sueRimn
 * @Description: 学不会 dp 的 fw
 * @FilePath: \main\demo.cpp
 */
//#include <bits/stdc++.h>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <math.h>
#include <bitset>
using namespace std;
typedef long long ll;
#define ll long long
//typedef unsigned long long uint;
const int N = 1e3 + 20;
const int maxn = 1e5 + 20;
int a[maxn];
// typedef pair<int, int> p;
// priority_queue<p, vector<p>, greater<p>> m;
//int sum[maxn];
int max(int a, int b) { return a > b ? a : b; }
int min(int a, int b) { return a < b ? a : b; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
void swap(int &x, int &y) { x ^= y, y ^= x, x ^= y; }
int lowbit(int x) { return (x) & (-x); }
struct node
{
    int l, r;
    mutable int w;
    node(int L = 0, int R = -1, int W = 0) : l(L), r(R), w(W){};
    bool operator<(const node &x) const { return l < x.l; }
};
#define It set<node>::iterator
set<node> S;
int ans;
It split(int pos)
{
    It it = S.lower_bound(pos);
    if (it != S.end() && it->l == pos)
        return it;
    it--;
    int L = it->l, R = it->r, w = it->w;
    S.erase(it);
    S.insert(node(L, pos - 1, w));
    return S.insert(node(pos, R, w)).first;
}
void push(int l, int r, int w) 
{

    It itr = split(r + 1), itl = split(l);
    int sum, res;
    sum = res = 0;
    for (It it = itl; it != itr; it++)
    {
        if ((it->w) && (w))
            sum += it -> r - it->l + 1;
        if (!(it->w) && !(w))
            res += it -> r - it->l + 1;
    }
    S.erase(itl, itr);
    S.insert(node(l, r, w));
    if (w)
        ans += r - l + 1 - sum;
    else
        ans += res - (r - l + 1);
}

int main()
{
    // ios::sync_with_stdio(false);
    // cin.tie(0), cout.tie(0);
    int n, q;
    scanf("%d %d", &n, &q);
    S.insert(node(1, n, 1));
    ans = n;
    while (q--)
    {
        int k, l, r;
        scanf("%d %d %d", &l, &r, &k);
        push(l, r, k - 1);
        printf("%d\n", ans);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值