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. 理解端口与IP

    理解IP和端口 端口,端口号,服务器端口------百科

  2. 64位系统web项目导出excel问题分析及解决方法汇总

    最近在web项目中做了一个导出Excel功能.在导出的时候报错:检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败. 一 ...

  3. DataTable内容导出为CSV文件

    CSVHelper.cs内容: using System; using System.Collections.Generic; using System.Linq; using System.Text ...

  4. 4 云计算系列之Openstack简介与keystone安装

    preface KVM 是openstack虚拟化的基础, 再介绍了kvm虚拟化技术之后,我们介绍下openstack和如何搭建. Openstack组件 openstack架构图如下所示 那么我们就 ...

  5. django 配置中STATICFILES_DIRS 和STATIC_ROOT不能同时出现

    系统环境: win7 django版本查看: 启动django项目的时候,一直找不到静态资源,很奇怪放在linux服务器上的时候好好的,拿下来随便修改了配置就说url找不到了. 用wingIDE没有任 ...

  6. 求字符串长度StringLength();

    // StringLength2.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" int StringLength(char str[]) ...

  7. linux环境中通过useradd命令,创建用户的时候指定用户的base-dir

    需求说明: 今天一个同事,问了一个这样的问题,在linux环境中,创建用户的时候,默认的是在/home目录下创建一个与用户名相同的家目录, 如何能够将这个/home更换成一个其他的,比如/opt/ap ...

  8. AngularJS------命令行

    如下:(‘$’符号不需要输入哦) $ ng build --发布项目

  9. mongodb 初学 索引

    连接服务器异常(Connection refused) 啦啦啦 mongodb 搭建主从服务器 啦啦啦 Mongodb启动命令mongod参数说明 啦啦啦 MongoDB 分片 啦啦啦 啦啦啦 啦啦啦 ...

  10. Winform判断EventHandler是否已经添加

    斜体部分替换成自己需要的 private bool HasValueChangedEventHandler(DateTimePicker b) { FieldInfo f1 = typeof(Date ...