Delivery

题目还是自己看吧 - -!

  看似图论,实际上是一个考察思维以及数据结构的题。

  我们对于先前和向后的边分别进行统计。

  对询问离线。

  小边按照左端点从大到小排序。

  

  1.对于向后的边,询问按照出发点从大到小排序。比如询问有

  2 3

  3 4

  我们先对3 4进行计算。把向后的小边(3,5) ,(3,4) 用线段树维护,分别在线段树的位置4,5中插入用该边时可以优化的值。询问3 4时,我们发现出发点3以及后面的小边都加进了线段树中,直接询问线段树区间 [3,4]的最小值进行计算即可。注意一下可能加入了边之后比不加边更差的情况。

  然后再对2 3进行计算,这次把小边(2,4)添加到线段树中,查询区间[2,4]的最小值即可。

  2.对于向前的边,询问按照出发点从大到小排序。

  同样跟1差不多,不过这次询问(x,y)时询问的是区间[1,y]。

  

  具体可以看代码。

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
#define lson rt<<1
#define rson rt<<1|1 /* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ char IN;
bool NEG;
int OUT[15],top;
inline void Int(int &x){
NEG = 0;
while(!isdigit(IN=getchar()))
if(IN=='-')NEG = 1;
x = IN-'0';
while(isdigit(IN=getchar()))
x = x*10+IN-'0';
if(NEG)x = -x;
}
inline void LL(ll &x){
NEG = 0;
while(!isdigit(IN=getchar()))
if(IN=='-')NEG = 1;
x = IN-'0';
while(isdigit(IN=getchar()))
x = x*10+IN-'0';
if(NEG)x = -x;
}
inline void out(ll x){
top = 0;
while(x){
OUT[++top] = x%10;
x /= 10;
}
if(!top)putchar('0');
while(top)putchar(char('0'+OUT[top--]));
puts("");
} /******** program ********************/ const int MAXN = 200005;
const ll INF = 1e15; ll ans[MAXN],sum[MAXN];
int val[MAXN],n,m; struct node{
int x,y,val;
node(){}
node(int _x,int _y,int _val):x(_x),y(_y),val(_val){}
friend bool operator < (node a,node b){
return a.x>b.x;
}
}p[MAXN],a[MAXN],b[MAXN]; struct segTree{
int l,r;
ll mx;
inline int mid(){
return (l+r)>>1;
}
}tree[MAXN<<2]; void build(int l,int r,int rt){
tree[rt].l = l;
tree[rt].r = r;
tree[rt].mx = INF;
if(l==r)return;
int mid = tree[rt].mid();
build(l,mid,lson);
build(mid+1,r,rson);
} void modify(int pos,ll val,int rt){
if(tree[rt].l==tree[rt].r){
cmin(tree[rt].mx,val);
return;
}
int mid = tree[rt].mid();
if(pos<=mid)modify(pos,val,lson);
else modify(pos,val,rson);
tree[rt].mx = min(tree[lson].mx,tree[rson].mx);
} ll ask(int l,int r,int rt){
if(l<=tree[rt].l&&tree[rt].r<=r)
return tree[rt].mx;
int mid = tree[rt].mid();
if(r<=mid)return ask(l,r,lson);
else if(l>mid)return ask(l,r,rson);
else return min(ask(l,r,lson),ask(l,r,rson));
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif while(~RD2(n,m)){
REP(i,2,n){
Int(val[i]);
sum[i] = sum[i-1]+val[i];
}
rep1(i,m){
Int(p[i].x);
Int(p[i].y);
Int(p[i].val);
} int qq;
Int(qq);
int na = 0 , nb = 0 , x , y;
rep1(i,qq){
Int(x);
Int(y);
if(x>y)b[++nb] = node(x,y,i);
else a[++na] = node(x,y,i);
} sort(a+1,a+na+1);
sort(p+1,p+m+1);
int pos = 1; build(1,n,1);
rep1(i,na){
while(pos<=m&&p[pos].x>=a[i].x){
x = p[pos].x , y = p[pos].y;
if(x<=y)
modify(y,p[pos].val-(sum[y]-sum[x]),1 );
pos ++;
} ll tmp = ask(a[i].x,a[i].y,1);
if(tmp>0)tmp = 0;
ans[ a[i].val ] = sum[ a[i].y ]-sum[ a[i].x ]+tmp;
} sort(b+1,b+nb+1);
pos = 1; build(1,n,1);
rep1(i,nb){
while( pos<=m&&p[pos].x>=b[i].x ){
x = p[pos].x , y = p[pos].y;
if(x>=y)
modify(y,sum[x]-sum[y]+p[pos].val,1);
pos ++;
}
ans[ b[i].val ] = ask(1,b[i].y,1)-(sum[b[i].x]-sum[b[i].y]);
} rep1(i,qq)
out(ans[i]);
} return 0;
}

  

