整体二分+树状数组


  整体二分……感谢zyf提供的入门题

  简单粗暴的做法:枚举每一个国家,二分他的$w_i$,然后计算……然而这样效率很低……

  整体二分就是:对所有的国家一起进行二分,$w_i$在mid之前的,进左边,否则进右边(是不是很像线段树呀~其实就是吧……

  其实感觉代码写起来也跟线段树有些相似?

  

  RE:我没有在结束的时候加if (L==R) return;这样的话,只靠开头的 if (!V[o].size()) return;当极限数据的时候,走到叶子再乘二的话,就超出4*N的范围了……就跑到外面去了……所以就RE了……

 /**************************************************************
Problem: 2527
User: Tunix
Language: C++
Result: Accepted
Time:17700 ms
Memory:56832 kb
****************************************************************/ //BZOJ 2527
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
#define pb push_back
using namespace std;
typedef long long LL;
inline int getint(){
int r=,v=; char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if (ch=='-') r=-;
for(; isdigit(ch);ch=getchar()) v=v*-''+ch;
return r*v;
}
const int N=3e5+;
/*******************template********************/ int n,m,K,p[N],l[N],r[N],a[N],ans[N],tot;
vector<int>G[N],V[N<<];
LL c[N];
void add(int x,LL v){
for(;x<=m;x+=x&(-x)) c[x]+=v;
}
LL sum(int x){
LL r=;
for(;x;x-=x&(-x)) r+=c[x];
return r;
}
void solve(int o,int L,int R){
if (!V[o].size()) return;
int mid=(L+R)>>;
F(i,L,mid){
if (l[i]>r[i]) add(,a[i]);
add(l[i],a[i]); add(r[i]+,-a[i]);
}
rep(i,V[o].size()){
int x=V[o][i]; LL y=;
rep(j,G[x].size()){
y+=sum(G[x][j]);
if (y>=p[x]) break;
}
if (y>=p[x]){
if (L==R) ans[x]=L;
else V[o<<].pb(x);
}else{
if (L==R) ans[x]=-;
else{
p[x]-=y;
V[o<<|].pb(x);
}
}
}
F(i,L,mid){
if (l[i]>r[i]) add(,-a[i]);
add(l[i],-a[i]); add(r[i]+,a[i]);
}
if (L==R) return;
solve(o<<,L,mid); solve(o<<|,mid+,R);
} int main(){
#ifndef ONLINE_JUDGE
freopen("2527.in","r",stdin);
freopen("2527.out","w",stdout);
#endif
n=getint(); m=getint();
F(i,,m){
int x=getint();
G[x].pb(i);
}
F(i,,n) p[i]=getint();
K=getint();
F(i,,K){
l[i]=getint(); r[i]=getint(); a[i]=getint();
}
F(i,,n) V[].pb(i);
solve(,,K);
F(i,,n){
if (ans[i]!=-) printf("%d\n",ans[i]);
else puts("NIE");
}
return ;
}

2527: [Poi2011]Meteors

Time Limit: 60 Sec  Memory Limit: 128 MB
Submit: 415  Solved: 165
[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

[Submit][Status][Discuss]

【BZOJ】【2527】【POI2011】Meteors的更多相关文章

  1. 【BZOJ 2754 喵星球上的点名】

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2512  Solved: 1092[Submit][Status][Discuss] Descript ...

  2. 【BZOJ】【2738】&【Tsinsen】【A1333】矩阵乘法

    整体二分+树状数组 过了[BZOJ][2527][POI2011]Meteors以后这题就没那么难啦~ 关键是[从小到大]依次插入数字,然后整体二分每个查询的第k大是在第几次插入中被插入的……嗯大概就 ...

  3. 【BZOJ】3052: [wc2013]糖果公园

    http://www.lydsy.com/JudgeOnline/problem.php?id=3052 题意:n个带颜色的点(m种),q次询问,每次询问x到y的路径上sum{w[次数]*v[颜色]} ...

  4. 【BZOJ】3319: 黑白树

    http://www.lydsy.com/JudgeOnline/problem.php?id=3319 题意:给一棵n节点的树(n<=1e6),m个操作(m<=1e6),每次操作有两种: ...

  5. 【BZOJ】3319: 黑白树(并查集+特殊的技巧/-树链剖分+线段树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3319 以为是模板题就复习了下hld............................. 然后n ...

  6. 【BZOJ】【1025】【SCOI2009】游戏

    DP/整数拆分 整个映射关系可以分解成几个循环(置换群的预备知识?),那么总行数就等于各个循环长度的最小公倍数+1(因为有个第一行的1~N).那么有多少种可能的排数就等于问有多少种可能的最小公倍数. ...

  7. 【BZOJ】1013: [JSOI2008]球形空间产生器sphere

    [BZOJ]1013: [JSOI2008]球形空间产生器sphere 题意:给n+1个n维的点的坐标,要你求出一个到这n+1个点距离相等的点的坐标: 思路:高斯消元即第i个点和第i+1个点处理出一个 ...

  8. 【BZOJ】1002:轮状病毒(基尔霍夫矩阵【附公式推导】或打表)

    Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下图 ...

  9. 【BZOJ】【3697】采药人的路径&【3127】【USACO2013 Open】Yin and Yang

    点分治 Orz hzwer 倒是比较好想到点分治……然而在方案统计这里,我犯了两个错误…… 1.我比较傻逼的想的是:通过儿子来更新父亲,也就是统计以x为根的子树中xxxx的路径有多少条……这样转移. ...

  10. 【BZOJ】【3083】遥远的国度

    树链剖分/dfs序 其实过了[BZOJ][4034][HAOI2015]T2以后就好搞了…… 链修改+子树查询+换根 其实静态树的换根直接树链剖分就可以搞了…… 因为其实只有一样变了:子树 如果roo ...

随机推荐

  1. thinkphp中导入和使用阿里云OSSsdk

    照做绝对行,在ThinkPHP中,第三方库都放在ThinkPHP/Library/Vendor/路径下. 1.下载OSS PHP SDK:https://help.aliyun.com/documen ...

  2. scala递归实现换零钱算法

    import scala.collection.mutable.ArrayBuffer import scala.util.control.Breaks object Exchange { def d ...

  3. MySQL 类型转换

    1.问题描述 在项目中遇到要将Int类型转为Char类型,然后利用转化后的Char类型进行模糊查询. 例:合同编号在数据库中为int类型 8066 用利用 806 模糊查询出合同编号为8066数据记录 ...

  4. JavaScript DOM大纲

    DOM 定义了访问和操作HTML文档的标准方法 访问(查找标签) ---- 直接查找 document.getElementById(“idname”) document.getElementsByT ...

  5. iOS 11开发教程(十二)iOS11应用视图始祖——UIView

    iOS 11开发教程(十二)iOS11应用视图始祖——UIView 在Swift中,NSObject是所有类的根类.同样在UIKit框架(UIKit框架为iOS应用程序提供界面对象和控制器)中,也存在 ...

  6. window.open()/剪切板ZeroClipboard

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Go语言特点

    作者:asta谢链接:https://www.zhihu.com/question/21409296/answer/18184584来源:知乎 1.Go有什么优势 可直接编译成机器码,不依赖其他库,g ...

  8. 如何成为一名优秀的CTO(首席技术官)

    最近我发现很多开发人员都表示不知道如何规划职业生涯的下一个步骤.基于我们目前所处的科技泡沫现象,很多工程师都倾向于留在大型的成熟公司,或者要么a)去初创企业工作要么b)自己搞初创公司. 回顾我自己的职 ...

  9. 本地文件包含漏洞(LFI漏洞)

    0x00 前言 本文的主要目的是分享在服务器遭受文件包含漏洞时,使用各种技术对Web服务器进行攻击的想法. 我们都知道LFI漏洞允许用户通过在URL中包括一个文件.在本文中,我使用了bWAPP和DVW ...

  10. hdu 3061 最大权闭合子图

    属于模板题吧... #include <cstdio> #include <cstring> #include <vector> #define min(a,b) ...