HDU1698 Just a Hook 線段樹成段更新
阿新 • • 發佈:2019-01-08
基礎的線段樹成段更新,學習了notonlysuccess dalao的程式碼後寫的
#include<iostream> #include<cstdio> #include<cstring> #include<ctime> #include<algorithm> #include<cstdlib> #include<cmath> #include<set> #include<bitset> #include<map> #include<stack> #include<queue> #include<vector> #include<utility> #define INF 0x3f3f3f3f #define inf 2*0x3f3f3f3f #define llinf 1000000000000000000 #define pi acos(-1) #define mod 1000000007 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; typedef long long ll; typedef pair<int,int>P; int col[100005*4],sum[100005*4],t,n,q,x,y,z; void pushup(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void pushdown(int rt,int m) { if (col[rt]) { col[rt<<1] = col[rt<<1|1] = col[rt]; sum[rt<<1] = (m - (m >> 1)) * col[rt]; sum[rt<<1|1] = (m >> 1) * col[rt]; col[rt] = 0; } } void build(int l,int r,int rt) { col[rt]=0; sum[rt]=1; if(l==r)return ; int m=(l+r)>>1; build(lson);build(rson); pushup(rt); } void change(int L,int R,int x,int l,int r,int rt) { if (L <= l && r <= R) { col[rt] = x; sum[rt] = x * (r - l + 1); return ; } pushdown(rt,r-l+1); int m=(l+r)>>1; if(L<=m)change(L,R,x,lson); if(R>m)change(L,R,x,rson); pushup(rt); } int main() { cin>>t; for(int ca=1;ca<=t;ca++) { scanf("%d%d",&n,&q); build(1,n,1); while(q--) { scanf("%d%d%d",&x,&y,&z); change(x,y,z,1,n,1); } printf("Case %d: The total value of the hook is %d.\n",ca,sum[1]); } return 0; }