QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#676432 | #6104. Building Bombing | nathan4690 | WA | 1ms | 7592kb | C++17 | 4.1kb | 2024-10-25 21:31:14 | 2024-10-25 21:31:15 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define el cout << '\n'
#define f1(i,n) for(int i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 1e5+5, inf=1e18;
struct Lazy{
long long data = 0;
Lazy(){};
Lazy(long long data): data(data){};
Lazy operator+(const Lazy &rhs) const{
// Push a lazy down
return Lazy(data + rhs.data);
}
};
struct Value{
long long data = 1e18;
Value(){};
Value(long long data): data(data){};
Value operator+(const Value &rhs) const{
// Merge two nodes
return Value(min(data, rhs.data));
}
Value operator+(const Lazy &rhs) const{
// Apply lazy to node
return Value(data + rhs.data);
}
friend ostream& operator<<(ostream& ouf, const Value &rhs){
return ouf << rhs.data;
}
};
template<class T, class U>
struct SegmentTree{
int n;
vector<T> st;
vector<U> lazy;
SegmentTree(){};
SegmentTree(int n): n(n), st(4*n+1), lazy(4*n+1){};
inline void down(int idx){
st[idx*2] = st[idx*2] + lazy[idx];
lazy[idx*2] = lazy[idx*2] + lazy[idx];
st[idx*2+1] = st[idx*2+1] + lazy[idx];
lazy[idx*2+1] = lazy[idx*2+1] + lazy[idx];
lazy[idx] = U();
}
void _upd1(int idx, int l, int r, int i, const T &v){
if(i < l || r < i) return;
if(l == r){
st[idx] = v;
return;
}
down(idx);
int mid = (l + r) / 2;
_upd1(idx*2, l, mid, i, v);
_upd1(idx*2+1, mid+1, r, i, v);
st[idx] = st[idx*2] + st[idx*2+1];
}
inline void updatePoint(int position, const T &value){
_upd1(1,1,n,position,value);
}
void _upd2(int idx, int l, int r, int u, int v, const U &w){
if(v < l || r < u) return;
if(u <= l && r <= v){
st[idx] = st[idx] + w;
lazy[idx] = lazy[idx] + w;
return;
}
down(idx);
int mid = (l + r) / 2;
_upd2(idx*2, l, mid, u, v, w);
_upd2(idx*2+1, mid+1, r, u, v, w);
st[idx] = st[idx*2] + st[idx*2+1];
}
inline void updateRange(int left_, int right_, const U &value){
if(left > right) return;
_upd2(1,1,n,left_, right_, value);
}
T _get(int idx, int l, int r, int u, int v){
if(v < l || r < u) return T();
if(u <= l && r <= v) return st[idx];
down(idx);
int mid = (l + r) / 2;
return _get(idx*2, l, mid, u, v) + _get(idx*2+1, mid+1, r, u, v);
}
inline T getRange(int left_, int right_){
return _get(1,1,n,left_, right_);
}
};
#define SegTree SegmentTree<Value, Lazy>
ll n,l,k,a[maxn],dp[maxn][12];
pair<ll,ll> all[maxn];
SegTree st;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if(fopen(__file_name ".inp", "r")){
freopen(__file_name ".inp","r",stdin);
freopen(__file_name ".out","w",stdout);
}
// code here
cin >> n >> l >> k;
st = SegTree(n+1);
f1(i,n) {
cin >> a[i];
all[i] = {a[i], -i};
}
all[n+1] = {inf, -n-1};
sort(all+1,all+n+2,greater<pair<ll,ll>>());
f1(i,n) dp[i][0] = inf;
// f1(i,k) dp[n+1][i] = inf;
// f1(i,n+1) st.updatePoint(i, Value(0));
// st.updateRange(n-1, n+1, Lazy(1));
f1(j,k){
f1(i,n+1) {
dp[i][j] = inf;
st.updatePoint(i, Value(1e9));
}
f1(i,n+1){
ll idx = -all[i].second;
// cout << idx << ' ' << st.getRange(idx, idx).data;el;
dp[idx][j] = st.getRange(idx, n+1).data - st.getRange(idx, idx).data + (int)(1e9);
st.updateRange(idx, idx, Lazy(-(int)(1e9) + dp[idx][j-1]));
// cout << "! " << idx+1 << ' ' << n+1;el;
st.updateRange(idx+1, n+1, Lazy(1));
// cout << "?? "; f1(w,n+1) cout << st.getRange(w,w) << ' ';el;
}
// f1(i,n) cout << dp[i][j] << ' ';el;
}
if(dp[l][k] > n) cout << -1;
else cout << dp[l][k];
el;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5592kb
input:
7 2 3 10 30 90 40 60 60 80
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 7592kb
input:
3 2 2 30 20 10
output:
-1
result:
ok 1 number(s): "-1"
Test #3:
score: 0
Accepted
time: 1ms
memory: 5624kb
input:
1 1 1 608954134
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3836kb
input:
10 5 3 872218248 517822599 163987167 517822599 458534407 142556631 142556631 458534407 458534407 872218248
output:
-1
result:
ok 1 number(s): "-1"
Test #5:
score: -100
Wrong Answer
time: 0ms
memory: 3508kb
input:
10 4 2 31201623 546478688 709777934 672927273 672927273 709777934 801718395 672927273 926114576 38983342
output:
2
result:
wrong answer 1st numbers differ - expected: '3', found: '2'