[Poi2011] Meteors(从不知所措到整体二分)
Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby galaxy. The planet is unsuitable for colonisation due to strange meteor showers, which on the other hand make it an exceptionally interesting object of study.
The member states of BIU have already placed space stations close to the planet's orbit. The stations' goal is to take samples of the rocks flying by. The BIU Commission has partitioned the orbit into sectors, numbered from
to
, where the sectors
and
are adjacent. In each sector there is a single space station, belonging to one of the
member states.
Each state has declared a number of meteor samples it intends to gather before the mission ends. Your task is to determine, for each state, when it can stop taking samples, based on the meter shower predictions for the years to come.
Input
The first line of the standard input gives two integers, and
(
), separated by a single space, that denote, respectively, the number of BIU member states and the number of sectors the orbit has been partitioned into.
In the second line there are integers
(
), separated by single spaces, that denote the states owning stations in successive sectors.
In the third line there are integers
(
), separated by single spaces, that denote the numbers of meteor samples that the successive states intend to gather.
In the fourth line there is a single integer (
) that denotes the number of meteor showers predictions. The following
lines specify the (predicted) meteor showers chronologically. The
-th of these lines holds three integers
,
,
(separated by single spaces), which denote that a meteor shower is expected in sectors
(if
) or sectors
(if
), which should provide each station in those sectors with
meteor samples (
).
Output
Your program should print lines on the standard output. The
-th of them should contain a single integer
, denoting the number of shower after which the stations belonging to the
-th state are expected to gather at least
samples, or the word NIE (Polish for no) if that state is not expected to gather enough samples in the foreseeable future.
Example
For the input data:
3 5
1 3 2 1 3
10 5 7
3
4 2 4
1 3 1
3 5 2
the correct result is:
3
NIE
1
题意:
n个国家,m个收集站,每个收集站分别归属于某个国家,一共又k颗流星,问每个国家再第几颗流星经过可以搜集够自己需要的流星need[i]。
先谈谈自己的感受。
普通二分过程([L,R]+一个询问X):二分[L,mid]或者二分[mid+1,R]。最后L==R时得到了一个询问的答案X。
整体二分过程([L,R]+一群询问X):部分询问(集合A)需要二分[L,mid](假设叫左二分),一些询问(集合B)需要二分[mid+1,R](右二分)。这个时候把属于左二分是一起左二分,属于有二分的一起右二分。 当L==R时得到了当前集合的答案,其中集合X=集合A+集合B。
对于整体效率的分析:
假设二分solve(X,L,R)表示集合X的答案在[L,R]间(L,R是由上一层二分判定缩小的范围,初始L=1,R=m,X为1到n的集合)。
按照上面整体二分的过程,solve(X,L,R)继续分解为solve(A,L,mid)和solve(B,mid+1,R)两部分;前面一部分是mid的时候,A集合满足条件need[],答案范围缩小为[L,mid];同理,B集合不满足条件need[],答案范围缩小到[mid+1,R]。
所以,若最开始的集合X=(1,2,3,...n)最后分解为了g个集合(1<=g<=m,m为操作数量,此题的流星数量),
每一个二分复杂度为O(qlgn),q为一个常数,大小取决于题目,总的效率为O(g*qlgn)<O(m*qlgn)。如果最后集合g,q不会太大,整体二分就行得通。而g取决于数据,q取决于算法。
综上,大概知道什么是整体二分了。具体针对这道题:
(抄袭的代码,From victorywonder,现在主要是学习思想,首先的化,多做几个题再整理)。
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int,int> pr;
const int infi=;
const int N=;
const int M=;
struct node{int x,y,z;}p[N];
int n,m,k,c[N],id[N],ans[N];
int g[N],to[N],nxt[N],tot;
int tol[N],tor[N];
ll h[N],tmp[N],cur[N];
void add(int pos,int x) {while (pos<=m) h[pos]+=x,pos+=(pos&-pos);}
void adddt(int x,int y,int z) {add(x,z); add(y+,-z);}
ll sum(int pos) {ll t=; while (pos>) t+=h[pos],pos-=(pos&-pos);return t;}
void addop(int x,int y,int z,int i) {p[i].x=x; p[i].y=y; p[i].z=z;}//第i颗流星
void addpt(int x,int y) {to[++tot]=y; nxt[tot]=g[x]; g[x]=tot;}//第x个国家,拥有的收集站。
void solve(int head,int tail,int l,int r) { //id [head到tail]的队列答案在[l,r]中
if (head>tail) return;
int i,k,x,mid=(l+r)>>,lnum=,rnum=;
if(l==r) {
for (i=head;i<=tail;i++) ans[id[i]]=l;
return;
}
for(i=l;i<=mid;i++) {//试探左半截。
if (p[i].x<=p[i].y) adddt(p[i].x,p[i].y,p[i].z);//加入树状数组
else adddt(p[i].x,m,p[i].z),adddt(,p[i].y,p[i].z);//由于是个环。
}
for(i=head;i<=tail;i++) {
tmp[id[i]]=;
for (k=g[id[i]];k;k=nxt[k]) {//第id[i]个国家的每一个站。
tmp[id[i]]+=sum(to[k]);
if (tmp[id[i]]+cur[id[i]]>c[id[i]]) break;
}
if (cur[id[i]]+tmp[id[i]]>=c[id[i]]) tol[++lnum]=id[i];
else tor[++rnum]=id[i],cur[id[i]]+=tmp[id[i]];
}
for (i=l;i<=mid;i++) {//减回去。效果就是memset(h,0,sizeof(h)),但是memset太浪费了;
if (p[i].x<=p[i].y) adddt(p[i].x,p[i].y,-p[i].z);
else adddt(p[i].x,m,-p[i].z),adddt(,p[i].y,-p[i].z);
}
for (i=;i<lnum;i++) id[head+i]=tol[i+];
for (i=;i<rnum;i++) id[head+lnum+i]=tor[i+];
solve(head,head+lnum-,l,mid);
solve(head+lnum,tail,mid+,r);
}
int main() {
int i,x,y,z;
scanf("%d%d",&n,&m);
for (i=;i<=m;i++) {
scanf("%d",&x);
addpt(x,i);
}
for (i=;i<=n;i++) {
scanf("%d",&c[i]);
id[i]=i;
}
scanf("%d",&k);
for (i=;i<=k;i++) {
scanf("%d%d%d",&x,&y,&z);
addop(x,y,z,i);
}
addop(,m,infi,++k);
solve(,n,,k);
for (i=;i<=n;i++) if (ans[i]!=k) printf("%d\n",ans[i]);
else puts("NIE");
return ;
}
[Poi2011] Meteors(从不知所措到整体二分)的更多相关文章
- 2527: [Poi2011]Meteors[整体二分]
2527: [Poi2011]Meteors Time Limit: 60 Sec Memory Limit: 128 MB Submit: 1528 Solved: 556 [Submit][S ...
- 【BZOJ2527】[Poi2011]Meteors 整体二分
[BZOJ2527][Poi2011]Meteors Description Byteotian Interstellar Union (BIU) has recently discovered a ...
- 【bzoj2527】[Poi2011]Meteors(树状数组(单点查询,区间修改)+整体二分)
[bzoj2527][Poi2011]Meteors Description Byteotian Interstellar Union (BIU) has recently discovered a ...
- 【BZOJ2527】【POI2011】Meteors [整体二分]
Meteors Time Limit: 60 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 这个星球经常会下陨石雨.BI ...
- bzoj 2527: [Poi2011]Meteors 整体二分
给每个国家建一个链表,这样分治过程中的复杂度就和序列长度线形相关了,无脑套整体二分就可以. (最坑的地方是如果所有位置都是一个国家,那么它的样本个数会爆longlong!!被这个坑了一次,大于p[i] ...
- BZOJ2527[Poi2011]Meteors——整体二分+树状数组
题目描述 Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby galaxy. The ...
- BZOJ2527 [Poi2011]Meteors 整体二分 树状数组
原文链接http://www.cnblogs.com/zhouzhendong/p/8686460.html 题目传送门 - BZOJ2527 题意 有$n$个国家. 太空里有$m$个太空站排成一个圆 ...
- Luogu3527 POI2011 Meteors 整体二分、树状数组、差分
传送门 比较板子的整体二分题目,时限有点紧注意常数 整体二分的过程中将时间在\([l,mid]\)之间的流星使用树状数组+差分进行维护,然后对所有国家查看一遍并分好类,递归下去,记得消除答案在\([m ...
- BZOJ 2527 [Poi2011]Meteors(整体二分)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2527 [题目大意] 有N个成员国.现在它发现了一颗新的星球, 这颗星球的轨道被分为M份 ...
随机推荐
- VueJS事件处理器v-on
事件监听可以使用 v-on 指令. v-on:click表达式 HTML: <!DOCTYPE html> <html> <head> <meta chars ...
- easyui datagrid自己定义操作列
通过formatter方法给Jquery easyui 的datagrid 每行添加操作链接 我们都知道Jquery的EasyUI的datagrid能够加入而且自己定义Toolbar. 这样我们选择一 ...
- SW线路中串联1K电阻的作用
主要作用的去ESD,去干扰 和ADC上串的一样一样的作用 物美价廉的ESD方案 我还是觉得起到控制开关打开的快慢,也就是控制开关脉冲的上升沿,加了这个1K电阻,可以减缓上升延的斜率.如果上升沿过快,会 ...
- getOutString 输出弹出字符串
输入字符串长度,字符串,计数m.从前往后计数,当数到m个元素时,第m个元素出列,同时将该元素赋值给m,然后从下一个数计数循环,直到所有数字都出列,给定的数全部为大于0的数字.输出出队队列. 例如: ...
- 集群通信组件Tribes之怎样维护集群成员信息
一个集群包括若干成员,要对这些成员进行管理就必需要有一张包括全部成员的列表.当要对某个节点做操作时通过这个列表能够准确找到该节点的地址进而对该节点发送操作消息.怎样维护这张包括全部成员的列表是本节要讨 ...
- SpringMVC hibernate增加多数据源 (SSHE/SYPRO增加多数据源为例)
SpringMVC hibernate增加多数据源 (以类SSHE/SYPRO增加多数据源为例作说明) 注:适用与SpringMVC + Hibernate的项目.其它框架的仅仅能说作參考用 配置Sp ...
- 微服务之旅:从Netflix OSS到 Istio Service Mesh
在这篇文章中,我们从Netflix开始,通过Envoy和Istio的崛起,快速浏览微服务的历史. 微服务是具有边界上下文的松散耦合服务,使您能够独立开发,部署和扩展服务.它还可以定义为构建独立开发和部 ...
- request 解决中文乱码问题
package request; import java.io.IOException;import javax.servlet.ServletException;import javax.servl ...
- 李振杰:火狐Mozilla被黑事件的启发
火狐浏览器开发商Mozilla近日宣布,因为数据库存在漏洞.Mozilla开发者的数万个电子邮件地址和加密password或遭到黑客窃取. 好多有为青年们往往刚刚获得了一个小成功,便開始沾沾自喜,自命 ...
- SkipList跳表(一)基本原理
一直听说跳表这个数据结构,说要学一下的,懒癌犯了,是该治治了 为什么选择跳表 目前经常使用的平衡数据结构有:B树.红黑树,AVL树,Splay Tree(这个树好像还没有听说过),Treep(也没有听 ...