P7492 [传智杯 #3 决赛] 序列 Solution

Description

给定长为 n n n 的序列 a = ( a 1 , a 2 , ⋯   , a n ) a=(a_1,a_2,\cdots,a_n) a=(a1,a2,,an),有 m m m 个操作,分以下两种:

  1. query ⁡ ( l , r ) \operatorname{query}(l,r) query(l,r):求 max ⁡ ( 0 , max ⁡ [ u , v ] ∈ [ l , r ] ∑ i = u v a i ) \max(0,\max\limits_{[u,v] \in [l,r]}\sum\limits_{i=u}^v a_i) max(0,[u,v][l,r]maxi=uvai)
  2. modify ⁡ ( l , r , k ) \operatorname{modify}(l,r,k) modify(l,r,k):对于每个 i ∈ [ l , r ] i\in [l,r] i[l,r] 执行 a i ← a i or ⁡ k a_i \leftarrow a_i \operatorname{or} k aiaiork,其中 or ⁡ \operatorname{or} or 为按位或。

Limitations

1 ≤ n , m ≤ 1 0 5 1 \le n, m \le 10^5 1n,m105
∣ a i ∣ , ∣ k ∣ ≤ 2 30 |a_i|,|k| \le 2^{30} ai,k230
1 ≤ l ≤ r ≤ n 1 \le l \le r \le n 1lrn
1 s , 128 MB 1\text{s},128\text{MB} 1s,128MB

Solution

modify ⁡ \operatorname{modify} modify 操作无法打标记,考虑暴力修改,但直接暴力改会 TLE \text{TLE} TLE.
考虑对每个区间维护 and ⁡ \operatorname{and} and 和,当发现区间 and ⁡ \operatorname{and} and or ⁡ \operatorname{or} or k k k 不变,那么继续修改显然不会改变 a i a_i ai,可以不向该区间递归.
剩下的就是 P4513 了.
时间复杂度 O ( n log ⁡ n log ⁡ V ) O(n\log n\log V) O(nlognlogV).

Code

3.17 KB , 5.87 s , 10.4 MB    ( in   total,   C++20   with   O2 ) 3.17\text{KB},5.87\text{s},10.4\text{MB} \; (\texttt{in total, C++20 with O2}) 3.17KB,5.87s,10.4MB(in total, C++20 with O2)
重构过了.

// Problem: P7492 [传智杯 #3 决赛] 序列
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P7492
// Memory Limit: 128 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
using ui128 = unsigned __int128;
using f4 = float;
using f8 = double;
using f16 = long double;

template<class T>
bool chmax(T &a, const T &b){
	if(a < b){ a = b; return true; }
	return false;
}

template<class T>
bool chmin(T &a, const T &b){
	if(a > b){ a = b; return true; }
	return false;
}

namespace seg_tree {
	struct Node {
	    int l, r;
	    i64 lmax, rmax, tsum, sum, andd;
	};
	
	inline void merge(Node& res, const Node& le, const Node& ri) {
		res.lmax = max(le.lmax, le.sum + ri.lmax);
		res.rmax = max(le.rmax + ri.sum, ri.rmax);
		res.tsum = max({le.tsum, ri.tsum, le.rmax + ri.lmax});
		res.sum = le.sum + ri.sum;
		res.andd = le.andd & ri.andd;
	}
	
	inline int ls(int u) { return 2 * u + 1; }
	inline int rs(int u) { return 2 * u + 2; }
	
	struct SegTree {
		vector<Node> tr;
		inline SegTree() {}
		inline SegTree(const vector<i64>& a) {
			const int n = a.size();
			tr.resize(n << 1);
			build(0, 0, n - 1, a);
		}
		
		inline void pushup(int u, int mid) { 
			merge(tr[u], tr[ls(mid)], tr[rs(mid)]); 
		}
		
		inline void build(int u, int l, int r, const vector<i64>& a) {
		    tr[u].l = l;
		    tr[u].r = r;
		    if (l == r) {
		        tr[u].sum = tr[u].andd = a[l];
		        tr[u].lmax = tr[u].rmax = tr[u].tsum = max(a[l], 0LL);
		        return;
		    }
		    const int mid = (l + r) >> 1;
		    build(ls(mid), l, mid, a);
		    build(rs(mid), mid + 1, r, a);
		    pushup(u, mid);
		}
		
		inline void update(int u, int l, int r, i64 val) {
		    if (tr[u].l == tr[u].r) {
		        tr[u].sum |= val;
		        tr[u].andd = tr[u].sum;
		        tr[u].lmax = tr[u].rmax = tr[u].tsum = max(tr[u].sum, 0LL);
		        return;
		    }
		    const int mid = (tr[u].l + tr[u].r) >> 1;
		    if (l <= mid && (tr[ls(mid)].andd | val) != tr[ls(mid)].andd) update(ls(mid), l, r, val);
		    if (r > mid && (tr[rs(mid)].andd | val) != tr[rs(mid)].andd) update(rs(mid), l, r, val);
		    pushup(u, mid);
		}
		
		inline Node query(int u, int l, int r) {
		    if (l <= tr[u].l && tr[u].r <= r) {
		        return tr[u];
		    }
		    const int mid = (tr[u].l + tr[u].r) >> 1;
		    if (r <= mid) return query(ls(mid), l, r);
		    if (l > mid) return query(rs(mid), l, r);
		    Node le = query(ls(mid), l, r), ri = query(rs(mid), l, r), res;
		    merge(res, le, ri);
		    return res;
		}
		
		inline void range_or(int l, int r, i64 x) { update(0, l, r, x); }
		inline i64 range_gss(int l, int r) { return query(0, l, r).tsum; }
	};
}
using seg_tree::SegTree;

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int n, m; scanf("%d %d", &n, &m);
	vector<i64> a(n);
	for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
	
	SegTree sgt(a);
	for (int i = 0, op, l, r, v; i < m; i++) {
	    scanf("%d %d %d", &op, &l, &r), l--, r--;
	    if (op == 1) printf("%lld\n", sgt.range_gss(l, r));
	    else {
	        scanf("%d", &v);
	        sgt.range_or(l, r, v);
	    }
	}
	return 0;
}
### 关于 P6464 #2 决赛 送门 树形动态规划 题解 #### 背景描述 题目涉及 n 座教学楼以及 m 条连接这些教学楼的双向道路,形成了一张无向图。目标是在这张图上执行特定的操作并求得最优解。 #### 解决方案概述 对于此类问题,通常采用树形动态规划 (Tree DP) 方法来解决。该方法适用于具有层次结构的数据集,在本案例中即为由教学楼构成的网络拓扑结构[^1]。 #### 动态规划状态定义 设 `f[u][i]` 表示以节点 u 作为根结点的子树内选择了 i 所能获得的最大价值,则可以建立如下转移方程: ```cpp for each child v of node u { for j from size[v] downto 0 { // 反向枚举防止重复计算 f[u][j + k] = max(f[u][j + k], f[u][j] + f[v][k]); } } ``` 这里假设已知所有孩子节点的状态,并通过上述方式更新父节点的状态。其中 `size[]` 数组记录各棵子树大小以便控制循环边界。 #### 边界条件处理 初始化时需考虑叶子节点的情况,一般设定其初始值为零或其他适当常数,具体取决于实际问题需求。另外还需注意特判仅有单个顶点的情形。 #### 复杂度分析 由于每次访问每条边两次(一次正向一次反向),因此时间复杂度大致为 O(n),这使得算法能够在合理时间内完成较大规模输入实例的运算。 #### 实现细节提示 - 使用邻接表存储图的信息; - 运用深度优先搜索遍历整棵树; - 记录每个节点及其对应子树内的最佳选择情况。 ```cpp #include <bits/stdc++.h> using namespace std; const int N = ...; vector<int> adj[N]; int dp[N]; void dfs(int u, int parent){ for(auto &v : adj[u]){ if(v != parent){ dfs(v, u); // 更新当前节点u的最佳决策 } } } int main(){ cin >> n >> m; while(m--){ int a,b; cin>>a>>b; adj[a].push_back(b); adj[b].push_back(a); } memset(dp, 0, sizeof(dp)); dfs(1,-1); // 假定第一个节点为根 cout << "最终结果:" << endl; return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值