HDU1698(线段树)
来源:互联网 发布:python 有抽象类吗? 编辑:程序博客网 时间:2024/05/29 14:23
roblem 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.
线段树裸区间更新。
#include "cstring"#include "cstdio"#include "iostream"#include "string.h"#define MAXSIZE 200005using namespace std;typedef struct{ int mark; int left, right;}NODE;NODE tree[MAXSIZE * 4];int build(int root,int lf,int rt){ int mid; tree[root].left = lf; tree[root].right = rt; if (lf == rt) return tree[root].mark = 1; mid = (lf + rt) / 2; int a, b; a = build(2 * root, lf, mid); b = build(2 * root + 1, mid + 1, rt); return tree[root].mark = 1;}void insert(int root,int x,int y,int z){ if(tree[root].mark==z) return; if(tree[root].left==x&&tree[root].right==y) { tree[root].mark=z; return; } if(tree[root].mark!=-1) { tree[root*2].mark=tree[root*2+1].mark=tree[root].mark; tree[root].mark=-1; } int mid=(tree[root].left+tree[root].right)/2; if(x>mid) insert(root*2+1, x, y, z); else if(y<=mid) insert(root*2,x,y,z); else { insert(root*2,x,mid,z); insert(root*2+1,mid+1,y,z); }}int sum=0;void cnt(int root){ if(tree[root].mark!=-1) { sum+=tree[root].mark*(tree[root].right-tree[root].left+1); return; } else { cnt(root*2); cnt(root*2+1); }} int main(){ int cas; scanf("%d",&cas); for(int i=1;i<=cas;i++) { sum=0; int n; scanf("%d",&n); build(1,1,n); int q; scanf("%d",&q); while(q--) { int x,y,z; scanf("%d%d%d",&x,&y,&z); insert(1,x,y,z); } cnt(1); printf("Case %d: The total value of the hook is %d.\n",i,sum); } return 0;}
- HDU1698(线段树)
- HDU1698(线段树)练习题
- Just a Hook(hdu1698,线段树)
- hdu1698(线段树,区间着色)
- hdu1698 Just a Hook(线段树)
- hdu1698 Just a Hook(线段树)
- hdu1698线段树(区间更新)
- HDU1698:Just a Hook(线段树)
- hdu1698 Just a Hook (线段树)
- hdu1698(线段树)
- hdu1698之线段树
- hdu1698线段树
- HDU1698 线段树
- hdu1698线段树
- HDU1698(线段树LAZY)
- HDU1698 线段树
- HDU1698线段树区间更新
- 线段树区间修改hdu1698
- ios 打开设置页面
- 一个千万量级的APP使用的一些第三方库
- 【分享】深入挖掘document.getElementsByTagName()方法的返回值
- mysql 5.7 安装手册(for linux)
- extjs6 基础组件扩展和自定义组件封装--04静态js方法的设计思想
- HDU1698(线段树)
- 配置Windows 防火墙,允许SQL Server的远程连接
- Zookeeper作用
- Python函数第一节
- Js代码用<script>调用时,放在<head>与<body>的区别
- Oracle集成云服务Integration Cloud Service
- 欢迎使用CSDN-markdown编辑器
- hdu5540 Secrete Master Plan--矩阵旋转
- Android Studio Monitor no debuggable applications