#include <bits/stdc++.h>
using namespace std;
struct node{
long long l,r,v;
};
struct dataa{
long long v = 0;
vector<pair<long long,int>>lazy;
void add(long long left,long long right,pair<long long,int>val){
if (val.second == 2){
v = val.first;
}
else if (val.second == 1){
v += val.first;
}
if (val.second == 2){
if (!lazy.empty()){
lazy.back() = val;
}
else{
lazy.push_back(val);
}
}
else{
if (!lazy.empty()){
lazy.back().first+=val.first;
}
else{
lazy.push_back(val);
}
}
}
};
struct Segment_Tree{
vector<dataa>tree;
int m;
void init(long long n){
tree.resize(2*n - 1);
m = n - 1;
}
dataa combine(dataa& left,dataa& right){
dataa res;
res.v = left.v + right.v;
return res;
}
void push(long long node,long long left,long long right){
long long mid = (left + right)>>1;
long long z = node + ((mid - left + 1)<<1);
while (!tree[node].lazy.empty()){
tree[node + 1].add(left,mid,tree[node].lazy.back());
tree[z].add(mid + 1,right,tree[node].lazy.back());
tree[node].lazy.pop_back();
}
}
long long query(long long node,long long left,long long right,long long qleft,long long qright){
if (qright<left|| qleft > right)return 0;
if (qleft<=left && qright>=right){
return tree[node].v;
}
push(node,left,right);
long long mid = (left + right)>>1;
long long z = node + ((mid - left + 1)<<1);
long long ans = 0;
if (qleft<=mid){
ans+=query(node + 1,left,mid,qleft,qright);
}
if (qright > mid){
ans+=query(z,mid + 1,right,qleft,qright);
}
return ans;
}
void update(long long node,long long left,long long right,long long uleft,long long uright,long long v,int type){
if (left > uright || right < uleft) return;
if (uleft <= left && right <=uright){
tree[node].add(left,right,make_pair(v,type));
return;
}
push(node,left,right);
long long mid = (left + right)>>1;
long long z = node + ((mid - left + 1)<<1);
if (uleft<=mid){
update(node + 1,left,mid,uleft,uright,v,type);
}
if (uright > mid){
update(z,mid + 1,right,uleft,uright,v,type);
}
tree[node] = combine(tree[node + 1],tree[z]);
}
long long query(int qleft,int& qright){
return query(0,0,m,qleft,qright);
}
void update(int uleft,int uright,long long v,int type){
update(0,0,m,uleft,uright,v,type);
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,m;cin>>n>>m;
vector<vector<int>>arr(n);
for (int i = 0;i<m;++i){
int x;cin>>x;
arr[x - 1].push_back(i);
}
vector<long long>brr(n);
for (int i = 0;i<n;++i)cin>>brr[i];
int k;cin>>k;
vector<node>edge(k);
for (int i = 0;i<k;++i){
cin>>edge[i].l>>edge[i].r>>edge[i].v;
edge[i].l--;
edge[i].r--;
}
queue<node>q;
vector<int>l(n),r(n);
for (int i = 0;i<n;++i){
l[i] = 0,r[i] = k;
}
vector<int>ans(n,-1);
bool changed = true;
vector<vector<int>>need(k + 1);
for (int i = 0;i<n;++i){
int mid = (l[i] + r[i])>>1;
if (l[i] == r[i]){
ans[i] = l[i];
}
else{
need[mid].push_back(i);
}
}
Segment_Tree st;
st.init(m);
while(changed){
changed = false;
st.update(0,m - 1,0,2);
for (int i = 0;i<k;++i){
if (edge[i].l<=edge[i].r){
st.update(edge[i].l,edge[i].r,edge[i].v,1);
}
else{
st.update(edge[i].l,m - 1,edge[i].v,1);
st.update(0,edge[i].r,edge[i].v,1);
}
while(!need[i].empty()){
int x = need[i].back();
need[i].pop_back();
changed = true;
long long sum = 0;
for (auto y:arr[x]){
sum+=st.query(y,y);
if (sum >= brr[x])break;
}
if (sum < brr[x]){
l[x] = i + 1;
}
else{
r[x] = i;
}
if (l[x] == r[x]){
ans[x] = l[x];
}
else{
need[(l[x] + r[x])>>1].push_back(x);
}
}
}
}
for (auto x:ans){
x++;
if (x > k)cout<<"NIE\n";
else cout<<x<<'\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
468 KB |
Output is correct |
2 |
Correct |
10 ms |
548 KB |
Output is correct |
3 |
Correct |
13 ms |
516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
468 KB |
Output is correct |
2 |
Correct |
10 ms |
524 KB |
Output is correct |
3 |
Correct |
12 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1524 ms |
9628 KB |
Output is correct |
2 |
Correct |
1780 ms |
12288 KB |
Output is correct |
3 |
Correct |
1830 ms |
11152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1757 ms |
10408 KB |
Output is correct |
2 |
Correct |
2170 ms |
10460 KB |
Output is correct |
3 |
Correct |
2286 ms |
12488 KB |
Output is correct |
4 |
Correct |
181 ms |
8576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
454 ms |
10172 KB |
Output is correct |
2 |
Correct |
615 ms |
11960 KB |
Output is correct |
3 |
Correct |
558 ms |
2900 KB |
Output is correct |
4 |
Correct |
1789 ms |
11752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1869 ms |
9284 KB |
Output is correct |
2 |
Correct |
1451 ms |
10856 KB |
Output is correct |
3 |
Correct |
1604 ms |
9292 KB |
Output is correct |
4 |
Correct |
2140 ms |
13164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4273 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6071 ms |
64300 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |