整体二分+树状数组


  整体二分……感谢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. 2017冬季24集训模拟-2.A问题

    ————————————————————————————————————————题解 唯一没有想出来的题 我们发现以上两种操作 a0,a3,a6,a9……的相对位置不变 a1,a4,a7,a10……的 ...

  2. Ionic入门六:按钮

    1.基本使用 按钮是移动app不可或缺的一部分,不同风格的app,需要的不同按钮的样式. 默认情况下,按钮显示样式为:display: inline-block. <button class=& ...

  3. linux 杂类

    1.linux 下替换windows换行符命令   set ff=unix(命令行)

  4. @Resource与@Autowired注解的区别(转)

    Spring不但支持自己定义的@Autowired注解,还支持由JSR-250规范定义的几个注解.如:@Resource.@PostConstruct及@PreDestroy 1.@Autowired ...

  5. bzoj——2127: happiness

    2127: happiness Time Limit: 51 Sec  Memory Limit: 259 MBSubmit: 2570  Solved: 1242[Submit][Status][D ...

  6. JAVAEE——淘淘商城第一天:电商行业的背景和技术特点,商城的介绍、技术的选型、系统架构和工程搭建

    1. 学习计划 1.电商行业的背景. 2.电商行业的技术特点 3.商城的介绍 a) 常用的名词介绍 b) 系统功能介绍 4.淘淘商城的系统架构 a) 传统架构 b) 分布式架构 c) 基于服务的架构 ...

  7. c#程序员机试题

    一.题目: 有一数组: int[] arr = new int[] { 48,1,3,55,15,29,12,33,26,41,56,32}; 1.求出最大值 2.按每个数字的10位数分组(说明:0~ ...

  8. python装饰器原理

    妙处在于装饰器的两个return 1.装饰器 # 使用闭包 def wrap(fun): def check(): print("正在检查用户权限!") fun() return ...

  9. zip函数实际应用

    一个需求,把两个列表要同时用for循环贴到模板上,用zip在后端组成  元祖组成的列表  这样在模板就可以用.0   .1的方式,类似python中 x[0]   x[1] 显示出来. html显示: ...

  10. springboot中使用JOIN实现关联表查询

    * 首先要确保你的表和想要关联的表有外键连接 repository中添加接口JpaSpecificationExecutor<?>,就可以使用springboot jpa 提供的API了. ...