题意:给定线段树,上面若干个节点坏了,求能表示出多少区间。

区间能被表示出当且仅当拆出来的log个节点都是好的。

解:每个区间在最浅的节点处计算答案。

对于每个节点维护从左边过来能有多少区间,从右边过来能有多少区间即可。

说起来水的一批为什么会被评成黑题啊。

 #include <bits/stdc++.h>

 typedef long long LL;
const int N = ;
const LL MO = ; LL lc[N], rc[N], ans;
int tot, ls[N], rs[N];
bool vis[N]; void add(LL L, LL R, LL l, LL r, int &o) {
//printf("%lld %lld %lld %lld %d \n", L, R, l, r, o);
if(!o) o = ++tot;
if(L <= l && r <= R) {
vis[o] = ;
return;
}
LL mid = (l + r) >> ;
if((r - l + ) & ) mid--;
if(L <= mid) add(L, R, l, mid, ls[o]);
if(mid < R) add(L, R, mid + , r, rs[o]);
} void cal(LL l, LL r, int &o) {
if(!o) {
o = ++tot;
LL len = (r - l + ) % MO;
ans = (ans + len * (len + ) / ) % MO;
//printf("[%lld %lld] ans += %lld = %lld \n", l, r, len * (len + 1) / 2, ans);
lc[o] = rc[o] = (len - + MO) % MO;
return;
}
if(l == r) {
ans += (!vis[o]);
//if(!vis[o]) printf("[%lld %lld] ans++ = %lld \n", l, r, ans);
return;
}
LL mid = (l + r) >> ;
if((r - l + ) & ) mid--;
cal(l, mid, ls[o]);
cal(mid + , r, rs[o]);
/// cal
ans = (ans + lc[rs[o]] * rc[ls[o]] % MO) % MO;
//printf("[%lld %lld] ans += %lld * %lld = %lld \n", l, r, lc[rs[o]], rc[ls[o]], ans);
if(!vis[ls[o]]) {
ans = (ans + lc[rs[o]]) % MO;
//printf("[%lld %lld] ans += %lld = %lld \n", l, r, lc[rs[o]], ans);
}
if(!vis[rs[o]]) {
ans = (ans + rc[ls[o]]) % MO;
//printf("[%lld %lld] ans += %lld = %lld \n", l, r, rc[ls[o]], ans);
}
if(!vis[o]) ans++;
lc[o] = lc[ls[o]];
if(!vis[ls[o]]) {
lc[o] = (lc[o] + lc[rs[o]] + ) % MO;
}
rc[o] = rc[rs[o]];
if(!vis[rs[o]]) {
rc[o] = (rc[o] + rc[ls[o]] + ) % MO;
}
//printf("l = %lld r = %lld lc = %lld rc = %lld \n", l, r, lc[o], rc[o]);
return;
} int main() { //printf("%d", sizeof(lc) * 3 / 1048576); LL n; int m, root = ;
scanf("%lld%d", &n, &m);
for(int i = ; i <= m; i++) {
LL l, r;
scanf("%lld%lld", &l, &r);
add(l, r, 1ll, n, root);
}
cal(, n, root);
printf("%lld\n", ans);
return ;
}

AC代码

洛谷P5111 zhtobu3232的线段树的更多相关文章

  1. P5111 zhtobu3232的线段树

    P5111 zhtobu3232的线段树 维护左子树右子树的贡献和跨区间贡献 #include<bits/stdc++.h> using namespace std; typedef lo ...

  2. 【BZOJ】1012: [JSOI2008]最大数maxnumber /【洛谷】1198(线段树)

    Description 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2. 插 ...

  3. 洛谷题解P4314CPU监控--线段树

    题目链接 https://www.luogu.org/problemnew/show/P4314 https://www.lydsy.com/JudgeOnline/problem.php?id=30 ...

  4. 洛谷P3372/poj3468(线段树lazy_tag)(询问区间和,支持区间修改)

    洛谷P3372 //线段树 询问区间和,支持区间修改 #include <cstdio> using namespace std; struct treetype { int l,r; l ...

  5. 洛谷P4065 [JXOI2017]颜色(线段树)

    题意 题目链接 Sol 线段树板子题都做不出来,真是越来越菜了.. 根据题目描述,一个合法区间等价于在区间内的颜色没有在区间外出现过. 所以我们可以对于每个右端点,统计最长的左端点在哪里,刚开始以为这 ...

  6. 洛谷P3960 列队 NOIp2017 线段树/树状数组/splay

    正解:动态开点线段树 解题报告: 传送门! 因为最近学主席树的时候顺便get到了动态开点线段树?刚好想起来很久很久以前就想做结果一直麻油做的这题,,,所以就做下好了QAQ 然后说下,这题有很多种方法, ...

  7. 题解——洛谷P2781 传教(线段树)

    可以说是数据结构学傻了的典型案例了 昨天跳到这题上 然后思考了一下 噫!好!线段树裸题 然后打完板子,发现\(  n \le 10^9 \) 显然线段树直接做不太行 然后这题又只有普及的难度 然后我就 ...

  8. 洛谷P4198 楼房重建(线段树)

    题意 题目链接 Sol 别问我为什么发两遍 就是为了骗访问量 这个题的线段树做法,,妙的很 首先一个显然的结论:位置\(i\)能被看到当且仅当\(\frac{H_k}{k} < \frac{H_ ...

  9. 2018.08.16 洛谷P1471 方差(线段树)

    传送门 线段树基本操作. 把那个方差的式子拆开可以发现只用维护一个区间平方和和区间和就可以完成所有操作. 同样区间修改也可以简单的操作. 代码: #include<bits/stdc++.h&g ...

随机推荐

  1. Day 5-8 自定义元类控制类的实例化行为

    __call__方法: 对象后面加括号,触发执行. 注:构造方法的执行是由创建对象触发的,即:对象 = 类名() :而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类( ...

  2. Hadoop2.0 Namenode HA实现方案

    Hadoop2.0 Namenode HA实现方案介绍及汇总 基于社区最新release的Hadoop2.2.0版本,调研了hadoop HA方面的内容.hadoop2.0主要的新特性(Hadoop2 ...

  3. 用 Python 写一个多进程兼容的 TimedRotatingFileHandler

    我前面有篇文章已经详细介绍了一下 Python 的日志模块.Python 提供了非常多的可以运用在各种不同场景的 Log Handler. TimedRotatingFileHandler 是 Pyt ...

  4. github & markdown & collapse & table

    github & markdown collapse & table https://github.com/Microsoft/TypeScript/issues/30034 GitH ...

  5. Java多线程6:Synchronized锁代码块(this和任意对象)

    一.Synchronized(this)锁代码块 用关键字synchronized修饰方法在有些情况下是有弊端的,若是执行该方法所需的时间比较长,线程1执行该方法的时候,线程2就必须等待.这种情况下就 ...

  6. layui内部使用jQuery

    layui是基于jQuery的框架,本身自带jQuery 根据官方推荐,是使用自带的好一点 这里记一下内部使用jQuery的方法: layui.use('jquery', function(){ va ...

  7. ADO工具类

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data; ...

  8. How to remove unwant Explorer Context Menu

    HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell

  9. kubernetes 容器挂载 ceph rbd 卷的平滑扩容方法

    https://blog.csdn.net/aixiaoyang168/article/details/79120095

  10. [Codeforces266E]More Queries to Array...——线段树

    题目链接: Codeforces266E 题目大意:给出一个序列$a$,要求完成$Q$次操作,操作分为两种:1.$l,r,x$,将$[l,r]$的数都变为$x$.2.$l,r,k$,求$\sum\li ...