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 ...
随机推荐
- nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### The error may ...
- git基础学习
1.git是什么 内容寻址文件系统,分布式版本控制系统 2.git作用 开发过程中的版本控制 3.git基础命令 克隆git仓库---clone:git clone 仓库url 选分支---check ...
- vue 初始化table数据,数据闪现的问题
使用的iview,很简单的一个table,可以扩展显示,我这里则是更改了一下,显示的也是表格,内容为明细数据. 原以为很简单的可以直接调用方法,进行数据的渲染,但是没想到,数据只是一闪而过. 百思不得 ...
- 【Android Studio】Frameworks detected: Android framework is detected in the project Configure
刚开始在 Mac 上用 Android Studio, 打开第一个项目就遇到了问题,描述如下: 上午9:: Frameworks detected: Android framework is dete ...
- Core CLR Host 源码简单分析
在定制 CLR Host的时候,可以通过调用如下代码,来获取当前需要被宿主的程序调用入口: hr = Host->CreateDelegate( domainId, L"Main,Ve ...
- 抓取崩溃的log日志
1.下载adb工具包 也就是解锁软件,如果要解锁的话,需确认有fastboot 安装jdk.sdk 2.注意事项 请确保电脑上只连接了一台手机设备(最好只连接一条USB线),同时确保手机已开启USB调 ...
- poj 1068 模拟
题目链接 大概题意就是告诉你有个n个小括号,每一个")"左边有多少个"("都告诉你了,然后让你求出每一对括号之间有多少对括号(包含自己本身). 思路: 我先计算 ...
- 为什么你要用 Spring?
前言 现在Spring几乎成为了Java在企业级复杂应用开发的代名词,得益于Spring简单的设计哲学和其完善的生态圈,确实为廉颇老矣,尚能饭否的 Java 带来了“春天”,有很多同学刚接触Jav ...
- Jmeter 接口测试参数处理
问题: 一.签名参数sign算法由文字描述,算法需自己编写 二. 参数param_json为变化的json串(json串内订单号唯一) 解决: 一. 签名sign: 1. 手动拼接后在https:// ...
- 图片格式:gif / png / pg / webp 介绍
本文引自:https://www.cnblogs.com/changyangzhe/articles/5718285.html GIF介绍 GIF 意为Graphics Interchange for ...