题目链接: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. Spring集成Quarz开发环境搭建

    第一步,搭建Spring相关的环境,参照:http://www.cnblogs.com/dyh004/p/4645572.html 第二步,下载Quartz相关的压缩文件,解压,下载地址:http:/ ...

  2. Spectral Bounds for Sparse PCA: Exact and Greedy Algorithms[贪婪算法选特征]

    目录 概括 Sparse PCA Formulation 非常普遍的问题 Optimality Conditions Eigenvalue Bounds 算法 代码 概括 这篇论文,不像以往的那些论文 ...

  3. AtCoder Beginner Contest 116 D - Various Sushi (贪心+栈)

    D - Various Sushi Time Limit: 2 sec / Memory Limit: 1024 MB Score : 400400 points Problem Statement ...

  4. 哈尔滨工程大学ACM预热赛

    https://ac.nowcoder.com/acm/contest/554#question A #include <bits/stdc++.h> using namespace st ...

  5. Composer之搭建自己的包工具

    作为一个标准的PHPer,必须学会优雅的使用composer,最近,萌生了一个想法,我们每搭建一个项目,里面都会有许多的公用的方法和类库,每次使用的时候就是将其拷贝过来,或者重新写一遍,过于繁琐,效率 ...

  6. 文件操作mode学习总结-----Python学习总结【第四篇】:Python之文件操作(文件、正则、json、pickle)

    非常全的博客,防丢链接参考https://www.cnblogs.com/madsnotes/articles/5521551.html 1.文件操作 1.1 操作流程 1)文件打开 2)文件操作 3 ...

  7. [转帖]SAP一句话入门:Finacial & Controlling Accounting

    SAP一句话入门:Finacial & Controlling Accounting http://blog.vsharing.com/MilesForce/A621147.html 财务,财 ...

  8. CLOUD流程设置

    流程-反写规则 允许超额

  9. 剑指offer(14)

    题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 思路: 这里有个细节,我们发现,6节点的子节点在操作之后并没有发生变化,所以等会我们在交换的时候,交换的不是节点的数值,而是整个节点. 另外我们进 ...

  10. MySQL的备份和回复

    一.备份的原因 二.备份的类型 三.备份的方式 四.备份策略 五.备份工具