CF gym 102920 I Stock Analysis 解题报告
CF gym 102920 I
给出一个序列和若干询问,每次求
[l,r] 的子区间中不超过w 的最大区间和。
n\leq 2000,Q\leq 200000
Sol:
首先这个范围就不是很数据结构,这个询问反倒很数据结构。
我们可以发现,其实一共最多只有
我们考虑对于每个位置
然后我们发现实际上是没有
struct BIT{
ll p[N][N];
void init() {
for (int i = 1;i <= n;++i) {
for (int j = 0;j <= n;++j) {
p[i][j] = -INF;
}
}
}
int lowbit(int x) {return x & (-x);}
void modify(int x,int y,ll w) {
for (;y <= n;y += lowbit(y)) {
p[x][y] = max(p[x][y],w);
}
}
ll query(int x,int y) {
ll ans = -INF;
for (;y;y -= lowbit(y)) {
ans = max(ans,p[x][y]);
}
return ans;
}
};
然后这样其实就可以通过了。每次的时间复杂度是
但是我们怎么能止步于此呢!!!!(其实是算复杂度的时候人算傻了)
我们考虑!对序列进行分块,然后再额外开一个树状数组维护每一块内以其中某个点为左端点的子区间的最大值。
每次询问的时候,
时间复杂度
Code:
直接树状数组啥事没有
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
#define int long long
#define ll long long
#define ri register int
const int N = 4010;
const int M = 4e6 + 10;
const ll INF = 1e17;
int n,m;
ll p[N][N];
int lowbit(int x) {return x & (-x);}
void modify(int x, int y, long long v){
for (int i = x; i < N; i += lowbit(i)){
for (int j = y; j < N; j += lowbit(j)){
p[i][j] = max(p[i][j], v);
}
}
}
long long query(int x, int y){
long long v = -1e18;
for (int i = x; i > 0; i -= lowbit(i)){
for (int j = y; j > 0; j -= lowbit(j)){
v = max(v, p[i][j]);
}
}
return v;
}
ll ans[M],st[N],prem;
struct memb {
int l,r,pos;
ll w;
friend inline bool operator <(const memb &a,const memb &b) {
return a.w == b.w ? a.pos < b.pos : a.w < b.w;
}
}q[M];
std::vector<pair<pair<ll,int>,pair<int,int> > > v;
ll a[N];
signed main() {
ios::sync_with_stdio(false);
for (int i = 0;i < N;++i) {
for (int j = 0;j < N;++j) {
p[i][j] = -INF;
}
}
cin >> n >> m;
for (int i = 1;i <= n;++i) cin >> a[i];
for (int i = 1;i <= m;++i) {
int x,y;ll w;
cin >> x >> y >> w;
v.push_back(make_pair(make_pair(w,i),make_pair(x,y)));
}
for (int i = 1;i <= n;++i) {
ll tmp = 0;
for (int j = i;j <= n;++j) {
tmp += a[j];
v.push_back(make_pair(make_pair(tmp,-1),make_pair(i,j)));
}
}
sort(v.begin(),v.end());
for (auto p:v) {
//cout << q[i].l<< " " << q[i].r<< " "<< q[i].pos << " " << q[i].w << endl;
if (p.first.second != -1) ans[p.first.second] = query(n - p.second.first + 1,p.second.second);
else modify(n - p.second.first + 1,p.second.second,p.first.first);
}
for (int i = 1;i <= m;++i) {
if (ans[i] != -INF) cout << ans[i] << endl;
else cout << "NONE" << endl;
}
return 0;
}
啥比才写分块
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
#define int long long
#define ll long long
#define ri register int
char buf[1 << 20], *p1, *p2;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)?EOF: *p1++)
template <typename T> inline void read(T &t) {
ri v = getchar();T f = 1;t = 0;
while (!isdigit(v)) {if (v == '-')f = -1;v = getchar();}
while (isdigit(v)) {t = t * 10 + v - 48;v = getchar();}
t *= f;
}
template <typename T,typename... Args> inline void read(T &t,Args&... args) {
read(t);read(args...);
}
const int N = 4010;
const int M = 4e6 + 10;
const ll INF = 1e17;
int n,m;
ll p[N][N];
void init() {
for (int i = 0;i < N;++i) {
for (int j = 0;j < N;++j) {
p[i][j] = -INF;
}
}
}
int lowbit(int x) {return x & (-x);}
void modify(int x, int y, long long v){
for (int i = x; i < N; i += lowbit(i)){
for (int j = y; j < N; j += lowbit(j)){
p[i][j] = max(p[i][j], v);
}
}
}
long long query(int x, int y){
long long v = -1e18;
for (int i = x; i > 0; i -= lowbit(i)){
for (int j = y; j > 0; j -= lowbit(j)){
v = max(v, p[i][j]);
}
}
return v;
}
ll ans[M],st[N],prem;
struct memb {
int l,r,pos;
ll w;
friend inline bool operator <(const memb &a,const memb &b) {
return a.w == b.w ? a.pos < b.pos : a.w < b.w;
}
}q[M];
ll a[N];
signed main() {
read(n,m);
for (int i = 1;i <= n;++i) read(a[i]);
for (int i = 1;i <= m;++i) {
read(q[i].l,q[i].r);read(q[i].w);q[i].pos = i;
}
prem = m;
init();
for (int i = 1;i <= n;++i) {
ll tmp = 0;
for (int j = i;j <= n;++j) {
tmp += a[j];
q[++m].l = i;q[m].r = j;q[m].w = tmp;q[m].pos = -1;
}
}
sort(q+1,q+1+m);
for (int i = 1;i <= m;++i) {
//cout << q[i].l<< " " << q[i].r<< " "<< q[i].pos << " " << q[i].w << endl;
if (q[i].pos != -1) ans[q[i].pos] = query(n - q[i].l + 1,q[i].r);
else modify(n - q[i].l + 1,q[i].r,q[i].w);
}
for (int i = 1;i <= prem;++i) {
if (ans[i] != -INF) cout << ans[i] << endl;
else puts("NONE");
}
return 0;
}
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