HDU1698(线段树入门题)
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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
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
Sample Input
Sample Output
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
struct Node{
int l,r;
int lazy,sum;
}a[MAXN*]; void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].lazy=;
if(l==r){
a[rt].sum=;
return ;
}
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
a[rt].sum=a[rt<<].sum+a[(rt<<)|].sum;
} void PushDown(int rt)
{
int mid=(a[rt].l+a[rt].r)>>;
a[rt<<].lazy=a[(rt<<)|].lazy=a[rt].lazy;
a[rt<<].sum=a[rt].lazy*(mid-a[rt].l+);
a[(rt<<)|].sum=a[rt].lazy*(a[rt].r-mid);
a[rt].lazy=;
} void update(int rt,int l,int r,int val)
{
if(a[rt].l==l&&a[rt].r==r)
{
a[rt].lazy=val;
a[rt].sum=val*(r-l+);
return ;
} if(a[rt].lazy) PushDown(rt); int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid) update(rt<<,l,r,val);
else if(mid<l) update((rt<<)|,l,r,val);
else{
update(rt<<,l,mid,val);
update((rt<<)|,mid+,r,val);
}
a[rt].sum=a[rt<<].sum+a[(rt<<)|].sum;
} int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
int n;
scanf("%d",&n);
build(,,n);
int m;
scanf("%d",&m);
while(m--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
update(,x,y,z);
}
printf("Case %d: The total value of the hook is %d.\n",cas,a[].sum);
}
return ;
}
最新文章
- linux 文件系统sysvinit 流程分析
- nullcon HackIM2016 -- Programming Question 3
- Java之内存分析和String对象
- DOS下无法调出中文输入法-Solved
- java基础笔记
- Kobject结构体分析
- 【转】lua Date和Time
- 游戏对象的变换-Transform
- Ubuntu 12.04搭建MTK 6577 安卓开发环境
- JQuery日记_5.13 Sizzle选择器(六)选择器的效率
- 用tp框架来对数据库进行增删改
- yum升级mysql
- kibana使用
- redis list命令操作
- 启动bind失败
- hdu1576-A/B-(同余定理+乘法逆元+费马小定理+快速幂)
- Hadoop 5 Hbase 遇到的问题
- 配置Tomcat apr运行模式
- MySQL5.7.10配置和使用
- Spring界的HelloWorld