zoj 3724 树状数组经典
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 100005;
const int maxm = 200010;
const int maxt = 200010;
const LL inf = (1LL<<60);
struct node{
int u,v,kind;
LL ind;
bool operator < (node a)const{
if( u == a.u && v == a.v)return kind < a.kind;
if( u == a.u)return v < a.v;
return u > a.u;
}
}p[maxn+maxm+maxt];
LL s[maxn],res[maxt];
LL b[maxn];
int n;
int lowbit(int x){
return x&(-x);
}
void update(int x,LL val){
while( x <= n){
b[x] = min(b[x],val);
x += lowbit(x);
}
}
LL query(int x){
LL res = inf;
while( x ){
res = min(res,b[x]);
x -= lowbit(x);
}
return res;
} int main(){
int i,m;
while(~scanf("%d %d",&n,&m)){
for( i = 2; i <= n; i++){
scanf("%lld",&s[i]);
s[i] += s[i-1];
}
for(i = 0; i < m; i++){
scanf("%d %d %lld",&p[i].u,&p[i].v,&p[i].ind);
p[i].kind = 0;
}
int t;scanf("%d",&t);
for( ; i < m+t; i ++){
scanf("%d %d",&p[i].u,&p[i].v);
p[i].kind = 1;p[i].ind =i-m;
}
sort(p,p+m+t);
// for(int i = 0; i < m+t; i++)cout << p[i].u << " " << p[i].v << " " << p[i].ind << " " << p[i].kind << endl;
memset(b,0,sizeof(b));
memset(res,0,sizeof(res));//当u == v时距离为0
for(int i = 0; i < m+t; i++){
if( p[i].kind == 0 && p[i].u < p[i].v)
update(p[i].v, p[i].ind - (s[p[i].v] - s[p[i].u]) );
else if( p[i].kind == 1 && p[i].u < p[i].v)
res[p[i].ind] = (s[p[i].v] - s[p[i].u]) + query(p[i].v);
}
for(int i = 0; i <= n+1;i++)b[i] = inf;
for(int i = 0; i < m+t; i++){
if( p[i].kind == 0 && p[i].u > p[i].v)
update(p[i].v,p[i].ind + (s[p[i].u] - s[p[i].v]));
else if( p[i].kind == 1 && p[i].u > p[i].v){
// int ans = query(p[i].v);cout << ans << endl;
res[p[i].ind] = query(p[i].v) - (s[p[i].u] - s[p[i].v]);
}
}
for(int i = 0; i < t; i++)
printf("%lld\n",res[i]);
}
return 0;
}
/* 5 3
1 2 3 4
2 4 2
1 3 2
5 1 3
5
1 4
4 2
3 1
1 3
1 5 */
zoj 3724 树状数组经典的更多相关文章
- Ping pong(树状数组经典)
Ping pong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- POJ 3067 Japan 【树状数组经典】
题目链接:POJ 3067 Japan Japan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32076 Accep ...
- ZOJ 3724 Delivery 树状数组好题
虽然看起来是求最短路,但因为条件的限制,可以转化为区间求最小值. 对于一条small path [a, b],假设它的长度是len,它对区间[a, b]的影响就是:len-( sum[b]-sum[a ...
- POJ 3067 Japan(经典树状数组)
基础一维树状数组 题意:左边一排 1-n 的城市,右边一排 1-m 的城市,都从上到下依次对应.接着给你一些城市对,表示城市这两个城市相连,最后问你一共有多少个交叉,其中处于城市处的交叉不算并且每个 ...
- hdu 1541/poj 2352:Stars(树状数组,经典题)
Stars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- POJ 2155 Matrix 【二维树状数组】(二维单点查询经典题)
<题目链接> 题目大意: 给出一个初始值全为0的矩阵,对其进行两个操作. 1.给出一个子矩阵的左上角和右上角坐标,这两个坐标所代表的矩阵内0变成1,1变成0. 2.查询某个坐标的点的值. ...
- ZOJ - 2112 主席树套树状数组
题意:动态第k大,可单点更新,操作+原数组范围6e4 年轻人的第一道纯手工树套树 静态第k大可以很轻易的用权值主席树作差而得 而动态第k大由于修改第i个数会影响[i...n]棵树,因此我们不能在原主席 ...
- ZOJ 3157 Weapon --计算几何+树状数组
题意:给一些直线,问这些直线在直线x=L,x=R之间有多少个交点. 讲解见此文:http://blog.sina.com.cn/s/blog_778e7c6e0100q64a.html 首先将直线分别 ...
- Dynamic Rankings ZOJ - 2112(主席树+树状数组)
The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with t ...
随机推荐
- TypeScript入门实例
前言 TypeScript是JavaScript的超集,微软公司开发,利用es6语法,实现对js的面向对象编程思想,写代码的时候会像强类型语言一样,指定参数类型.返回值类型,类型不对会报错,但编译后还 ...
- 用python twilio模块实现发手机短信的功能
前排提示:这个模块不是用于对陌生人进行短信轰炸和电话骚扰的,这个模块也没有这个功能,如果是抱着这个心态来的,可以关闭网页了 语言:python 步骤一:安装twilio模块 pip install t ...
- Spring Boot中自定义注解+AOP实现主备库切换
摘要: 本篇文章的场景是做调度中心和监控中心时的需求,后端使用TDDL实现分表分库,需求:实现关键业务的查询监控,当用Mybatis查询数据时需要从主库切换到备库或者直接连到备库上查询,从而减小主库的 ...
- mysql5.7.18-winx64安装
win10下装mysql-5.7.18-winx64 步骤1 官网下载地址:https://dev.mysql.com/downloads/mysql/ 选择手动安装版: 解压到D盘mysql文件夹下 ...
- Okhttp3 网络请求框架与 Gson
Maven环境 : <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>o ...
- 已知词频生成词云图(数据库到生成词云)--generate_from_frequencies(WordCloud)
词云图是根据词出现的频率生成词云,词的字体大小表现了其频率大小. 写在前面: 用wc.generate(text)直接生成词频的方法使用很多,所以不再赘述. 但是对于根据generate_from_f ...
- 【Kubernetes 系列四】Kubernetes 实战:管理 Hello World 集群
目录 1. 创建集群 1.1. 安装 kubectl 1.1.1. 安装 kubectl 到 Linux 1.1.2. 安装 kubectl 到 macOS 1.1.3. 安装 kubectl 到 W ...
- c#小灶——常量、变量和赋值
常量 常量很好理解,和变量相对,就是不会变的量.比如,1就是常量,3.6也是常量,‘a’也是常量,“aaaaa”也是常量,只是不同类型.这些都是表面上一眼就看出来的常量,还有一种表面上看不出来的常量, ...
- https理论及实践
什么是https协议? http协议以明文的方式在网络中传输,安全性难以保证,https在http协议的基础上加入SSL/TLS层.TLS是SSL协议的最新版本,SSL使用SSL数字证书在通信两端建立 ...
- warpAffine仿射变换
仿射变换,其实就是不同的坐标系的相互转换,用于图像的平移和旋转. 首先看一下官方的api描述. https://docs.opencv.org/2.4/modules/imgproc/doc/geom ...