(hdu 1698) Just a Hook(區間更新)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 34260 Accepted Submission(s): 16716
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
題意:一個鉤有銅,銀,金三種棍子,重分別為1,2,3
給定n,q,開始[1,n]內都是銅棍,經過q次[x,y]內的更新後,問鉤多重?
分析:區間更新,直接把[x,y]內的數值 置為 z
注意輸出的標點符號!!! 找這種bug 可以讓自己懷疑題目錯了。。。 噗
1341MS 5972K 1675 B C++
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long LL;
const LL N=1e5+5;
LL sum[N<<2],a[N],MAX[N<<2],ad[N<<2];
void pushup(LL rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
// MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);
}
void pushdown(int rt,int m)
{
if(ad[rt])
{
ad[rt<<1]=ad[rt];
ad[rt<<1|1]=ad[rt];
sum[rt<<1]=ad[rt]*(m-(m>>1));
sum[rt<<1|1]=ad[rt]*(m>>1);
ad[rt]=0;
}
}
void build(LL l,LL r,LL rt)
{
ad[rt]=0;
if(l==r)
{
sum[rt]=1;
return ;
}
LL m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(LL L,LL R,LL c,LL l,LL r,LL rt)
{
if(L<=l&&R>=r)
{
ad[rt]=c;
sum[rt]=c*(r-l+1);
return ;
}
pushdown(rt,r-l+1);
LL m=(l+r)>>1;
if(L<=m) update(L,R,c,lson);
if(R>m) update(L,R,c,rson);
pushup(rt);
}
LL query(LL L,LL R,LL l,LL r,LL rt)
{
if(L<=l&&R>=r)
{
return sum[rt];
}
pushdown(rt,r-l+1);
LL m=(l+r)>>1;
LL ret=0;
if(L<=m) ret+=query(L,R,lson);
if(R>m) ret+=query(L,R,rson);
return ret;
}
int main()
{
int t,cas=1;
scanf("%d",&t);
while(t--)
{
int n,q;
scanf("%d%d",&n,&q);
build(1,n,1);
for(int i=0;i<q;i++)
{
LL x,y,z;
scanf("%lld%lld%lld",&x,&y,&z);
update(x,y,z,1,n,1);
}
printf("Case %d: The total value of the hook is %lld.\n",cas++,query(1,n,1,n,1));
}
return 0;
}
相關文章
- HDU 1698 Just a Hook (線段樹區間更新)Hook
- HDU1698 Just a Hook【線段樹基礎:區間修改+區間查詢】Hook
- HDU 1556【區間更新+單點查詢 樹狀陣列】陣列
- 區間更新+差分
- 【樹狀陣列 區間更新區間查詢】code陣列
- HDU 1754 I Hate It (線段樹 區間最值)
- poj 3468 區間更新 整個區間加一個數和區間求和操作
- poj 3237 樹鏈剖分(區間更新,區間查詢)
- hdu3415 單調佇列求區間最大和佇列
- HYSBZ 2243 樹鏈剖分(區間更新,區間查詢)較難
- (poj3468)A Simple Problem with Integers(區間更新)
- Just Do IT
- Just a joke
- oracle sybase之間聯表更新的區別Oracle
- hihocoder 1078 線段樹的區間修改 (線段樹 區間更新 模板)
- POJ 3468 A Simple Problem with Integers (線段樹 區間更新)
- Codeforces 52C (線段樹區間更新)
- HDU1754 I Hate It 【線段樹基礎:點修改+區間查詢】
- HDU 4251-The Famous ICPC Team Again(劃分樹-區間中位數)AI
- POJ 1195-Mobile phones(二維樹狀陣列-區間更新區間查詢)陣列
- HDU 1556-Color the ball(樹狀陣列-區間修改 單點查詢)陣列
- 城市文化區域更新模式助力夜間經濟模式
- POJ 3468-A Simple Problem with Integers(區間更新線段樹)
- 樹狀陣列單點更新和區間查詢陣列
- MySQL ERROR 1698 (28000) 錯誤MySqlError
- It's not just a place to restREST
- 【樹狀陣列 單點修改,區間求值】hdu 1166 敵兵佈陣陣列
- HDU 4622 Reincarnation( 任意區間子串的長度, 字尾陣列+RMQ)陣列MQ
- Hook技術之Hook ActivityHook
- DNS: More than just namesDNS
- Just as it's the EA name attached to it
- hook!Hook
- POJ 2528 Mayor's posters (線段樹 區間更新+離散化)
- POJ 2528 Mayor's posters (線段樹區間更新 + 離散化)
- HDU1166 敵兵佈陣【線段樹基礎:點修改+區間查詢】
- HDU1166 敵兵佈陣【樹狀陣列 單點修改+區間查詢】陣列
- hdu4417 樹狀陣列(求指定區間比指定數小的數的個數)陣列
- HDU 3074 Multiply game(線段樹 單點更新)GAM