2527: [Poi2011]Meteors

Time Limit: 60 Sec  Memory Limit: 128 MB
Submit: 1528  Solved: 556
[Submit][Status][Discuss]

Description

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 msectors,
numbered from 1to m, where the sectors 1and mare adjacent. In each
sector there is a single space station, belonging to one of the nmember
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.
 
Byteotian Interstellar
Union有N个成员国。现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站。
这个星球经常会下陨石雨。BIU已经预测了接下来K场陨石雨的情况。
BIU的第i个成员国希望能够收集Pi单位的陨石样本。你的任务是判断对于每个国家,它需要在第几次陨石雨之后,才能收集足够的陨石。
输入:
第一行是两个数N,M。
第二行有M个数,第i个数Oi表示第i段轨道上有第Oi个国家的太空站。
第三行有N个数,第i个数Pi表示第i个国家希望收集的陨石数量。
第四行有一个数K,表示BIU预测了接下来的K场陨石雨。
接下来K行,每行有三个数Li,Ri,Ai,表示第K场陨石雨的发生地点在从Li顺时针到Ri的区间中(如果Li<=Ri,就是Li,Li+1,...,Ri,否则就是Ri,Ri+1,...,m-1,m,1,...,Li),向区间中的每个太空站提供Ai单位的陨石样本。
输出:
N行。第i行的数Wi表示第i个国家在第Wi波陨石雨之后能够收集到足够的陨石样本。如果到第K波结束后仍然收集不到,输出NIE。
数据范围:
 
数据范围: 1<=n,m,k<=3*10^5 1<=Pi<=10^9 1<=Ai<10^9

Input

The first line of the standard input gives two integers, n and m(1<=n,m<=3*10^5) 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 mintegers Oi(1<=Oi<=n) separated by single spaces, that denote the states owning stations in successive sectors.
In the third line there are nintegers Pi(1<=Pi<=10^9) 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 k(1<=k<=3*10^5) that denotes the number of meteor showers predictions. The following klines specify the (predicted) meteor showers chronologically. The i-th of these lines holds three integers Li, Ri, Ai(separated by single spaces), which denote that a meteor shower is expected in sectors Li,Li+1,…Ri (if Li<=Ri) or sectors Li,Li+1,…,m,1,…Ri (if Li>Ri), which should provide each station in those sectors with Aimeteor samples (1<=Ai<10^9).
In tests worth at least 20% of the points it additionally holds that .

Output

 
Your program should print nlines on the standard output. The i-th of them should contain a single integer Wi, denoting the number of shower after which the stations belonging to the i-th state are expected to gather at least Pi samples, or the word NIE (Polish for no) if that state is not expected to gather enough samples in the foreseeable future.

Sample Input

3 5
1 3 2 1 3
10 5 7
3
4 2 4
1 3 1
3 5 2
 

Sample Output

3
NIE
1
 

HINT

 

Source

[分析]:

对于单个查询(假设为第i个国家),我们可以二分k,每次对于一个区间[l,r],手动模拟一下在第mid场流星雨过后,第i个国家一共收集到了多少单位的陨石,如果比pi大,那么答案在[l,mid]范围内,否则答案在[mid+1,r]范围内。

对于多组查询,我们也可以这么做——整体二分。首先,我们需要用一个列表id[]记录所有查询的编号,刚开始的时候,id[]自然是递增的.同时,我们用一个数组lans[i]记录下,第i个国家在l-1场流星雨过后,收集到的陨石的数目。

主过程为void
divide(int opl,int opr,int l,int r),表示对于id[opl]到id[opr]的所有询问,在[l,r]范围内查询答案,通过上一层的操作,我们保证id[opl]到id[opr]的所有询问的答案都在[l,r]范围内。

首先,我们先模拟[l,mid]这么多次操作(在询问重新划分之后,必须要再次模拟,将数组清空),用树状数组(推荐“常数小”)或者是线段树计算出在[l,mid]场流星雨之后,每个空间站收集到的陨石的数目。

然后我们查询,每个国家收集到的陨石的数目,要注意的是,我们需要用链表储存每个国家对应的空间站,并且一一枚举,用nans[id[i]]表示国家id[i]收集到的陨石的数目。

那么从[1,mid]这么多次操作之后,国家id[i]收集到的陨石数目就是nans[id[i]]+lans[id[i]],如果nans[id[i]]+lans[id[i]]>P[id[i]],那么表明对于国家id[i],其答案在[l,mid]这个范围内,否则其答案在[mid+1,r]范围内,并将nans[id[i]]累加到lans[id[i]]上。

