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 - 可持久化线段树 - 二分法的更多相关文章

  1. 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 ...

  2. 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 ...

  3. BZOJ - 2588 Spoj 10628. Count on a tree (可持久化线段树+LCA/树链剖分)

    题目链接 第一种方法,dfs序上建可持久化线段树,然后询问的时候把两点之间的所有树链扒出来做差. #include<bits/stdc++.h> using namespace std; ...

  4. 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 ...

  5. 计蒜客 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 ...

  6. PYOJ 44. 【HNSDFZ2016 #6】可持久化线段树

    #44. [HNSDFZ2016 #6]可持久化线段树 统计 描述 提交 自定义测试 题目描述 现有一序列 AA.您需要写一棵可持久化线段树,以实现如下操作: A v p x:对于版本v的序列,给 A ...

  7. 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1878  Solved: 846[Submit][Status ...

  8. 【BZOJ-2653】middle 可持久化线段树 + 二分

    2653: middle Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1298  Solved: 734[Submit][Status][Discu ...

  9. HDU 4866 Shooting(持久化线段树)

    view code//第二道持久化线段树,照着别人的代码慢慢敲,还是有点不理解 #include <iostream> #include <cstdio> #include & ...

随机推荐

  1. 用log4net记录日志信息

    在.net中用log4net记录日志信息,已经是很平常的事情了. log4net下载:http://logging.apache.org/log4net/download_log4net.cgi 百度 ...

  2. SSH--完全分布式主机设置【克隆过安装过Hadoop的主机后】

    ====准备完全分布式主机的ssh==== 2018-12-21 14:27:47 1.删除所有主机上.ssh下所有文件 2.在s250主机上生成密钥对 $>ssh-keygen -t rsa ...

  3. 阿里云esc服务器上部署java项目

    文章中使用centos(6和7版本).Xshell.Xftp 因为部署过程直接从操作服务器开始,如果你还没有连接好服务器请参考http://blog.csdn.net/ctrlxv/article/d ...

  4. LeetCode83.删除排序链表中的重复的元素

    给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: 输入: 1->1->2->3-&g ...

  5. a标签响应onclick事件,并且不执行href动作

    1.javascript:void(0)相当于一个死链接,href不执行 <a href="javascript:void(0)" onclick="doSomet ...

  6. vue框架(三)_vue引入jquery、bootstrap

    一.vue安装jquery 1.按照之前博客的内容,新建一个vue工程. 2.在项目文件夹下,使用命令npm install jquery --save-dev 引入jquery. 3.在build/ ...

  7. Thinkphp 框架

    核心函数库:   配置文件:     语言包  :  核心资源库:入口文件 核心配置文件 系统行为目录:功能目录:核心文件:摸板类文件目录

  8. 软工网络15团队作业4——Alpha阶段敏捷冲刺1.0

    软工网络15团队作业4--Alpha阶段敏捷冲刺1.0 1. 各个成员在 Alpha 阶段认领的任务,以及整个项目预期的任务量(使用整数表示,与项目预估的总工作小时数一致.比如项目A预估需120小时才 ...

  9. MQTT 发布者订阅者

    添加依赖: <dependency> <groupId>org.eclipse.paho</groupId> <artifactId>org.eclip ...

  10. linux环境下tab键自动缩进4个空格

    1. 进入 root 模式 su root 2. 编辑 /etc/vimrc 文件 root@localhost /home/xiluhua/tscripts $ vi /etc/vimrc 3. 文 ...