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 & ...
随机推荐
- keras模型的保存与重新加载
# 模型保存JSON文件 model_json = model.to_json() with open('model.json', 'w') as file: file.write(model_jso ...
- windows连接远程win服务器失败,win7win10都存在此问题,显示出现身份验证错误,要求的函数不受支持,可能由于CredSSP加密Oracle修正 (原)
之前电脑windows+mstsc连接另一个windows服务器正常登陆,可在更新本地系统为win10以后,登陆就出现了问题,提示的错误是,出现身份验证错误.要求的函数不受支持,可能由于CredSSP ...
- 用C#创建一个窗体,在构造函数里面写代码和在from_load事件里面写代码有什么不同?
没太大区别.一区别就是代码加载时间先后的问题.构造函数先加载,load事件中后加载.
- docker-php-ext-install.md(https://gist.github.com/giansalex/2776a4206666d940d014792ab4700d80)
```bash RUN apt update RUN apt upgrade -y RUN apt install -y apt-utils RUN a2enmod rewrite RUN apt i ...
- SQL中的replace函数
REPLACE 用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式. 语法 REPLACE ( 'string_expression1' , 'string_expression2 ...
- Rpgmakermv(38)MOG_Theatrhythm
1.============================葡萄牙语=================================================+++ MOG - Theatrh ...
- DataRow 点不出 Select
DataRow 点不出 Select?why?using system.linq;
- html5-边框属性
width: 500px; height: 300px; background: rgb(122,30,60); border: 10px solid black; /*bor ...
- SLAM学习笔记
ORB_SLAM2源码: 获得旋转矩阵,来自这里:http://www.cnblogs.com/shang-slam/p/6406584.html 关于Covisibility图来自:http://b ...
- Java综合高级篇
1.你用过哪些集合类? 大公司最喜欢问的Java集合类面试题 40个Java集合面试问题和答案 java.util.Collections 是一个包装类.它包含有各种有关集合操作的静态多态方法. ja ...