#include <bits/stdc++.h>
#define ll long long
#define SZ(X) (int)(X.size())
#define MAX 300005
#define pii pair <int,ll>
#define ind first
#define val second
#define INF (ll)(1e18)
using namespace std;
struct Query{
int le, ri, amo;
};
int typeNum, len, queNum;
int arr[MAX];
int ans[MAX];
vector <int> sta[MAX]; // stations of every type
vector <Query> allQ;
vector <pii> allT;
int a, b, c;
vector <pii> loc; // considered locations and dif
vector <pii> tCopy; // copy of type
void solve(int le, int ri, vector <pii> & type){ // type: station types and how much is needed, le and ri of queries
//cout << le << " " << ri << " " << SZ(type) << "\n";
loc.clear();
tCopy.clear();
if (type.empty()) return;
for (pii t : type){
for (int s : sta[t.ind])
loc.push_back({s, 0});
tCopy.push_back(t);
}
sort(loc.begin(), loc.end());
if (loc.empty()) return;
int mid = (le+ri)/2;
for (int i = le; i <= mid; i++){ // set up prefix difference
Query q = allQ[i];
int add = q.le, sub = q.ri;
if (q.le > q.ri) { // ring-shaped array
loc[0].val += q.amo;
}
auto it = lower_bound(loc.begin(), loc.end(), make_pair(add, -INF));
if (it != loc.end()) it->val += q.amo;
it = lower_bound(loc.begin(), loc.end(), make_pair(sub+1, -INF));
if (it != loc.end()) it->val -= q.amo;
}
ll sum = 0;
for (pii l : loc){ // use prefix difference to calculate how much more we need for each type
sum += l.val;
auto it = lower_bound(tCopy.begin(), tCopy.end(), make_pair(arr[l.ind], -INF));
it->val -= sum;
//cout << sum << " ";
}
if (le == ri){ // this is the final step
for (pii t : tCopy){
if (t.val <= 0) ans[t.ind] = le+1;
}
return;
}
vector <pii> typeA, typeB;
for (int i = 0; i < SZ(type); i++){
if (tCopy[i].val <= 0) typeA.push_back(type[i]);
else typeB.push_back(tCopy[i]);
}
solve(le, mid, typeA);
solve(mid+1, ri, typeB);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> typeNum >> len;
for (int i = 1; i <= len; i++) {
cin >> arr[i];
sta[arr[i]].push_back(i);
}
for (int i = 1; i <= typeNum; i++){
cin >> a;
allT.push_back({i, a});
}
cin >> queNum;
for (int i = 0; i < queNum; i++){
cin >> a >> b >> c;
allQ.push_back({a, b, c});
}
solve(0, queNum-1, allT);
for (int i = 1; i <= typeNum; i++){
if (ans[i] == 0) cout << "NIE\n";
else cout << ans[i] << "\n";
}
return 0;
}
/*
10 11
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10
10
1 3 1
3 4 1
7 1 1
1 5 1
1 5 1
6 6 1
7 7 1
8 8 1
9 9 1
10 10 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
5 ms |
7512 KB |
Output is correct |
3 |
Correct |
5 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7432 KB |
Output is correct |
2 |
Correct |
5 ms |
7508 KB |
Output is correct |
3 |
Correct |
5 ms |
7512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
10260 KB |
Output is correct |
2 |
Correct |
163 ms |
17344 KB |
Output is correct |
3 |
Correct |
128 ms |
10948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
10612 KB |
Output is correct |
2 |
Correct |
100 ms |
10680 KB |
Output is correct |
3 |
Correct |
151 ms |
14592 KB |
Output is correct |
4 |
Correct |
48 ms |
11712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
10496 KB |
Output is correct |
2 |
Correct |
77 ms |
15532 KB |
Output is correct |
3 |
Correct |
21 ms |
8512 KB |
Output is correct |
4 |
Correct |
101 ms |
11500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
10236 KB |
Output is correct |
2 |
Correct |
103 ms |
10752 KB |
Output is correct |
3 |
Correct |
60 ms |
10248 KB |
Output is correct |
4 |
Correct |
112 ms |
13156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1238 ms |
44932 KB |
Output is correct |
2 |
Runtime error |
122 ms |
44848 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1132 ms |
38672 KB |
Output is correct |
2 |
Incorrect |
243 ms |
25784 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |