标签:cstring ade hook for 修改 inpu ever nal metal
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]); } }
每天刷题,身体棒棒!
标签:cstring ade hook for 修改 inpu ever nal metal
原文地址:http://www.cnblogs.com/stxy-ferryman/p/7634508.html