题目链接:D. Frets On Fire

思路:明明可以离散化+二分写,思路硬是歪到了线段树上,自闭了,真实弟弟,怪不得其他人过得那么快

只和查询的区间长度有关系,排完序如果相邻的两个点的差值小于等于查询的区间长度,那么给结果带来的变化就会新增差值个数,如果大于区间长度那么就会新增区间长度个数

维护的话,线段树和二分都可以,二分需要离散化处理,再给差值排个序,每次找到第一个大于当前区间长度的差值位置就好了,(没实现,但是理论上应该没问题)

线段树直接动态开点可以不用离散化。。

实现代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mid ll m = (l + r) /2
const ll M = 1e5+;
#define ROF(i,a,b) for(ll i=a;i>=b;i--)
ll sum[M*],num[M*];
ll ls[M*],rs[M*];
ll idx;
void update(ll p,ll c,ll l,ll r,ll &rt){
if(!rt) rt = ++idx;
sum[rt] += c;
num[rt] += ;
if(l == r){
return ;
}
mid;
if(p <= m) update(p,c,l,m,ls[rt]);
else update(p,c,m+,r,rs[rt]);
} ll query(ll L,ll R,ll l,ll r,ll rt){
if(L <= l&&R >= r){
return sum[rt];
}
mid;
ll ret = ;
if(L <= m) ret += query(L,R,l,m,ls[rt]);
if(R > m) ret += query(L,R,m+,r,rs[rt]);
return ret;
} ll ask(ll L,ll R,ll l,ll r,int rt){
if(L <= l&&R >= r){
return num[rt];
}
mid;
ll ret = ;
if(L <= m) ret += ask(L,R,l,m,ls[rt]);
if(R > m) ret += ask(L,R,m+,r,rs[rt]);
return ret;
}
ll a[*M];
int main()
{
ll n,m,x,y,rt = ;
scanf("%lld",&n);
for(ll i = ;i <= n;i ++){
scanf("%lld",&a[i]);
}
sort(a+,a++n);
for(ll i = ;i <= n;i ++){
ll num = a[i] - a[i-];
update(num,num,,1e18,rt);
}
scanf("%lld",&m);
for(ll i = ;i <= m;i ++){
scanf("%lld%lld",&x,&y);
ll num = y-x+;
ll ans = num;
ans += query(,num,,1e18,rt);
//cout<<ans<<" ";
ans += ask(num+,1e18,,1e18,rt)*num;
printf("%lld\n",ans);
}
}

Codeforces Global Round 2 D. Frets On Fire (动态开点线段树,沙雕写法)的更多相关文章

  1. codeforces 893F - Physical Education Lessons 动态开点线段树合并

    https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...

  2. Physical Education Lessons CodeForces - 915E (动态开点线段树)

    Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a fir ...

  3. Codeforces Round #539 (Div. 1) C. Sasha and a Patient Friend 动态开点线段树

    题解看这里 liouzhou_101的博客园 更简洁的代码看这里: #include <bits/stdc++.h> using namespace std; typedef long l ...

  4. codeforces 915E - Physical Education Lessons 动态开点线段树

    题意: 最大$10^9$的区间, $3*10^5$次区间修改,每次操作后求整个区间的和 题解: 裸的动态开点线段树,计算清楚数据范围是关键... 经过尝试 $2*10^7$会$MLE$ $10^7$会 ...

  5. Codeforces 803G Periodic RMQ Problem ST表+动态开节点线段树

    思路: (我也不知道这是不是正解) ST表预处理出来原数列的两点之间的min 再搞一个动态开节点线段树 节点记录ans 和标记 lazy=-1 当前节点的ans可用  lazy=0 没被覆盖过 els ...

  6. Codeforces 915E. Physical Education Lessons(动态开点线段树)

    E. Physical Education Lessons 题目:一段长度为n的区间初始全为1,每次成段赋值0或1,求每次操作后的区间总和.(n<=1e9,q<=3e5) 题意:用线段树做 ...

  7. CodeForces - 915E 动态开点线段树

    题目 晚上有n个亮着的灯泡,标号从1到n. 现在存在2种操作,如下: 操作1,关掉标号 [l,r] 区间的灯 操作2,打开标号 [l,r] 区间的灯 下面有q次询问,每次询问执行其中一种操作,询问格式 ...

  8. Codeforces Global Round 2 题解

    Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...

  9. Codeforces Global Round 3

    Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...

随机推荐

  1. RPM打包原理、示例、详解及备查

    原文地址:https://blog.csdn.net/qq_16542775/article/details/80961213 RPM(Redhat Package Manager)是用于Redhat ...

  2. 十九、多文件上传(ajaxFileupload实现多文件上传功能)

    来源于https://www.jb51.net/article/128647.htm 打开google 搜索"ajaxFileupload' ‘多文件上传"可以搜到许许多多类似的, ...

  3. Average Sleep Time CodeForces - 808B (前缀和)

    It's been almost a week since Polycarp couldn't get rid of insomnia. And as you may already know, on ...

  4. Python编码与变量

    (一)Python执行的方式 Window: 在CMD里面,使用 Python + 相对路径/绝对路径 在解释器里面,直接输入,一行代码一行代码的解释 Linux: 明确地指出用Python解释器来执 ...

  5. 线程中的current thread not owner异常错误

    多线程常用的一些方法: wait(),wait(long),notify(),notifyAll()等 这些方法是当前类的实例方法, wait()      是使持有对象锁的线程释放锁;wait(lo ...

  6. 现代程序设计 homework-10

    经过大半学期的学习和练习, 我们把学到的东西综合起来. 通过<现代程序设计>这门课,自己的确学到了好多东西.其实并不是说讲课有多棒,一是因为讲课的次数其实并不多,二是讲课的内容其实感觉并没 ...

  7. beego 自定义控制器与路由

    框架浅析 这是之前使用bee创建的webapp目录层级结构: ├── conf 配置文件 │ └── app.conf ├── controllers 控制器 │ └── default.go ├── ...

  8. CentOS的el5, el6, el7代表什么

    https://www.cnblogs.com/EasonJim/p/9051851.html el: enterprise linux?

  9. Chrome 浏览器的简单设置 无痕模式 暗黑模式 自定义用户目录

    1. Chrome73 新增加了暗黑模式 可以通过修改快捷方式的方式来默认开启方法如下 1.1 关闭浏览器 2.2 鼠标焦点定位到任务栏 Chrome 图标处, 并且按住shift 按键 执行右键操作 ...

  10. JS学习笔记 等于和包装对象

    严格等于 a===b 首先判断两边数据的类型,若类型不同,返回false. 若类型相同(1.2和1.2,字符串相等指内容和长度都是一样的),返回true null===null undefined== ...