HDU

来源:互联网 发布:钉钉免费网络通话原理 编辑:程序博客网 时间:2024/05/23 11:54

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4578点击打开链接

Transformation

Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others)
Total Submission(s): 5897    Accepted Submission(s): 1464


Problem Description
Yuanfang is puzzled with the question below: 
There are n integers, a1, a2, …, an. The initial values of them are 0. There are four kinds of operations.
Operation 1: Add c to each number between ax and ay inclusive. In other words, do transformation ak<---ak+c, k = x,x+1,…,y.
Operation 2: Multiply c to each number between ax and ay inclusive. In other words, do transformation ak<---ak×c, k = x,x+1,…,y.
Operation 3: Change the numbers between ax and ay to c, inclusive. In other words, do transformation ak<---c, k = x,x+1,…,y.
Operation 4: Get the sum of p power among the numbers between ax and ay inclusive. In other words, get the result of axp+ax+1p+…+ay p.
Yuanfang has no idea of how to do it. So he wants to ask you to help him. 
 

Input
There are no more than 10 test cases.
For each case, the first line contains two numbers n and m, meaning that there are n integers and m operations. 1 <= n, m <= 100,000.
Each the following m lines contains an operation. Operation 1 to 3 is in this format: "1 x y c" or "2 x y c" or "3 x y c". Operation 4 is in this format: "4 x y p". (1 <= x <= y <= n, 1 <= c <= 10,000, 1 <= p <= 3)
The input ends with 0 0.
 

Output
For each operation 4, output a single integer in one line representing the result. The answer may be quite large. You just need to calculate the remainder of the answer when divided by 10007.
 

Sample Input
5 53 3 5 71 2 4 44 1 5 22 2 5 84 3 5 30 0
 

Sample Output
3077489
 

题意简单易懂就不说了

三种操作 区间增加 区间乘法 区间改变 最后是次方操作 最后一步可以用多维数组实现

然而三种操作具有优先级别 这就导致了在标记释放的时候判断十分麻烦

网上看到了一个不是很起眼的博客的另一种简便写法 因为写这篇文章的时候有点距离学习那篇文章一两个星期了 就不找原文了

本次贴的代码也是使用Union回溯的方法

先简单说一下这种思想

依旧使用懒惰标记 但是标记的是整个区间是否为同一个状态

并且每次更新的时候回溯状态 即如果左右子树都是相同状态 则在该树的根节点上更新标记这一统一状态

如此一来 只需要在计算区间的时候进行区间内的更新操作即可 因为区间内都为统一状态 可以用乘法得到

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>using namespace std;const int mod=10007;int n,q;struct xjy{    int left;    int right;    int lazy;    int num;};xjy tree[400009];void build(int i,int left ,int right){    {        tree[i].left=left;        tree[i].right=right;        tree[i].lazy=1;        tree[i].num=0;    }    if(left!=right)    {        int mid=(left+right)>>1;        build(i<<1,left,mid);        build (i<<1|1,mid+1,right);    }}void Union(int k,int l,int r){    if(tree[l].lazy&&tree[r].lazy&&tree[k<<1].num==tree[k<<1|1].num)        tree[k].lazy=1,tree[k].num=tree[k<<1].num;    else        tree[k].lazy=0;}void update(int k,int ll,int rr,int val,int type){    if(tree[k].left==ll&&tree[k].right==rr&&tree[k].lazy)    {        if(type==1) tree[k].num=(tree[k].num+val)%mod;        else if(type==2) tree[k].num=(tree[k].num*val)%mod;        else tree[k].num=val;        return ;    }//    if(tree[k].left==tree[k].right)//        return;    if(tree[k].lazy)    {        tree[k<<1].lazy=tree[k<<1|1].lazy=1;        tree[k<<1].num=tree[k<<1|1].num=tree[k].num;        tree[k].lazy=0;    }    int mid=(tree[k].right+tree[k].left)>>1;    if(rr<=mid) update(k<<1,ll,rr,val,type);    else if(ll>mid) update(k<<1|1,ll,rr,val,type);    else    {        update(k<<1,ll,mid,val,type);        update(k<<1|1,mid+1,rr,val,type);    }    Union(k,k<<1,k<<1|1);}int aans=0;void query(int k,int ll,int rr,int val){    if(tree[k].left==ll&&tree[k].right==rr&&tree[k].lazy)    {        int ans=1;        for(int i=1;i<=val;i++) ans=(ans*tree[k].num)%mod;        ans=(ans*(tree[k].right-tree[k].left+1))%mod;        aans=(aans+ans)%mod;        return ;    }//    if(tree[k].left==tree[k].right)//        return;    if(tree[k].lazy)    {        tree[k<<1].lazy=tree[k<<1|1].lazy=1;        tree[k<<1].num=tree[k<<1|1].num=tree[k].num;        tree[k].lazy=0;    }    int mid=(tree[k].right+tree[k].left)>>1;    if(rr<=mid)        query(k<<1,ll,rr,val);    else if(ll>mid)        query(k<<1|1,ll,rr,val);    else    {        query(k<<1,ll,mid,val);        query(k<<1|1,mid+1,rr,val);    }}int main(){    while(~scanf("%d%d",&n,&q)&&(n+q))    {        build(1,1,n);        aans=0;        int x,l,r,val;        while(q--)        {            scanf("%d%d%d%d",&x,&l,&r,&val);            if(x>=1&&x<=3) update(1,l,r,val,x);            else            {                aans=0;                query(1,l,r,val);                printf("%d\n",aans);            }        }    }    return 0;}





原创粉丝点击