【bzoj2527】[Poi2011]Meteors

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 forno) 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
 
整体二分,就是二分到哪里为止,然后就是统计,判断有没有到了收集个数,
到了放左边,没到放右边,这样继续二分,如何判断NIE,最后加一个inf的,
就是k+1的那一轮一定可以满足所有条件即可。
 
 #include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<vector> #define ll long long
#define N 300007
#define inf 1000000009
using namespace std;
inline int read ()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if (ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,m,k,T;
int ans[N],id[N],l[N],r[N],val[N],num[N],tmp[N];
ll tr[N];
bool mark[N];
vector<int>a[N]; inline int lowbit(int x){return x&(-x);}
void add(int x,int num)
{
if (x>m) return;
for (int i=x;i<=m;i+=lowbit(i)) tr[i]+=num;
}
ll query(int x)
{
ll res=;
for (int i=x;i>=;i-=lowbit(i))
res+=tr[i];
return res;
}
void update(int x,int f)
{
if (l[x]<=r[x]) add(l[x],f*val[x]),add(r[x]+,f*val[x]*-);
else add(,f*val[x]),add(r[x]+,f*val[x]*-),add(l[x],f*val[x]);
}
void solve(int l,int r,int L,int R)
{
if (l>r) return;
if(L==R)
{
for (int i=l;i<=r;i++)ans[id[i]]=L;
return;
}
int mid=(L+R)>>;
while(T<=mid)T++,update(T,);
while(T>mid)update(T,-),T--;
int cnt=,now;ll tot;
for (int i=l;i<=r;i++)
{
tot=,now=id[i];
for (int j=;j<a[now].size();j++)
{
tot+=query(a[now][j]);
if (tot>=num[now])break;//优化
}
if (tot>=num[now]) mark[now]=,cnt++;
else mark[now]=;
} int l1=l,l2=l+cnt;
for (int i=l;i<=r;i++)
if(mark[id[i]])tmp[l1++]=id[i];
else tmp[l2++]=id[i];
for (int i=l;i<=r;i++)id[i]=tmp[i]; solve(l,l1-,L,mid),solve(l1,l2-,mid+,R);
}
int main()
{
n=read(),m=read();
for (int i=;i<=m;i++){int x=read();a[x].push_back(i);}
for (int i=;i<=n;i++)num[i]=read();
k=read();
for (int i=;i<=k;i++)l[i]=read(),r[i]=read(),val[i]=read();
k++,l[k]=,r[k]=n,val[k]=inf;
for (int i=;i<=n;i++)id[i]=i;
solve(,n,,k);
for (int i=;i<=n;i++)
if (ans[i]!=k) printf("%d\n",ans[i]);
else printf("NIE\n");
}

【bzoj2527】[Poi2011]Meteors(树状数组(单点查询,区间修改)+整体二分)的更多相关文章

  1. 题解报告:Luogu P3368 【模板】树状数组 2(区间修改,单点查询)

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数数加上x 2.求出某一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. ...

  2. A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. poj 2763 Housewife Wind(树链剖分+单点查询+区间修改)

    题目链接:http://poj.org/problem?id=2763 题意:给一个数,边之间有权值,然后两种操作,第一种:求任意两点的权值和,第二,修改树上两点的权值. 题解:简单的树链剖分. #i ...

  4. Libre OJ 130、131、132 (树状数组 单点修改、区间查询 -> 区间修改,单点查询 -> 区间修改,区间查询)

    这三题均可以用树状数组.分块或线段树来做 #130. 树状数组 1 :单点修改,区间查询 题目链接:https://loj.ac/problem/130 题目描述 这是一道模板题. 给定数列 a[1] ...

  5. TZOJ 2725 See you~(二维树状数组单点更新区间查询)

    描述 Now I am leaving hust acm. In the past two and half years, I learned so many knowledge about Algo ...

  6. hdu 2642二维树状数组 单点更新区间查询 模板题

    二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...

  7. POJ 2155 Matrix【二维树状数组+YY(区间计数)】

    题目链接:http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissio ...

  8. P3368 【模板】树状数组 2(区间增减,单点查询)

    P3368 [模板]树状数组 2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数数加上x 2.求出某一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表 ...

  9. 洛谷 P3368 【模板】树状数组 2(区间加,单点查询)

    题目链接 https://www.luogu.org/problemnew/show/P3368 树状数组 最基础的用法:https://www.cnblogs.com/yinyuqin/p/1096 ...

  10. nyoj 123 士兵杀敌(四) 树状数组【单点查询+区间修改】

    士兵杀敌(四) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5   描述 南将军麾下有百万精兵,现已知共有M个士兵,编号为1~M,每次有任务的时候,总会有一批编号连在一起人请战 ...

随机推荐

  1. java 缓冲流 Buffer

    缓冲流 Buffer :设置缓冲区加快执行效率 子类: (一)BufferedInputStream : 缓冲输入字节流 ,目的:提高读取文件的效率  注意: BufferedInputStream ...

  2. 完美单例宏定义(兼容ARC和MRC),项目中可以直接使用

     单例模式: 1.永远只分配一块内存来创建对象 2.提供一个类方法, 返回内部唯一的一个对象(一个实例) 3.最好保证init方法也只初始化一次 写一个宏定义文件,传入宏定义函数名,自动生成符合类名的 ...

  3. 【分享】iTOP-iMX6UL开发板驱动看门狗 watchdog 以及 Linux-c 测试例程

    iTOP-iMX6UL开发板看门狗测试例程,iTOP-iMX6UL 开发板的看门狗驱动默认已经配置,可以直接使用测试例程. 版本 V1.1:1.格式修改:2.例程修改完善,其中增加喂狗代码.1 看门狗 ...

  4. Google浏览器开发者工具:CSSViewer(一个Css查看器)

    CSSViewer的简介 CSSViewer是一款可以帮助用户快速查看当前的网页元素的CSS属性的谷歌浏览器插件,在Chrome中安装了CSSViewer插件以后,用户就可以在设计网页的时候,快速地模 ...

  5. 在Swift中定义属于自己的运算符

    precedencegroup ChainingPrecedence { associativity: left higherThan: TernaryPrecedence } infix opera ...

  6. loadrunner 响应时间和TPS

    例子:一个高速路有10个入口,每个入口每秒钟只能进1辆车 1.请问1秒钟最多能进几辆车?    TPS=10 2.每辆车需要多长时间进行响应?    reponse time = 1 3.改成20辆车 ...

  7. c++ 数组长度

    数组长度求解 sizeof template <class T>int getArrayLen(T &array){ return (sizeof(array) / sizeof( ...

  8. DEBUG无法进入断点解决方法

    18/08/17 任务栏:Tools->Options->Debugging->General->Require source files to exactly match t ...

  9. hive纯命令行

    vim /etc/profileexport HIVE_HOME=/export/servers/hive...export PATH=:$HIVE_HOME/bin:$PATH 前台启动hive:h ...

  10. 秋招复习-C++( 一)

    Linux/Unix编程部分 1.进程间通信方式:信号,信号量,消息队列,共享内存,套接字Socket 2.ipcs: Linux/Unix下的命令,可以用来查看当前系统中所使用的进程间通信方式的各种 ...