HDU 4578 Transformation
题意很简单,说是裸的线段树。然而我觉得十分有难度。。。。
首先第一步:将所有数值看作是ax+b的形式。对于乘操作 变成kax+kb,对于加操作变成ax+b+c;
第二步操作顺序:因为操作顺序会影响答案(一开始还想每个区间维护一个queue结果在TLE和MLE之间徘徊)
首先最优的一定是设置为一个值set操作,因为这个操作会清空前面的操作使得ax+b中a=0,b=0
第二个操作就是mul乘操作,这里就要注意了执行mul操作之前一定要对如果区间的add值不为0,一定要对区间add值进行更新。变成add*系数
第三个操作就是加。
第三步:如果push_down,lazy如何下推
对于ax+b如果一个区间要push_down那么他可以变成kax+kb+c k为mul系数,c为加系数。注意k可以为1,c可以等于0,不影响下面的结果
第一sum1,一次方和 这个太简单了就不说了
第二sum2,注意有(x + y) ^ 2 = x ^ 2 + y ^ 2 + 2xy 在本题中,每一项变成了
sum(a[i]x[i] + b[i])^2 和新设置的值sum(ka[i]x[i] + kb[i] + c) ^ 2 这里是最终的和的公式
为了方便看,你只需要对随便一项展开就可以看到如何下推,详情请看代码即可知道,展开后可以提出k^2还有什么忘了
的一个项,你展开就可以看到如果做的
第三 sum3 同sum2 只是更复杂一些 同样展开我记得有k^3,k^2*c,k*c^2这2个可以提出来
下面就是代码
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <climits> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define PI 3.1415926535897932626 using namespace std; int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);} const int MAXN = 100010; const int INF = 0x3f3f3f3f; const int MOD = 10007; struct node { int l,r; int flagset,flagadd,flagmul; int sum1,sum2,sum3; }tree[MAXN * 4]; void build(int id,int l,int r) { tree[id].l = l; tree[id].r = r; tree[id].flagset = tree[id].flagadd = 0; tree[id].flagmul = 1; tree[id].sum1 = tree[id].sum2 = tree[id].sum3 = 0; if (l == r) return; int mid = (l + r) / 2; build(id * 2,l,mid); build(id * 2 + 1,mid + 1,r); } void push_up(int id) { if (tree[id].l == tree[id].r) return; tree[id].sum1 = (tree[id * 2].sum1 + tree[id * 2 + 1].sum1) % MOD; tree[id].sum2 = (tree[id * 2].sum2 + tree[id * 2 + 1].sum2) % MOD; tree[id].sum3 = (tree[id * 2].sum3 + tree[id * 2 + 1].sum3) % MOD; } void push_down(int id) { if (tree[id].l == tree[id].r)return; if (tree[id].flagset != 0) { tree[id * 2].flagset = tree[id * 2 + 1].flagset = tree[id].flagset; tree[id * 2].flagmul = tree[id * 2 + 1].flagmul = 1; tree[id * 2].flagadd = tree[id * 2 + 1].flagadd = 0; tree[id * 2].sum1 = (tree[id * 2].r - tree[id * 2].l + 1) * tree[id].flagset % MOD; tree[id * 2].sum2 = (tree[id * 2].r - tree[id * 2].l + 1) * tree[id].flagset % MOD * tree[id].flagset % MOD; tree[id * 2].sum3 = (tree[id * 2].r - tree[id * 2].l + 1) * tree[id].flagset % MOD * tree[id].flagset % MOD * tree[id].flagset % MOD; tree[id * 2 + 1].sum1 = (tree[id * 2 + 1].r - tree[id * 2 + 1].l + 1) * tree[id].flagset % MOD; tree[id * 2 + 1].sum2 = (tree[id * 2 + 1].r - tree[id * 2 + 1].l + 1) * tree[id].flagset % MOD * tree[id].flagset % MOD; tree[id * 2 + 1].sum3 = (tree[id * 2 + 1].r - tree[id * 2 + 1].l + 1) * tree[id].flagset % MOD * tree[id].flagset % MOD * tree[id].flagset % MOD; tree[id].flagset = 0; } if (tree[id].flagadd != 0 || tree[id].flagmul != 1) { tree[id * 2].flagadd = ( tree[id].flagmul * tree[id * 2].flagadd % MOD + tree[id].flagadd )%MOD; tree[id * 2].flagmul = tree[id * 2].flagmul * tree[id].flagmul % MOD; int sum1,sum2,sum3; sum1 = (tree[id * 2].sum1 * tree[id].flagmul % MOD + (tree[id * 2].r - tree[id * 2].l + 1) * tree[id].flagadd % MOD) % MOD; sum2 = (tree[id].flagmul * tree[id].flagmul % MOD * tree[id * 2].sum2 % MOD + 2 * tree[id].flagadd * tree[id].flagmul % MOD * tree[id * 2].sum1 % MOD + (tree[id * 2].r - tree[id * 2].l + 1) * tree[id].flagadd % MOD * tree[id].flagadd % MOD) % MOD; sum3 = tree[id].flagmul * tree[id].flagmul % MOD * tree[id].flagmul % MOD * tree[id * 2].sum3 % MOD; sum3 = (sum3 + 3 * tree[id].flagmul % MOD * tree[id].flagmul % MOD * tree[id].flagadd % MOD * tree[id * 2].sum2) % MOD; sum3 = (sum3 + 3 * tree[id].flagmul % MOD * tree[id].flagadd % MOD * tree[id].flagadd % MOD * tree[id * 2].sum1) % MOD; sum3 = (sum3 + (tree[id * 2].r - tree[id * 2].l + 1) * tree[id].flagadd % MOD * tree[id].flagadd % MOD * tree[id].flagadd % MOD) % MOD; tree[id * 2].sum1 = sum1; tree[id * 2].sum2 = sum2; tree[id * 2].sum3 = sum3; tree[id * 2 + 1].flagadd = ( tree[id].flagmul * tree[id * 2 + 1].flagadd % MOD + tree[id].flagadd )%MOD; tree[id * 2 + 1].flagmul = tree[id * 2 + 1].flagmul * tree[id].flagmul % MOD; sum1 = (tree[id * 2 + 1].sum1 * tree[id].flagmul % MOD + (tree[id * 2 + 1].r - tree[id * 2 + 1].l + 1) * tree[id].flagadd % MOD) % MOD; sum2 = (tree[id].flagmul * tree[id].flagmul % MOD * tree[id * 2 + 1].sum2 % MOD + 2 * tree[id].flagadd * tree[id].flagmul % MOD * tree[id * 2 + 1].sum1 % MOD + (tree[id * 2 + 1].r - tree[id * 2 + 1].l + 1) * tree[id].flagadd % MOD * tree[id].flagadd % MOD)% MOD; sum3 = tree[id].flagmul * tree[id].flagmul % MOD * tree[id].flagmul % MOD * tree[id * 2 + 1].sum3 % MOD; sum3 = (sum3 + 3 * tree[id].flagmul % MOD * tree[id].flagmul % MOD * tree[id].flagadd % MOD * tree[id * 2 + 1].sum2) % MOD; sum3 = (sum3 + 3 * tree[id].flagmul % MOD * tree[id].flagadd % MOD * tree[id].flagadd % MOD * tree[id * 2 + 1].sum1) % MOD; sum3 = (sum3 + (tree[id * 2 + 1].r - tree[id * 2 + 1].l + 1) * tree[id].flagadd % MOD * tree[id].flagadd % MOD * tree[id].flagadd % MOD) % MOD; tree[id * 2 + 1].sum1 = sum1; tree[id * 2 + 1].sum2 = sum2; tree[id * 2 + 1].sum3 = sum3; tree[id].flagadd = 0; tree[id].flagmul = 1; } } void update(int id,int l,int r,int type,int val) { if (tree[id].l == l && tree[id].r == r) { val %= MOD; if (type == 1) { tree[id].flagadd = (tree[id].flagadd + val) % MOD; tree[id].sum3 = (tree[id].sum3 + 3 * tree[id].sum2 % MOD * val % MOD + 3 * tree[id].sum1 % MOD * val % MOD * val % MOD + (tree[id].r - tree[id].l + 1) * val % MOD * val % MOD * val % MOD) % MOD; tree[id].sum2 = (tree[id].sum2 + 2 * tree[id].sum1 % MOD * val % MOD + (tree[id].r - tree[id].l + 1)* val % MOD * val % MOD) % MOD; tree[id].sum1 = (tree[id].sum1 + (tree[id].r - tree[id].l + 1) * val % MOD)%MOD; } else if (type == 2) { tree[id].flagadd = (tree[id].flagadd * val) % MOD; tree[id].flagmul = (tree[id].flagmul * val) % MOD; tree[id].sum1 = tree[id].sum1 * val % MOD; tree[id].sum2 = tree[id].sum2 * val % MOD * val % MOD; tree[id].sum3 = tree[id].sum3 * val % MOD * val % MOD * val % MOD; } else { tree[id].flagmul = 1; tree[id].flagadd = 0; tree[id].flagset = val % MOD; tree[id].sum1 = val * (tree[id].r - tree[id].l + 1) % MOD; tree[id].sum2 = val * (tree[id].r - tree[id].l + 1) % MOD * val % MOD; tree[id].sum3 = val * (tree[id].r - tree[id].l + 1) % MOD * val % MOD * val % MOD; } return; } push_down(id); int mid = (tree[id].l + tree[id].r) / 2; if (l > mid) update(id * 2 + 1,l,r,type,val); else if (r <= mid) update(id * 2,l,r,type,val); else { update(id * 2,l,mid,type,val); update(id * 2 + 1,mid + 1,r,type,val); } push_up(id); } int query(int id,int l,int r,int ask) { if (tree[id].l == l && tree[id].r == r) { //printf("%d %d %I64d\n",tree[id].sum2); if (ask == 1) return tree[id].sum1; else if (ask == 2) return tree[id].sum2; else return tree[id].sum3; } push_down(id); int mid = (tree[id].l + tree[id].r) / 2; if (l > mid) return query(id * 2 + 1,l,r,ask) % MOD; else if (r <= mid) return query(id * 2,l,r,ask) % MOD; else { return (query(id * 2,l,mid,ask) % MOD + query(id * 2 + 1,mid + 1,r,ask) % MOD) % MOD; } } int main() { //freopen("sample.txt","r",stdin); int N,M; while (scanf("%d%d",&N,&M) != EOF) { if (N == 0 && M == 0) break; build(1,1,N); while (M--) { int x,y,z,op; scanf("%d%d%d%d",&op,&x,&y,&z); if (op == 4) printf("%d\n",query(1,x,y,z) % MOD); else update(1,x,y,op,z); } } return 0; }
今年输的,明年全都要赢回来