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 ...
随机推荐
- ASP.NET Core on K8S深入学习(1)K8S基础知识与集群搭建
在上一个小系列文章<ASP.NET Core on K8S学习初探>中,通过在Windows上通过Docker for Windows搭建了一个单节点的K8S环境,并初步尝试将ASP.NE ...
- Mac Android 配置环境变量
进入终端,输入以下命令: cd ~ touch .bash_profile //没有该文件的话新建一个 vi .bash_profile //vim 形式打开 输入内容jdk变量配置内容: expor ...
- DVWA-SQL注入
SQL注入解题思路 寻找注入点,可以通过web扫描工具实现 通过注入点,尝试得到连接数据库的用户名,数据库名称,权限等信息. 猜解关键数据库表极其重要字段与内容. 通过获得的用户信息寻找后台进行登录. ...
- Spring的依赖注入和管理Bean
采用Spring管理Bean和依赖注入 1.实例化spring容器 和 从容器获取Bean对象 实例化Spring容器常用的两种方式: 方法一: 在类路径下寻找配置文件来实例化容器 [推荐使用] Ap ...
- (14)ASP.NET Core 中的日志记录
1.前言 ASP.NET Core支持适用于各种内置和第三方日志记录提供应用程序的日志记录API.本文介绍了如何将日志记录API与内置提供应用程序一起使用. 2.添加日志提供程序 日志记录提供应用程序 ...
- Unity学习--捕鱼达人笔记
1.2D模式和3D模式的区别,2D模式默认的摄像机的模式是Orthographic(正交摄像机),3D模式默认的摄像机的模式是Perspective(透视摄像机).3D会额外给你一个平衡光.3D模式修 ...
- SDS模块
早上花了一点时间读了下sds的相关源码,其实sds就是构造了两个字段用来记录len和free的状态,然后还有一个char[]用来记录字符串的值. 然后sds模块的函数都是在模拟str的操作. 比较,追 ...
- Letters Shop
B. Letters Shop time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- JVM解剖乐园
1.JVM锁粗化和循环原文标题:JVM Anatomy Quark #1: Lock Coarsening and Loops 众所周知Hotsport编译器会进行JVM锁粗化和优化,它将相邻的锁区块 ...
- MQ 服务器错误代码2035
MQ 服务器错误代码20352013-06-12 19:29:39 搭建一个MQ7.1服务器,用了一个小的demo测试程序,结果报错, 测试代码: import com.ibm.mq.MQC; imp ...