【HDU1698】【线段树成段更新】
2015-08-26 19:26
381 查看
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 23190 Accepted Submission(s): 11624
Problem Description
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.
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.
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.
Source
2008 “Sunline Cup” National Invitational Contest
Recommend
wangye
Statistic | Submit | Discuss | Note
#include <iostream> #include <cstring> #include <cmath> #include <queue> #include <stack> #include <list> #include <map> #include <string> #include <cstdlib> #include <cstdio> #include <algorithm> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define mp push_back #define lson l,m,rt<<1 #define rson m,r,rt<<1|1 int T; int n,m; const int MAXN = 100010; int sum[MAXN*4]; int set[MAXN*4]; void PushUp(int rt) { sum[rt] = sum[rt<<1] + sum[rt<<1|1]; } void PushDown(int rt,int l1,int l2) { if(set[rt] != -1) { set[rt<<1] = set[rt]; sum[rt<<1] = set[rt] * l1; set[rt<<1|1] = set[rt]; sum[rt<<1|1] = set[rt] * l2; set[rt] = -1; } } void build(int l,int r,int rt) { set[rt] = -1; if(r - l == 1) { sum[rt] = 1; return; } int m = (l + r) >> 1; build(lson); build(rson); PushUp(rt); } void update(int L,int R,int v,int l,int r,int rt) { if(L <= l && r <= R) { set[rt] = v; sum[rt] = (r - l) * v; return; } int m = (l + r) >> 1; PushDown(rt,m-l,r-m); if(L <= m-1) update(L,R,v,lson); if(m <= R-1) update(L,R,v,rson); PushUp(rt); } int main() { freopen("in.txt","r",stdin); scanf("%d",&T); int C = 1; while(T --) { scanf("%d",&n); build(1,n+1,1); scanf("%d",&m); for(int i=0;i<m;i++) { int l,r,v; scanf("%d%d%d",&l,&r,&v); update(l,r+1,v,1,n+1,1); } printf("Case %d: The total value of the hook is %d.\n",C++,sum[1]); } }
相关文章推荐
- 安装 HoldemManager2 的一点经历
- 第五周第三天
- hdu 1272 小希的迷宫(并查集)
- bzoj-1194 潘多拉的盒子
- SqlBulkCopy-从 bcp 客户端收到一个对 colid 1 无效的列长度
- [React] React Fundamentals: JSX Deep Dive
- Cocos2dx学习笔记1:创建游戏项目解决方案
- Spark-1.4.0单机部署(Hadoop-2.6.0采用伪分布式)【已测】
- Product of Array Except Self
- A,B两个整数集合,设计一个算法求他们的交集,尽可能的高效(牛客网)
- c/c++ 取得当前目录
- Ajax提高篇(2)DOM进行Web响应
- WebViewer报错Error loading document: Invalid XOD file: Zip end header data is wrong size!
- [POJ2503]Babelfish
- oracle ORA-12560: TNS: 协议适配器错误的问题
- ubutun安装unoconv转换office文件为PDF
- C语言基础知识之(八):二维数组,二维字符串数组,代码安全
- Python多进程编程
- android自动化工具之--Monkeyrunner使用方法及实例
- C++中的error C2662,const的this指针问题