結果
| 問題 |
No.1496 不思議な数え上げ
|
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2021-05-11 23:23:44 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 430 ms / 3,000 ms |
| コード長 | 3,827 bytes |
| コンパイル時間 | 1,406 ms |
| コンパイル使用メモリ | 119,244 KB |
| 最終ジャッジ日時 | 2025-01-21 10:09:40 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 39 |
ソースコード
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
typedef long long ll;
template <typename T>
struct segtree{
int n;
T UNIT;
vector<T> dat;
T (*calc)(T, T);
segtree(int n_, T unit, T (*_calc)(T, T)){
UNIT = unit;
calc = _calc;
n = 1;
while(n < n_) n *= 2;
dat = vector<T>(2*n);
for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
}
void insert(int k, T a){
dat[k+n-1] = a;
}
void update_all(){
for(int i = n-2; i >= 0; i--){
dat[i] = calc(dat[i*2+1], dat[i*2+2]);
}
}
//k番目の値(0-indexed)をaに変更
void update(int k, T a){
k += n-1;
dat[k] = a;
while(k > 0){
k = (k-1)/2;
dat[k] = calc(dat[k*2+1], dat[k*2+2]);
}
}
//[a, b)
//区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
T query(int a, int b, int k=0, int l=0, int r=-1){
if(r < 0) r = n;
if(r <= a || b <= l) return UNIT;
if(a <= l && r <= b) return dat[k];
else{
T vl = query(a, b, k*2+1, l, (l+r)/2);
T vr = query(a, b, k*2+2, (l+r)/2, r);
return calc(vl, vr);
}
}
};
int n;
vector<int> p;
vector<ll> p_idx;
vector<ll> p_cumsum;
vector<ll> a;
vector<ll> ans;
inline ll sect_sum(int l, int r){
return p_cumsum[r]-p_cumsum[l];
}
void input(){
cin >> n;
p.resize(n);
p_idx.resize(n+1);
p_cumsum.resize(n+1);
a.resize(n+1);
ans.resize(n+1);
for(int i = 0; i < n; i++) {
cin >> p[i];
p_idx[p[i]] = i;
p_cumsum[i+1] = p_cumsum[i]+p[i];
}
for(int i = 1; i <= n; i++) cin >> a[i];
}
void solve(int l, int r, segtree<int> &sgt){
if(l == r) return;
if(r == l+1){
int q = p[l];
if(q <= a[q]) ans[q] = 1;
return;
}
int q = sgt.query(l, r);
int c = p_idx[q];
solve(l, c, sgt);
solve(c+1, r, sgt);
if(c-l < r-c){
for(int i = l; i <= c; i++){
if(sect_sum(i, c+1) > a[q]) continue;
if(sect_sum(i, r) <= a[q]){
ans[q] += r-c;
}else{
int s = c+1, t = r;
while(t-s > 1){
int x = (s+t)/2;
if(sect_sum(i, x) > a[q]) t = x;
else s = x;
}
ans[q] += s-c;
}
}
}else{
for(int i = c; i < r; i++){
if(sect_sum(c, i+1) > a[q]) continue;
if(sect_sum(l, i+1) <= a[q]){
ans[q] += c-l+1;
}else{
int s = l, t = c;
while(t-s > 1){
int x = (s+t)/2;
if(sect_sum(x, i+1) > a[q]) s = x;
else t = x;
}
ans[q] += c-s;
}
}
}
}
const int INF = 1e9;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
input();
segtree<int> sgt(n, INF, [](int a, int b){ return min(a, b); });
for(int i = 0; i < n; i++) sgt.update(i, p[i]);
solve(0, n, sgt);
for(int i = 1; i <= n; i++) cout << ans[i] << endl;
}
milanis48663220