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);
    }
}

猜你喜欢

转载自blog.csdn.net/yangzijiangac/article/details/108759224
今日推荐