还有一个坑点是,nans[id[i]]可能很大,会爆掉long
long,所以如果枚举一个国家的所有空间站的时候,发现nans[id[i]]已经大于P[id[i]]了,那么就break好了,不然会出错。

因为可能会出现怎么也无法满足的情况,所以我们需要多增加一场流星雨,这场流星雨的数量为+oo,保证能够让所有国家都满足要求,那么最后,对于所有答案为k+1的询问,输出NIE就行了。

#include<cstdio>
#include<cstring>
#include<iostream>
#define lowbit(x) (x&-x)
using namespace std;
typedef long long ll;
inline void read(int &x){
register char ch=getchar();x=0;
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
}
const int N=3e5+5;
const ll oo=0x7ffffffffffffLL;
struct query{
int x,y;ll d;
query(){}
query(int _x,int _y,ll _d){
x=_x,y=_y,d=_d;
}
}q[N];int m,n,k,P[N],ans[N];
int id[N],idl[N],idr[N];
ll nans[N],lans[N];
int tot,to[N],head[N],next[N];
struct BIT{
ll c[N];
inline void clr(){memset(c,0,sizeof c);}
inline void plus(int p,ll v){
for(int i=p;i<=m;i+=lowbit(i)) c[i]+=v;
}
inline void opera(int l,int r,ll v){
plus(l,v);plus(r+1,-v);
}
inline ll qsum(int &p){
ll res=0;
for(int i=p;i;i-=lowbit(i)) res+=c[i];
return res;
}
}bit;
inline void add(int x,int y){
to[++tot]=y;next[tot]=head[x];head[x]=tot;
}
//链表记录国家对应环上的点
//nans记入当前区间的贡献(贡献:当前区间所有不同点的陨石量)
//lans记入上一区间(l-1区间)的贡献
void divide(int opl,int opr,int l,int r){
if(opl>opr||l>r) return ;
if(l==r){
for(int i=opl;i<=opr;i++) ans[id[i]]=l;
return ;
}
int ql=0,qr=0;
int mid=(l+r)>>1;
for(int i=l;i<=mid;i++){
if(q[i].x<=q[i].y) bit.opera(q[i].x,q[i].y,q[i].d);
else bit.opera(q[i].x,m,q[i].d),bit.opera(1,q[i].y,q[i].d);
}
for(int i=opl;i<=opr;i++){
nans[id[i]]=0;
for(int j=head[id[i]];j;j=next[j]){
nans[id[i]]+=bit.qsum(to[j]);
if(nans[id[i]]+lans[id[i]]>=(ll)P[id[i]]) break;
}
if(nans[id[i]]+lans[id[i]]>=(ll)P[id[i]]) idl[++ql]=id[i];
else idr[++qr]=id[i],lans[id[i]]+=nans[id[i]];
}
for(int i=l;i<=mid;i++){
if(q[i].x<=q[i].y) bit.opera(q[i].x,q[i].y,-q[i].d);
else bit.opera(q[i].x,m,-q[i].d),bit.opera(1,q[i].y,-q[i].d);
}
for(int i=1;i<=ql;i++) id[opl+i-1]=idl[i];
for(int i=1;i<=qr;i++) id[opl+ql+i-1]=idr[i];
divide(opl,opl+ql-1,l,mid);
divide(opl+ql,opr,mid+1,r);
}
int main(){
read(n);read(m);
for(int i=1,x;i<=m;i++) read(x),add(x,i);
for(int i=1,x;i<=n;i++) read(P[i]),id[i]=i;
read(k);
for(int i=1,l,r,x;i<=k;i++) read(l),read(r),read(x),q[i]=query(l,r,x);
q[++k]=query(1,m,oo);
divide(1,n,1,k);
for(int i=1;i<=n;i++) if(ans[i]!=k) printf("%d\n",ans[i]);else puts("NIE");
return 0;
}