zoj 3742 Delivery 好题的更多相关文章

  1. ZOJ 3724 Delivery 树状数组好题

    虽然看起来是求最短路,但因为条件的限制,可以转化为区间求最小值. 对于一条small path [a, b],假设它的长度是len,它对区间[a, b]的影响就是:len-( sum[b]-sum[a ...

  2. 九度OJ 1006 ZOJ问题 (这题測试数据有问题)

    题目1006:ZOJ问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:15725 解决:2647 题目描写叙述: 对给定的字符串(仅仅包括'z','o','j'三种字符),推断他能否AC ...

  3. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  4. ZOJ 3469Food Delivery(区间DP)

    Food Delivery Time Limit: 2 Seconds      Memory Limit: 65536 KB When we are focusing on solving prob ...

  5. Capture the Flag ZOJ - 3879(模拟题)

    In computer security, Capture the Flag (CTF) is a computer security competition. CTF contests are us ...

  6. ZOJ 3594 年份水题 【注意:没有0年】

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...

  7. zoj 3809 枚举水题 (2014牡丹江网赛 A题)

    题目大意:给出一列取样的几个山的高度点,求山峰有几个? Sample Input 291 3 2 4 6 3 2 3 151 2 3 4 5Sample Output 30 # include < ...

  8. Beauty of Array ZOJ - 3872(思维题)

    Edward has an array A with N integers. He defines the beauty of an array as the summation of all dis ...

  9. ZOJ 2679 Old Bill ||ZOJ 2952 Find All M^N Please 两题水题

    2679:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1679 2952:http://acm.zju.edu.cn/onli ...

随机推荐

  1. 关于C#中文本模板(.tt)的简单应用

    这两天做项目突遇 .tt文件,之前没有接触过,so查询学习做笔记,帮助记忆和后来者. 在项目添加中点击选择文本模板 下面贴出代码,做了简单的注释 <#@ template debug=" ...

  2. (剑指Offer)面试题33:把数组排成最小的数

    题目: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 思路: 1.全 ...

  3. Oracle-11g-R2 RAC 环境下 GPnP Profile 文件

    GPnP Profile 文件的作用: GPnP Profile 文件是一个保存于 $GRID_HOME/gpnp/<hostname>/profiles/peer 目录下的小型 XML ...

  4. 微信分享朋友圈监听(PHP)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Firefox插件一键切换兼容IE

    转载:http://mozilla.com.cn/thread-42137-1-1.html 让火狐兼容IE的双核扩展,一键切换至IE内核,网银支付无忧愁.支持Adblock plus和FireGes ...

  6. 【转】获取手机的ipv4地址

    http://blog.csdn.net/yueqinglkong/article/details/17391051 直接贴代码: public class GetLocalIpAddress ext ...

  7. 解决ArcGIS Android Could not find class 'com.esri.android.map.MapView'问题

    环境win7 64bit sp1,eclipse 4.2.1 ,android API 16,ADT 23.0.2,arcgis android sdk 10.2.4 从arcgis-android- ...

  8. Java数据结构之树和二叉树(2)

    从这里始将要继续进行Java数据结构的相关讲解,Are you ready?Let's go~~ Java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来 ...

  9. 2016 icpc-camp 之旅(一)

    啦啦啦,终于前往icpccamp啦! 嗯,该死的飞机居然晚点了! 诶,晚点居然还会发赔偿金! 飞机上没什么好说的,和萌神一起看了5集龙与虎,然后就到了. 讲道理,海南航空感觉一般. 我的座位前面有个平 ...

  10. linux C(hello world)三个数最大和三个数最新