BZOJ4627 权值线段树
4627: [BeiJing2016]回转寿司
Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 1204 Solved: 475
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
用权值线段树进行维护;
预处理出前缀和后,就是查询
sum[i]-R~sum[i]-L这个区间sum[j]的数量;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 10000000005ll
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;inline int rd() {int x = 0;char c = getchar();bool f = false;while (!isdigit(c)) {if (c == '-') f = true;c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f ? -x : x;
}ll gcd(ll a, ll b) {return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {if (!b) {x = 1; y = 0; return a;}ans = exgcd(b, a%b, x, y);ll t = x; x = y; y = t - a / b * y;return ans;
}
*/struct node {ll l, r, cnt;
}tr[maxn<<4];ll sum[maxn];
ll root = 1, cntrt = 1;
ll L, R;void Insert(ll &o, ll l, ll r, ll v) {if (!o)o = ++cntrt;tr[o].cnt++;if (l == r)return;ll mid = (l + r) >> 1;if (v <= mid)Insert(tr[o].l, l, mid, v);else Insert(tr[o].r, mid + 1, r, v);
}int query(ll l, ll r, ll LL, ll RR, ll rt) {if (LL <= l && r <= RR)return tr[rt].cnt;ll mid = (l + r) >> 1;int ans = 0;if (LL <= mid)ans += query(l, mid, LL, RR, tr[rt].l);if (mid < RR)ans += query(mid + 1, r, LL, RR, tr[rt].r);return ans;
}int main()
{// ios::sync_with_stdio(0);ll n; rdllt(n); rdllt(L); rdllt(R);for (int i = 1; i <= n; i++) {ll x; rdllt(x); sum[i] = sum[i - 1] + x;}Insert(root, -inf, inf, 0);ll ans = 0;for (int i = 1; i <= n; i++) {ans += 1ll * query(-inf, inf, sum[i] - R, sum[i] - L, root);Insert(root, -inf, inf, sum[i]);}printf("%lld\n", 1ll * ans);return 0;
}
转载于:https://www.cnblogs.com/zxyqzy/p/10381864.html
BZOJ4627 权值线段树相关推荐
- BZOJ4627 前缀和 + 权值线段树
https://www.lydsy.com/JudgeOnline/problem.php?id=4627 题意:求序列中和在L到R之间的字串种数. 要求的是和的范围,我们可以考虑先求一个前缀和pre ...
- 【bzoj2770】YY的Treap 权值线段树
题目描述 志向远大的YY小朋友在学完快速排序之后决定学习平衡树,左思右想再加上SY的教唆,YY决定学习Treap.友爱教教父SY如砍瓜切菜般教会了YY小朋友Treap(一种平衡树,通过对每个节点随机分 ...
- 树套树 ----- P1975 [国家集训队]排队(树状数组套权值线段树求动态逆序对)
解题思路: 首先我们知道交换两个数a[l]和a[r]a[l]和a[r]a[l]和a[r]影响到的区间是[l+1,r−1][l+1,r-1][l+1,r−1] 对于a[l]a[l]a[l],我们要减去[ ...
- 树套树 ---- 树状数组套权值线段树模板题 P2617 Dynamic Rankings 动态第K大
题目链接 题目大意: 给你一个数组aaa,aaa有两个操作 询问aaa中[l,r][l,r][l,r]区间里面第kkk小的数是哪个? 修改axa_xax为yyy 解题思路: 首先我们知道权值线段树是 ...
- codevs1688 求逆序对(权值线段树)
1688 求逆序对 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 给定一个序列a1,a2,-,an,如 ...
- 【HDU6701】Make Rounddog Happy【权值线段树+双向单调队列】
题意:给你一个序列,求满足要求的子序列个数,其中要求为: 1.子序列的max-子序列长度len<=k 2.子序列中不出现重复的数字 题解:首先看到子序列max,很容易想到枚举最大值然后分治,这个 ...
- HDU-5249 KPI(STL or 权值线段树)
题目链接 Problem Description 你工作以后, KPI 就是你的全部了. 我开发了一个服务,取得了很大的知名度.数十亿的请求被推到一个大管道后同时服务从管头拉取请求.让我们来定义每个请 ...
- Governing sand(权值线段树/主席树)
链接:https://ac.nowcoder.com/acm/contest/887/C 来源:牛客网 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 65536K,其他语言13107 ...
- Minimum Inversion Number HDU - 1394(权值线段树/树状数组)
The inversion number of a given number sequence a1, a2, -, an is the number of pairs (ai, aj) that s ...
- 权值线段树小结(hdu多校,普通平衡树,郁闷的出纳员)
之前刷了一点主席树的题目,但是没有系统的做过权值线段树的题目.主席树是多根权值线段树的综合.权值线段树可以解决在总区间里求第k大的问题.在普通的线段树里,我们每一个节点维护的是权值大小.但是在权值线段 ...
最新文章
- 数学:拓展中国剩余定理
- 引路蜂地图API:Gis.Navigation包定义
- 一个账号,防止多设备登陆
- spring+ibatis+注解开发
- 【回归预测】基于matlab鲸鱼算法优化BP神经网络回归预测(多输入单输出)【含Matlab源码 1554期】
- 偶然的相遇【我与51CTO的故事】
- python计算正方形、立方体、圆、球的面积和体积
- Python AutoCAD 选择集
- html中图片放大镜效果图,HTML5使用不同精度的图片来实现图像放大镜效果
- html5火焰字体效果前缀,ps制作火焰字体特效
- 第一部分——交错(拉丝)(Interlace)的产生
- Linux下通过CCID协议与USB设备进行交互经验总结
- 记录某大门户网站自动跳转不良网站,团队通宵排查病毒木马全过程
- PHP 中如何正确统计中文字数
- mint系统用wine打开exe文件
- BSP-充电名词解释
- CST学习笔记1--------CST基础建模
- 聚星完整源码 UI超级好看+对接NG接口+双玩系统
- Verilog轮询仲裁器设计——Round Robin Arbiter
- 图网络可解释性(2)
热门文章
- 双11技术大练兵,阿里云开启新十年
- 【Vue】watch监听器案例,watch监听器实战、用法,watch监听购物车小案例。watch、filters、computed
- Grid++Report横向换页
- [模型浅析] Swin Transformer源码阅读
- 灵动微 MM32电机 以 MM32SPIN06PF 为主控的空调外机方案介绍
- Django使用mysql操作实战系列之七
- java定位问题、发布紧急版本验证方案
- 长庆石油学校计算机97,长庆石油技师学校
- 二维码插件(qrcode)的使用
- 建议看一下,零基础学Python都想问的6个问题