이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define dbg(x) "[" #x " = " << (x) << "]"
#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}
const int N = 3e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;
int n, q, d, a[N], diff[N];
pair<int,int> Q[N];
namespace Subtask1{
void process()
{
for(int cur_query = 1; cur_query <= q; cur_query++){
int pos_l = Q[cur_query].first, pos_r = Q[cur_query].second;
int answer = 0; bool can = true;
stack<pair<int,int>> save;
save.push(make_pair(a[pos_l]/d, pos_l));
for(int i = pos_l+1; i <= pos_r; i++){
if(diff[i] >= 0){
save.push(make_pair(diff[i]/d, i));
}
else{
int need = (-diff[i] + d-1) / d;
while(!save.empty() && need){
pair<int,int> e = save.top(); save.pop();
int oper = e.first, l = e.second;
int op_done = min(oper, need);
need -= op_done, oper -= op_done;
answer = answer + op_done * (i-l);
if(oper) save.push(make_pair(oper, l));
}
if(need){
can = false;
break;
}
}
}
cout << (can ? answer : -1) <<"\n";
}
}
}
namespace Subtask5{
struct FWT{
vector<ll> bit;
FWT(int n = 0): bit(n+1) {}
void update(int p, int val){
for(; p < sz(bit); p += (p&(-p))) bit[p] += 1ll * val;
return;
}
ll get(int p){
ll res = 0;
for(; p; p -= (p&(-p))) res += bit[p];
return res;
}
ll get_range(int l, int r){
return get(r) - get(l-1);
}
};
ll answer[N];
vector<int> Que[N];
void process()
{
for(int i = 1; i <= q; i++){
int r = Q[i].second;
Que[r].push_back(i);
}
stack<pair<int,int>> save;
FWT cost(n), operate(n);
for(int i = 1; i <= n; i++){
if(i > 1){
if(diff[i] >= 0){
save.push(make_pair(diff[i]/d, i));
}
else{
int need = (-diff[i] + d-1) / d;
cost.update(i, need*i);
operate.update(i, -need);
while(!save.empty() && need){
pair<int,int> e = save.top(); save.pop();
int rem_op = e.first, pos = e.second;
int op_done = min(rem_op, need);
need -= op_done;
rem_op -= op_done;
cost.update(pos, -op_done*pos);
operate.update(pos, op_done);
if(rem_op) save.push(make_pair(rem_op, pos));
}
}
}
for(int idQuery : Que[i]){
int l = Q[idQuery].first, r = Q[idQuery].second;
int cur_op = operate.get_range(l+1, r);
if(cur_op < 0){
int can_proc = min(-cur_op, a[l]/d);
cur_op += can_proc;
if(cur_op < 0){
answer[idQuery] = -1;
continue;
}
answer[idQuery] += -can_proc*l;
}
answer[idQuery] += cost.get_range(l+1, r);
}
}
for(int i = 1; i <= q; i++) cout << answer[i] <<"\n";
}
}
void solve()
{
cin >> n >> d;
for(int i = 1; i <= n; i++){
cin >> a[i];
if(i > 1){
diff[i] = a[i] - a[i-1];
}
}
cin >> q;
for(int i = 1; i <= q; i++){
cin >> Q[i].first >> Q[i].second;
}
//Subtask1::process();
Subtask5::process();
return;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:185:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
185 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:186:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
186 | freopen(name".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |