题目链接: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. TCP/IP 协议 OSI七层协议

    ------------------你来自何处并不重要,重要的是你要去往何方,人生最重要的不是所站的位置,而是所去的方向.人只要不失去方向,就永远不会失去自己! day 27 # # -------- ...

  2. hybrid App cordova打包webapp PhoneGap

    Hybrid APP基础篇(一)->什么是Hybrid App APP三种开发模式--之--HybridApp解决方案 Hybrid App开发 四大主流平台分析 Hybrid App 开发模式 ...

  3. oc之考试答题类效果

    https://www.jianshu.com/p/ec29feb0b5a6 2017.07.27 11:48* 字数 424 阅读 615评论 9喜欢 11 demo地址:https://githu ...

  4. PAT L2-024 部落

    https://pintia.cn/problem-sets/994805046380707840/problems/994805056736444416 在一个社区里,每个人都有自己的小圈子,还可能 ...

  5. JDK8 的FullGC 之 metaspace

    JDK8 的FullGC 之 metaspace - 简书https://www.jianshu.com/p/1a0b4bf8d498

  6. 上传图片(photoClip)

    首先我们需要引入4个js包(这4个包总共106.6KB) <script src="__STATIC__/hammer.min.js" ></script> ...

  7. react-redux异步数据操作

    import React, { Component } from 'react'; import './App.css'; import {connect} from 'react-redux'; i ...

  8. taro实战1

    npm install -g @tarojs/cli //或 yarn global add @tarojs/cli

  9. Day 4-11 re正则表达式

    正则表达式就是字符串的匹配规则,在多数编程语言里都有相应的支持,python里对应的模块是re '.' 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行 '^' ...

  10. java学习之—栈匹配字符串符号

    /** * 栈 * Create by Administrator * 2018/6/11 0011 * 上午 10:20 **/ public class StackR { private int ...