vj网址 点击打开链接
这道题 题意就是 给你 给你输入 n ,1 n 区间 内的数字为 1 然后 q 行是 修改nn到mm的值 然后这些值 等于输入的值(注意 不是求和) 然后问你最后的数字 和是多少 然后这一道题我思考了 很久很久 然后就是没有ac 然后最后证明了 我的思路 有了很大的问题 我一开始想的就是 让 这个相等的区间转化成 相加的区间 然后样例 就没有过 当时 傻乎乎的我 以为是样例错了 然后就特判了 样例 结果 是wa了 然后我队友在我旁边笑得贼开心 (这队友有点想砍死的冲动 ) 然后 我就看了别人的博客~~ 没办法 当时没有换一种思路 因为坚信自己的思路是正确的然后 看了看别人的博客 在模拟了一下自己的思路 发现 想要 转换成相加不现实 因为 每个区间的值不一样 怎么可能转化成 相等呢 然后想明白这一点 就 自己打一遍 就直接ac了
#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<string> #include<math.h> using namespace std; const int kk=105000; int sum[kk<<2],add[kk<<2],summ; int n,m,z; void upset(int re) { sum[re]=sum[re<<1]+sum[re<<1|1]; } void build(int l,int r,int re) { if(l==r) { sum[re]=1; //printf("%d\n",re); return; } int mid=(l+r)>>1; build(l,mid,re<<1); build(mid+1,r,re<<1|1); upset(re); } void ad(int l,int r,int re) { // printf("%d %d %d\n",l,r,re); if(add[re]) { sum[re<<1]=l*add[re]; sum[re<<1|1]=r*add[re]; add[re<<1]=add[re]; add[re<<1|1]=add[re]; } add[re]=0; } void update(int l,int r,int ll,int rr,int re) { if(l>=ll&&r<=rr) { sum[re]=(r-l+1)*z; add[re]=z; return; } int mid=(l+r)>>1; // printf("%d %d %d\n",l,r,mid); ad(mid-l+1,r-mid,re); if(rr>mid) update(mid+1,r,ll,rr,re<<1|1); if(mid>=ll) update(l,mid,ll,rr,re<<1); upset(re); } int main() { int t,tt,q,qw=0; scanf("%d",&t); while(t--) { summ=0; memset(add,0,sizeof(add)); qw++; scanf("%d",&tt); // printf("1\n"); build(1,tt,1); scanf("%d",&q); while(q--) { scanf("%d%d%d",&n,&m,&z); update(1,tt,n,m,1); // for(int i=1;i<=10;i++) // printf("%d\n",sum[i]); } printf("Case %d: The total value of the hook is %d.\n",qw,sum[1]); } return 0; }