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. memcached系列之

    Slab Allocator的机制分配.管理内存 slabs---->slabs class:chunk size------>申请内存后分配的规格. chunk-->存放记录的单位 ...

  2. 【翻译自mos文章】在RHEL7/OL7上安装Oracle 12.1.0.2的server端或者client时,报须要&quot;compat-libstdc++&quot;包

    在RHEL7/OL7上安装Oracle 12.1.0.2的server端或者client时,报须要"compat-libstdc++"包 来源于: Installation of ...

  3. 调用 COM 对象

    调用 COM 对象 大多数 Windows 程序猿都熟悉组件对象模型(Component Object Model,COM).在某程度上..NET 框架 就是为了替换 COM,可是.系统仍然保留了这个 ...

  4. EF6 Code First & Auto Migration on Appharbor

    之前不小心看到EF的code first在appharbor上进行migration的时候比较麻烦,今天碰巧也要更新数据库了,顺便试试. modify model public class SiteI ...

  5. MathType如何设置标尺的单位

    MathType在编辑公式的时候,经常会需要将公式对齐.在将公式对齐的这个过程中,有时候会用到标尺,这样可以更精确的定位公式的位置.我们在使用标尺的时候,有时候会发现标尺上显示的是英寸,而我们平常已经 ...

  6. Android开发学习笔记-关于Android的消息推送以及前后台切换

    下面是最简单的Android的消息推送的实现方法 package com.example.shownotic; import java.util.Random; import android.supp ...

  7. 纯CSS制作的TAB选项卡

    代码 这里主要使用表单的单选按钮来实现这个TAB显示和隐藏,首页tab里的内容默认隐藏,如果单选按钮为选中状态(checked)就显示内容.具体请看下面代码. 关于兼容性,因为是用CSS3来制作的,所 ...

  8. 【2018年12月14日】A股最便宜的股票

    新钢股份(SH600782) - 当前便宜指数:193.12 - 滚动扣非市盈率PE:2.91 - 动态市净率PB:0.96 - 动态年化股息收益率:1.75% - 新钢股份(SH600782)的历史 ...

  9. 阿里云被挖矿使用,导致cpu长期处于100%,ddgs进程,xWx3T进程,关于redis密码

    1.使用top命令,查看到一个叫xWx3T的进程cpu占用99.8%,由于我的阿里云是单核的,所以最高只能100%. 把它用kill命令杀死后,过一会儿又启动了,又占用100%. 使用ps -ef可以 ...

  10. 【Access2007】将Excel表导入至Access2007的当中一张已存在的表之中

    将Excel表导入至Access2007,你会发现万恶的Access2007会帮你自己主动创建一张表.全然没有问你是否要插入一张已存在的表之中. 那么,我们须要这样解决: 一.依照正常的步骤先将Exc ...