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. vue-lazyload的使用

    1.下载依赖 npm install vue-lazyload --save 2.引入 import Vue from 'vue' import App from '@/App' import Vue ...

  2. case when 遇到varchar转为int类型值失败的错误

    问题描述: 在Sql Server 2005下, 使用如下语句报错:在将 varchar 值 '大' 转换成数据类型 int 时失败. 注:status 是整型字段 select ff=  case ...

  3. C# 去重处理字符大小写

    本文展示了如何对集合去重并且处理大小写

  4. c# 确定dynamic类型的数据对象是否存在某个属性

    public static bool IsPropertyExist(dynamic data, string propertyname)   {     if (data is ExpandoObj ...

  5. TCP连接图示

    转移2018.4.6 自己总结绘图

  6. Azure Event Hub 技术研究系列1-Event Hub入门篇

    前两个系列研究了Azure IoT Hub和Azure Messaging.最近准备继续研究Azure Event Hub,即Azure的事件中心.首先, Azure Event Hub的官方介绍: ...

  7. KindEditor echarts

    var editor; KindEditor.ready(function (K) { editor = K.create('textarea[name="content"]', ...

  8. [openjudge-搜索]广度优先搜索之鸣人和佐助

    题目描述 描述 佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢?已知一张地图(以二维矩阵的形式表示)以及佐助和鸣人的位置.地图上的每个位置都可以走到,只不过有些位置上有大蛇丸的手下,需要先打败大蛇丸 ...

  9. Spring源码阅读(五)

    这一讲我们分析真正的bean实例创建方法——doCreateBean,源码分析如下 /** * Actually create the specified bean. Pre-creation pro ...

  10. Java解析Json字符串--复杂对象

    { "name": "三班", "students": [ { "age": 25, "gender" ...