HDU 1698 Just A Hook(线段树修改区间到某值)
来源:互联网 发布:三维软件是什么 编辑:程序博客网 时间:2024/06/05 22:29
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.
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.
11021 5 25 9 3
Case 1: The total value of the hook is 24.
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <cmath>#define maxn 11111using namespace std;typedef long long ll;ll num[maxn];ll segtree[maxn*30];ll add[maxn*30];void init(int n){ for(int i=1;i<=n;i++) { segtree[i]=0; }}void PushDown(int rt,int l){ if(add[rt]!=0) { add[rt<<1]=add[rt]; add[rt<<1|1]=add[rt]; segtree[rt<<1]=(l-(l>>1))*add[rt]; segtree[rt<<1|1]=(l>>1)*add[rt]; add[rt]=0; }}void plant(ll left,ll right,ll rt){ add[rt]=0; if(left==right) { segtree[rt]=1; return ; } ll mid=(left+right)>>1; plant(left,mid,rt<<1); plant(mid+1,right,rt<<1|1); segtree[rt]=segtree[rt<<1]+segtree[rt<<1|1];}ll query(ll left,ll right,ll rt,ll L,ll R){ if(L<=left&&right<=R) { return segtree[rt]; } PushDown(rt,right-left+1); ll mid=(left+right)/2; ll ans=0; if(L<=mid) ans+=query(left,mid,rt<<1,L,R); if(R>mid) ans+=query(mid+1,right,rt<<1|1,L,R); return ans;}void update(ll left,ll right,ll rt,ll l,ll r,ll up){ if(l<=left&&r>=right) { segtree[rt]=(ll)up*(right-left+1); add[rt]=up; return; } PushDown(rt,right-left+1); int mid=(left+right)>>1; if(l<=mid)update(left,mid,rt<<1,l,r,up); if(r>mid)update(mid+1,right,rt<<1|1,l,r,up); segtree[rt]=segtree[rt<<1]+segtree[rt<<1|1];}int main(){ int n,m; int t; m=1; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(segtree,0,sizeof(segtree)); memset(add,0,sizeof(add)); plant(1,n,1); int a,b,c; int q; scanf("%d",&q); for(int i=0;i<q;i++) { scanf("%d%d%d",&a,&b,&c); update(1,n,1,a,b,c); } printf("Case %d: The total value of the hook is %I64d.\n",m++,query(1,n,1,1,n)); } return 0;}
阅读全文
0 0
- HDU 1698 Just A Hook (线段树,区间修改)
- HDU - 1698 Just a Hook (线段树区间修改)
- HDU 1698 Just a Hook (线段树区间修改)
- HDU 1698 Just a Hook(线段树区间修改)
- hdu 1698Just a Hook(线段树区间修改)
- (HDU 1698)Just a Hook 线段树区间修改
- HDU ACM 1698 Just a Hook->线段树+区间修改
- HDU-1698 Just a Hook (线段树 区间修改查询)
- 【hdu】Just a Hook(线段树区间修改)
- HDU 1698 Just A Hook(线段树修改区间到某值)
- HDU 1698 Just a Hook(线段树-区间修改|区间查询)
- HDU 1698 Just a Hook (线段树:区间修改+区间查询)
- HDU 1698 Just a Hook 数据结构+线段树+区间修改(设值)+区间求和
- HDU-1698-Just A Hook (线段树区间修改,区间查询)
- 线段树 段更新 HDU 1698 Just a Hook (线段树区间修改)
- hdu 1698 Just a Hook (线段树区间操作,)
- 线段树(区间更新) hdu-1698-Just a Hook
- hdu 1698 Just a Hook(线段树-区间更新)
- D3.js实现树图节点为矩形+内容分割+可折叠demo
- Hibernate 一对多注解 实例
- 士兵杀敌(五)
- Python httplib2异常以及互动方式
- Error:Unknown host 'downloads.gradle.org'. Enable Gradle 'offline mode' and sync project
- HDU 1698 Just A Hook(线段树修改区间到某值)
- CRL证书
- 长沙智慧眼算法工程师笔试题
- tomcat启动Error ListenerStart解决办法
- 表达式求值(数据结构)
- mybatis笔记-1-数据库的基本操作
- 安卓教程第一篇 认识安卓项目开发
- easyui 获取datagrid的url和参数 获取url 获取查询参数
- SSD linux下配置