This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
// #define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int N = 3e5 + 5;
vector<long long> bit(N);
int n, m;
void Update(int pos, long long val)
{
for(int i = pos; i > 0; i -= (i & (-i)))
{
bit[i] += val;
}
}
long long Query(int pos)
{
if(pos == 0) return 0;
long long ans = 0;
for(int i = pos; i < m + 5; i += (i & (-i)))
{
ans += bit[i];
}
return ans;
}
void Update(int l, int r, int val)
{
if(r < l)
{
Update(r, val);
// cout << "Update(" << r << ", " << val << ")\n";
r = m;
}
// cout << "Update(" << r << ", " << val << ")\n";
Update(r, val);
// cout << "Update(" << l - 1 << ", " << -val << ")\n";
Update(l - 1, -val);
}
void Solve()
{
cin >> n >> m;
bit.resize(m + 5);
vector<int> sector[n + 1];
vector<int> requirements(n + 1);
for(int i = 1; i <= m; i++)
{
int x;
cin >> x;
// cout << "x = " << x << ", i = " << i << "\n";
sector[x].push_back(i);
}
for(int i = 1; i <= n; i++)
{
bit[i] = 0;
cin >> requirements[i];
}
bit[0] = 0;
int k;
cin >> k;
vector<array<int, 3> > queries(k + 1);
vector<array<int, 3> > ranges(n + 1);
vector<int> middle[k + 1];
for(int i = 1; i <= k; i++)
{
cin >> queries[i][0] >> queries[i][1] >> queries[i][2];
}
for(int i = 1; i <= n; i++)
{
ranges[i] = {1, k, k + 1};
}
while(true)
{
int cnt = 0;
for(int i = 0; i <= k; i++)
{
middle[i].clear();
}
for(int i = 0; i < m + 5; i++)
{
bit[i] = 0;
}
for(int i = 1; i <= n; i++)
{
if(ranges[i][0] > ranges[i][1])
{
cnt++;
}
else
{
int mid = (ranges[i][0] + ranges[i][1]) / 2;
// cout << "i = " << i << ", ranges[i][0] = " << ranges[i][0] << ", ranges[i][1] = " << ranges[i][1] << ", mid = " << mid << endl;
middle[mid].push_back(i);
}
}
if(cnt == n)
{
break;
}
for(int i = 1; i <= k; i++)
{
Update(queries[i][0], queries[i][1], queries[i][2]);
// for(int j = 1; j <= m; j++)
// {
// cout << Query(j) << " ";
// }
// cout << "\n";
for(int j: middle[i])
{
int f = 0;
long long gathered = 0;
for(int cur: sector[j])
{
gathered += Query(cur);
// cout << "Query(cur) = " << Query(cur) << "\n";
// cout << "position = " << cur << ", gathered = " << gathered << "\n";
if(gathered >= requirements[j])
{
ranges[j][1] = i - 1;
ranges[j][2] = i;
f = 1;
break;
}
}
// cout << "gathered = " << gathered << ", query number = " << i << ", organization number = " << j << "\n";
if(f == 0)
{
ranges[j][0] = i + 1;
}
}
}
}
for(int i = 1; i <= n; i++)
{
if(ranges[i][2] == k + 1)
{
cout << "NIE\n";
}
else
{
cout << ranges[i][2] << "\n";
}
}
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |