HDU1698 线段树
来源:互联网 发布:小米电视3怎么样 知乎 编辑:程序博客网 时间:2024/06/15 22:08
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.
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
11021 5 25 9 3
Sample Output
Case 1: The total value of the hook is 24.
Source
2008 “Sunline Cup” National Invitational Contest
线段树模板系列.....
WA了好久,错在多组数据flag得重新变为0上...
不多说,贴代码:
线段树模板系列.....
WA了好久,错在多组数据flag得重新变为0上...
不多说,贴代码:
#include <iostream>#include<stdio.h>using namespace std;const int N=1e6+50;struct node{ int left,right; int weight; int flag;}tree[4*N];void build(int k,int ll,int rr,int x) //非输入建树,初始化为x{ tree[k].left=ll; tree[k].right=rr; tree[k].flag=0; //******* if(tree[k].left==tree[k].right) { tree[k].weight=x; //*** return ; } int mid=(ll+rr)/2; build(2*k,ll,mid,x); //*** build(2*k+1,mid+1,rr,x); //*** tree[k].weight=tree[2*k].weight+tree[2*k+1].weight;}void down(int k){ tree[2*k].flag=tree[k].flag; tree[2*k+1].flag=tree[k].flag; tree[2*k].weight=tree[k].flag*(tree[2*k].right-tree[2*k].left+1); tree[2*k+1].weight=tree[k].flag*(tree[2*k+1].right-tree[2*k+1].left+1); tree[k].flag=0;}void change_interval(int k,int a,int b,int y) //改为y{ if(tree[k].left>=a&&tree[k].right<=b) { tree[k].weight=(tree[k].right-tree[k].left+1)*y; tree[k].flag=y; return ; } if(tree[k].flag) down(k); int mid=(tree[k].left+tree[k].right)/2; if(a<=mid) change_interval(2*k,a,b,y); if(b>mid) change_interval(2*k+1,a,b,y); tree[k].weight=tree[2*k].weight+tree[2*k+1].weight;}int main(){ int t,n,q; scanf("%d",&t); for(int i=1;i<=t;i++) { scanf("%d%d",&n,&q); build(1,1,n,1); int u,v,w; while(q--) { scanf("%d%d%d",&u,&v,&w); change_interval(1,u,v,w); } printf("Case %d: The total value of the hook is %d.\n",i,tree[1].weight); } return 0;}
阅读全文
0 0
- hdu1698(线段树)
- hdu1698之线段树
- hdu1698线段树
- HDU1698 线段树
- HDU1698(线段树)
- hdu1698线段树
- HDU1698(线段树LAZY)
- HDU1698 线段树
- HDU1698线段树区间更新
- 线段树区间修改hdu1698
- hdu1698 线段树区间更新
- hdu1698 线段树 区间更新
- HDU1698线段树区间更新
- hdu1698(线段树区间更新)
- 线段树区间更新hdu1698
- hdu1698 线段树区间更新
- HDU1698(线段树+延迟标记)
- HDU1698 线段树 区间更新
- 自己的开发,请大家多多支持
- 设计模式(Design Pattern)
- svn搭建
- web.xml拼写引发的404
- Python——range()、xrange()和np.arange()应用说明
- HDU1698 线段树
- PTA的6-10 二叉树的遍历
- 如何避免团队协作中的无效沟通
- 最大上升、下降、非上升、非下降子序列 -- n²与nlogn
- PullToRefreshListView的布局文件
- CentOS 6.4安装配置LAMP服务器(Apache+PHP5+MySQL)
- 传递、返回复杂类型的对象
- StringBuilder,ArrayList等常用数据结构扩容大小
- win10下Anaconda3配置环境变量