整体二分+树状数组


  整体二分……感谢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. Zookeeper服务器集群的搭建与操作

    ZooKeeper 作用:Zookeeper 可以用来保证数据在zk集群之间的数据的事务性一致(原子操作). 介绍:Zookeeper 是 Google 的 Chubby一个开源的实现,是 Hadoo ...

  2. NPOI操作Excel文件

    首先,通过NuGet添加NPOI. NPOI依赖SharpZipLib,通过NuGet添加SharpZipLib. 然后添加NPOI. 添加后项目的引用列表如下: 把DataTable转换成Excel ...

  3. 关于谷歌浏览器62版本之后引用video.js不能自动播放的问题(Cross-origin plugin content from http://vjs.zencdn.net/swf/5.0.0-rc1/video-js.swf must have a visible size larger than 400 x 300 pixels, or it will be blocked.)

    Cross-origin plugin content from http://vjs.zencdn.net/swf/5.0.0-rc1/video-js.swf must have a visibl ...

  4. ACM训练计划建议(转)

    ACM训练计划建议 From:freecode#  Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...

  5. Adobe PS CS6安装详解

    Adobe PS CS6安装破解详解 注:电脑上是否拥有虚拟光驱,若是没有,推荐2345好压:官网http://haozip.2345.com/下载地址:http://dl.2345.com/haoz ...

  6. Ubuntu16.04 下的网易云出现网络异常、无法播放,界面无响应问题的统一解决

    能够在Linux系统下体验到原生界面的网易云音乐是件不错的事情,但是它总是经常性的出现网络异常,界面无响应的问题 为了听歌的体验,进行深入探究: 首先通过终端启用网易云音乐:sudo netease- ...

  7. CSUOJ 1270 Swap Digits

    Description ) in the first line, which has the same meaning as above. And the number is in the next ...

  8. 【原创】MySQL常用脚本整理

    #1.查看表容量空间 ) ) ) AS 'free_size(G)' FROM information_schema.tables WHERE TABLE_SCHEMA='test' AND TABL ...

  9. JDK源码分析(五)——HashSet

    目录 HashSet概述 内部字段及构造方法 存储元素 删除元素 包含元素 总结 HashSet概述   从前面开始,已经分析过集合中的List和Map,今天来介绍另一种集合元素:Set.这是JDK对 ...

  10. python学习笔记 - for循环: 遍历字典, 分别打印key, value, key:value

    #遍历字典, 分别打印key, value, key:value emp = {'name':'Tom', 'age':20, 'salary' : 8800.00} for k in emp.key ...