SPOJ Meteors - 可持久化线段树 - 二分法
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 sectors, numbered from
to
, where the sectors
and
are adjacent. In each sector there is a single space station, belonging to one of the
member 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.
Input
The first line of the standard input gives two integers, and
(
), 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 integers
(
), separated by single spaces, that denote the states owning stations in successive sectors.
In the third line there are integers
(
), 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 (
) that denotes the number of meteor showers predictions. The following
lines specify the (predicted) meteor showers chronologically. The
-th of these lines holds three integers
,
,
(separated by single spaces), which denote that a meteor shower is expected in sectors
(if
) or sectors
(if
), which should provide each station in those sectors with
meteor samples (
).
Output
Your program should print lines on the standard output. The
-th of them should contain a single integer
, denoting the number of shower after which the stations belonging to the
-th state are expected to gather at least
samples, or the wordNIE (Polish for no) if that state is not expected to gather enough samples in the foreseeable future.
Example
For the input data:
3 5
1 3 2 1 3
10 5 7
3
4 2 4
1 3 1
3 5 2
the correct result is:
3
NIE
1
题目大意 一个星球的环形轨道上有m个空间站,每个空间站属于n个国家中的一个,有k次流星雨,每次会使得一段空间站可以获得一些陨石,每个国家有个收集目标,问每个国家在第多少次流星雨后达成目标,或者无法达到目标。
将每次流星雨当成一次区间修改,修改可持久化线段树。用vector存下每个国家有哪些空间站。
然后枚举每个国家,二分答案,判断的时候暴力枚举每个国家的空间站,进行查询。
好在spoj不卡空间,bzoj空间直接卡死。然后我发现似乎我算节点数的时候忘记算常数了,发现内存池开小了很多,所以就动态开节点慢了许多。
Code
/**
* SPOJ
* Problem#METEORS
* Accepted
* Time:3220ms
* Memory:521216k
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <stack>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << ) - );
const double eps = 1e-;
const int binary_limit = ;
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} #define LL long long typedef class SegTreeNode {
public:
LL val;
LL lazy;
SegTreeNode *l, *r; SegTreeNode():val(), lazy(), l(NULL), r(NULL) { } inline void pushUp() {
val = l->val + r->val;
}
}SegTreeNode; #define LIMIT 5600000
SegTreeNode pool[LIMIT + ];
int top = ; inline SegTreeNode* newnode() {
if(top >= LIMIT) return new SegTreeNode();
return &pool[top++];
} inline SegTreeNode* newnode(SegTreeNode*& b) {
if(top >= LIMIT) {
SegTreeNode* p = new SegTreeNode();
*p = *b;
return p;
}
pool[top] = *b;
return &pool[top++];
} typedef class DurableSegTree {
public:
int now, n;
SegTreeNode** ls; DurableSegTree() { }
DurableSegTree(int n, int limit):now(), n(n) {
ls = new SegTreeNode*[(limit + )];
build(ls[], , n);
} void build(SegTreeNode*& node, int l, int r) {
node = newnode();
if(l == r) return;
int mid = (l + r) >> ;
build(node->l, l, mid);
build(node->r, mid + , r);
} void update(SegTreeNode*& old, SegTreeNode*& newo, int l, int r, int ql, int qr, LL val) {
newo = newnode(old);
if(ql > qr) return;
if(ql == l && qr == r) {
newo->lazy += val;
newo->l = old->l, newo->r = old->r;
return;
}
int mid = (l + r) >> ;
if(qr <= mid) {
newo->r = old->r;
update(old->l, newo->l, l, mid, ql, qr, val);
} else if(ql > mid){
newo->l = old->l;
update(old->r, newo->r, mid + , r, ql, qr, val);
} else {
update(old->l, newo->l, l, mid, ql, mid, val);
update(old->r, newo->r, mid + , r, mid + , qr, val);
}
newo->val += val * 1LL * (qr - ql + );
} inline void update(int l, int r, LL val) {
if(l <= r) {
update(ls[now], ls[now + ], , n, l, r, val);
} else {
update(ls[now], ls[now + ], , n, r + , l - , -val);
ls[now + ]->lazy += val;
}
now++;
} void query(SegTreeNode*& node, int l, int r, int idx, LL& ret) {
ret += node->lazy;
if(l == idx && r == idx) {
ret += node->val;
return;
}
int mid = (l + r) >> ;
if(idx <= mid) query(node->l, l, mid, idx, ret);
else query(node->r, mid + , r, idx, ret);
} inline LL query(int k, int pos) {
LL ret = ;
query(ls[k], , n, pos, ret);
return ret;
}
}DurableSegTree; int n, m, q;
vector<int> *owns;
int *goals;
DurableSegTree dst; inline void init() {
readInteger(n);
readInteger(m);
owns = new vector<int>[n + ];
goals = new int[(n + )];
for(int i = , x; i <= m; i++) {
readInteger(x);
owns[x].push_back(i);
}
for(int i = ; i <= n; i++)
readInteger(goals[i]); readInteger(q);
dst = DurableSegTree(m, q + );
for(int i = , a, b, c; i <= q; i++) {
readInteger(a);
readInteger(b);
readInteger(c);
dst.update(a, b, c);
}
} boolean check(int country, int mid) {
LL ret = ;
for(int i = ; i < (signed)owns[country].size() && ret < goals[country]; i++)
ret += dst.query(mid, owns[country][i]);
return ret >= goals[country];
} inline void solve() {
for(int c = ; c <= n; c++) {
int l = , r = q;
while(l <= r) {
int mid = (l + r) >> ;
if(check(c, mid)) r = mid - ;
else l = mid + ;
}
if(r == q) puts("NIE");
else printf("%d\n", r + );
}
} int main() {
// freopen("meteors.in", "r", stdin);
init();
solve();
return ;
}
SPOJ Meteors - 可持久化线段树 - 二分法的更多相关文章
- BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 9280 Solved: 2421 ...
- HDU 4348.To the moon SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- BZOJ - 2588 Spoj 10628. Count on a tree (可持久化线段树+LCA/树链剖分)
题目链接 第一种方法,dfs序上建可持久化线段树,然后询问的时候把两点之间的所有树链扒出来做差. #include<bits/stdc++.h> using namespace std; ...
- SPOJ 3267 D-query (可持久化线段树,区间重复元素个数)
D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- PYOJ 44. 【HNSDFZ2016 #6】可持久化线段树
#44. [HNSDFZ2016 #6]可持久化线段树 统计 描述 提交 自定义测试 题目描述 现有一序列 AA.您需要写一棵可持久化线段树,以实现如下操作: A v p x:对于版本v的序列,给 A ...
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
- 【BZOJ-2653】middle 可持久化线段树 + 二分
2653: middle Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1298 Solved: 734[Submit][Status][Discu ...
- HDU 4866 Shooting(持久化线段树)
view code//第二道持久化线段树,照着别人的代码慢慢敲,还是有点不理解 #include <iostream> #include <cstdio> #include & ...
随机推荐
- cocos2d-x JS 纯代码渲染Lable描边
/** * Enables shadow style and sets color, offset and blur radius styles. * @param {cc.Color} shadow ...
- java 之多线程
多线程基本概念_程序_线程 1.1程序.进程.线程 程序:Program是一个指令的集合 进程:Process(正在执行中的程序)是一个静态的概念.进程是程序的一次静态执行过程,占用特定的地址空间.每 ...
- 14.ajax基础知识、用ajax做登录页面、用ajax验证用户名是否可用、ajax动态调用数据库
1.ajax的基础知识 ajax是结合了jquery.php等几种技术延伸出来的综合运用的技术,不是新的内容.ajax也是写在<script>标签里面的. 如果使用ajax一定是要有1个处 ...
- summaryなな
Word如果遇到有空白页不能删除的情况,将光标定位在空白页前一页的末尾,然后按Delete键就可以删除空白页了. 实时计算,强调的是实时.比如小明要查看他去年一年的消费总额度,那么当小明点下统计按钮的 ...
- React创建组件的不同方式(ES5 & ES6)
一. 首先缕清楚React.createElement.React.createClass.React.Component之间的关系 1. React.createElement(HTML eleme ...
- 018-AJAX异步请求XMLHttpRequest
创建XMLHttpRequest对象 一.先来创建XMLHttpRequest对象在IE.Firefox.safari和Opera中创建该对象的JavaScript代码为: var xhr = new ...
- IE8 CSS hack
IE8正式版出来有一段日子了,但是针对ie8正式版的CSS hack却很少,其实这是值得庆幸的,因为ie8修复了很多IE6和IE7的一些BUG,更加接近W3C标准. 针对IE8正式版的CSS hack ...
- Codeforces Round #322 (Div. 2) E F
E. Kojiro and Furrari 题意说的是 在一条直线上 有n个加油站, 每加一单位体积的汽油 可以走1km 然后每个加油站只有一种类型的汽油,汽油的种类有3种 求从起点出发到达终点要求使 ...
- AmiGO2:在线浏览和查询GO信息的利器
GO数据库的信息是非常庞大的,为了有效的检索和浏览GO数据库的信息,官方提供了AmiGO, 可以方便的浏览,查询和下载对应信息,官网如下 http://amigo.geneontology.org/a ...
- 仿照admin实现一个自定义的增删改查的组件
1.首先,创建三个项目,app01,app02,stark,在settings里边记得配置.然后举例:在app01的model里边写表,用的db.sqlite3,所以数据库不用再settings里边配 ...