2527: [Poi2011]Meteors[整体二分]的更多相关文章

  1. bzoj 2527: [Poi2011]Meteors 整体二分

    给每个国家建一个链表,这样分治过程中的复杂度就和序列长度线形相关了,无脑套整体二分就可以. (最坑的地方是如果所有位置都是一个国家,那么它的样本个数会爆longlong!!被这个坑了一次,大于p[i] ...

  2. BZOJ 2527 [Poi2011]Meteors (整体二分+树状数组)

    整体二分板题,没啥好讲的-注意是个环-还有所有贡献会爆longlong,那么只要在加之前判断一下有没有达到需要的值就行了- CODE #include <set> #include < ...

  3. 【BZOJ2527】[Poi2011]Meteors 整体二分

    [BZOJ2527][Poi2011]Meteors Description Byteotian Interstellar Union (BIU) has recently discovered a ...

  4. BZOJ2527[Poi2011]Meteors——整体二分+树状数组

    题目描述 Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby galaxy. The ...

  5. BZOJ2527 [Poi2011]Meteors 整体二分 树状数组

    原文链接http://www.cnblogs.com/zhouzhendong/p/8686460.html 题目传送门 - BZOJ2527 题意 有$n$个国家. 太空里有$m$个太空站排成一个圆 ...

  6. Luogu3527 POI2011 Meteors 整体二分、树状数组、差分

    传送门 比较板子的整体二分题目,时限有点紧注意常数 整体二分的过程中将时间在\([l,mid]\)之间的流星使用树状数组+差分进行维护,然后对所有国家查看一遍并分好类,递归下去,记得消除答案在\([m ...

  7. BZOJ.2527.[POI2011]MET-Meteors(整体二分)

    题目链接 BZOJ 洛谷 每个国家的答案可以二分+求前缀和,于是可以想到整体二分. 在每次Solve()中要更新所有国家得到的值,不同位置的空间站对应不同国家比较麻烦. 注意到每次Solve()其国家 ...

  8. 【bzoj2527】[Poi2011]Meteors 整体二分+树状数组

    题目描述 有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站. 这个星球经常会下陨石雨.BIU已经预测了接下来K场陨石雨的情况.BI ...

  9. BZOJ 2527 [POI2011]MET-Meteors (整体二分+树状数组)

    题目大意:略 洛谷传送门 整体二分裸题 考虑只有一个国家的情况如何处理 对询问数量二分答案,暴力$O(m)$打差分,求前缀和验证,时间是$O(mlogK)$ 如果有$n$个国家,就是$O(nmlogK ...

随机推荐

  1. linux 上安裝lnmp

    1.確保有一台服務器可以正常運行 2.熟練知道一些基本的命令 3.這裡我以lnmp集成環境為例 https://lnmp.org/install.html 4.安裝大約30分鐘左右 5.安裝完畢,訪問 ...

  2. docker默认ip查询

    查询docker ip地址 docker-machine ip default

  3. 安卓开发笔记——TabHost组件(二)(实现底部菜单导航)

    上面文章<安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)>中提到了利用自定义View(ImageView+TextView)来设置一个底部菜单的样式 这边再补充一种更为灵 ...

  4. redhat enterprise edition 6.8:禁止ipv6后,nfs文件系统无法挂载:no such device

    如题:谨记. 附注:如何禁止ipv6? 方法一 第一种方法是通过 /etc/sysctl.conf 文件对 /proc 进行永久修改. 换句话说,就是用文本编辑器打开 /etc/sysctl.conf ...

  5. VS或编译的时候不生成Release文件夹

    今天在编译第三方类的时候,总是发布的时候报没有第三方类库的的Release版本 解决方案: Build=>Configuration Manager=>Release 编译=>配置管 ...

  6. yum常用操作

    一.yum安装使用: 1.Yum:rpm的前端程序,用来解决软件包相关依赖性,可以在多个库之间定位软件包,up2date的替代工具 2.yum repository:yum repo,存储了众多rpm ...

  7. 使用d3.v3插件绘制出svg图

    众所周知,这个插件使用的svg技术,而IE8(包括IE8)之前的浏览器是不支持svg的 接下来看代码吧 从后台获取到带id和父id的目录数据[json格式] var module = requestU ...

  8. CouchDB 未授权访问漏洞

    0x00 CouchDB安装 CouchDB官方网站:http://couchdb.apache.org/ 一路Next直到完成安装,打开浏览器并访问以下链接:http://127.0.0.1:598 ...

  9. Python闭包装饰器笔记

    Python三大器有迭代器,生成器,装饰器,这三个中使用最多,最重要的就是装饰器.本篇将重要从函数嵌套开始讲起,从而引入闭包,装饰器的各种用法等. python中的一切都是一个对象(函数也是) 1.首 ...

  10. C语言从零开始(十四)-字符串处理

    在软件开发过程中,字符串的操作相当频繁.在标准C语言库中提供了很多字符串处理的函数.今天我们来介绍一些常用的字符串处理函数.1. 字符串输入输出1.1 printf() scanf() 之前我们学习过 ...