just a hook 线段树区间更新
L - Just a Hook
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.
Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.
Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
Output
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
Sample Input
1
10
2
1 5 2
5 9 3
Sample Output
Case 1: The total value of the hook is 24.
#include<iostream> #include<cstdio> using namespace std; const int N = 1e5 + 5; int f[N<<2]; int lazy[N<<2]; void pushdown(int root, int mid) { int rt=root<<1; int left=mid>>1; if (lazy[root]){ lazy[rt ] = lazy[root]; lazy[rt+1] = lazy[root]; f[rt] = lazy[root]*(mid-left); f[rt+1] = lazy[root]*(mid >> 1); lazy[root] = 0; } } void build(int left, int right, int root){ lazy[root] = 0; if (left == right){ f[root] = 1; return; } int mid = (left + right) >> 1; int rt=root<<1; build(left, mid, rt); build(mid+ 1, right, rt+1); f[root] = f[rt] + f[rt+1]; } void update(int uleft,int uright, int x, int left, int right, int root) { if (uleft<= left && right<=uright) { lazy[root] = x; f[root]= (int)x*(right - left + 1); return; } pushdown(root,right-left+1); int mid = (left + right) >> 1; int rt=root<<1; if (uleft <= mid) { update(uleft, uright, x,left,mid,rt); } if (uright > mid) { update(uleft, uright, x , mid+1,right,rt+1); } f[root] = f[rt] + f[rt +1]; } int main() { int t; int a, b, c; int k = 1; scanf("%d",&t); while (t--) { int n, m; scanf("%d%d",&n,&m); build(1, n, 1); while (m--) { scanf("%d%d%d",&a,&b,&c); update(a, b, c, 1, n, 1); } printf("Case %d: The total value of the hook is %d.\n", k++, f[1]); } return 0; } //Memory: 3436 KB Time: 639 MS //Language: G++ Result: Accepted
分类:
线段树
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现