Hdu 1698(线段树 区间修改 区间查询)
![](https://gsp0.baidu.com/5aAHeD3nKhI2p27j8IqW0jdnxx1xbK/tb/editor/images/client/image_emoticon25.png)
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.
InputThe 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.
OutputFor 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.
题意:有一个长度为n的钩子,钩子由金银铜三种棒子组成,所以问题来了:骚年,这是你的金棒子,还是你的银棒子,咳咳,扯远了,现在有个浪到飞起的男人,他能将一段的不管曾经是怎样的棒子全部改成自己想要的棒子.好的,现在已知金棒子价值为3,银为2,铜为1.
求修改完以后整个钩子的价值.
再多练几道线段树吧...不过这道题没强制在线,似乎可以搞些别的东东?当我没说....反正就打线段树吧...
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define N 100000 #define lson root<<1 #define rson root<<1|1 using namespace std; int node[N<<2],lazy[N<<2]; void pushup(int root) { node[root]=node[lson]+node[rson]; } void pushdown(int root,int val) { if(lazy[root]!=-1) { lazy[lson]=lazy[root]; lazy[rson]=lazy[root]; node[lson]=(val-(val>>1))*lazy[root]; node[rson]=(val>>1)*lazy[root]; lazy[root]=-1; } } void build(int l,int r,int root) { lazy[root]=-1; node[root]=1; if(l==r) { return; } int mid=(l+r)>>1; build(l,mid,lson); build(mid+1,r,rson); pushup(root); } void update(int left,int right,int val,int l,int r,int root) { if(left<=l&&right>=r) { lazy[root]=val; node[root]=val*(r-l+1); return; } pushdown(root,r-l+1); int mid=(l+r)>>1; if(left<=mid) { update(left,right,val,l,mid,lson); } if(mid<right) { update(left,right,val,mid+1,r,rson); } pushup(root); } int main() { int ttt=0,t; scanf("%d",&t); while(t--) { int n,m; scanf("%d%d",&n,&m); build(1,n,1); for(int i=1;i<=m;i++) { int ll,rr,ww; scanf("%d%d%d",&ll,&rr,&ww); update(ll,rr,ww,1,n,1); } printf("Case %d: The total value of the hook is %d.\n",++ttt,node[1]); } }
每天刷题,身体棒棒!