K - Transformation

K - Transformation

Yuanfang is puzzled with the question below:
There are n integers, a 1, a 2, …, a n. The initial values of them are 0. There are four kinds of operations.
Operation 1: Add c to each number between a x and a y inclusive. In other words, do transformation a k<—a k+c, k = x,x+1,…,y.
Operation 2: Multiply c to each number between a x and a y inclusive. In other words, do transformation a k<—a k×c, k = x,x+1,…,y.
Operation 3: Change the numbers between a x and a y to c, inclusive. In other words, do transformation a k<—c, k = x,x+1,…,y.
Operation 4: Get the sum of p power among the numbers between a x and a y inclusive. In other words, get the result of a x p+a x+1 p+…+a y 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 5
3 3 5 7
1 2 4 4
4 1 5 2
2 2 5 8
4 3 5 3
0 0
Sample Output
307
7489

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#define lt k<<1
#define rt k<<1|1
#define lson l, mid, lt
#define rson mid+1, r, rt
using namespace std;
const int maxn = 100005;
const int N = 10007;

struct node
{
    int l,r;
    int lazy1,lazy2,lazy3;//分别表示加号,乘号,等号标记
} t[maxn<<2];
int n;
void build(int l, int r, int k)
{
    t[k].l = l;
    t[k].r = r;
    t[k].lazy1 = 0;
    t[k].lazy2 = 1;
    t[k].lazy3 = -1;
    if(l == r)
    {
        t[k].lazy3 = 0;//最底层等号赋值为0
        return;
    };
    int mid = (l+r)>>1;
    build(lson);
    build(rson);
}

void pushdown(int k)
{
    if(t[k].l == t[k].r)
    {
        return;
    }
    if(t[k].lazy3 != -1)//处理等号
    {
        t[lt].lazy3 = t[rt].lazy3 = t[k].lazy3;//更新子区间等号标记
        t[lt].lazy2 = t[rt].lazy2 = 1;//清空子区间加乘标记
        t[lt].lazy1 = t[rt].lazy1 = 0;
        t[k].lazy3 = -1;
        return;
    }
    if(t[k].lazy2 != 1)//处理乘号
    {
        if(t[lt].lazy3 != -1)
        {
            t[lt].lazy3 = (t[lt].lazy3*t[k].lazy2) % N;//如果子区间有等号标记,直接修改等号标记
        }
        else//否则清空该子区间标记,进行子区间标记
        {
            pushdown(lt);
            t[lt].lazy2 = (t[lt].lazy2*t[k].lazy2)%N;
        }
        if(t[rt].lazy3 != -1)
        {
            t[rt].lazy3=(t[rt].lazy3*t[k].lazy2) % N;//同理
        }
        else
        {
            pushdown(rt);
            t[rt].lazy2=(t[rt].lazy2*t[k].lazy2)%N;
        }
        t[k].lazy2=1;//清空乘法标记
    }
    if(t[k].lazy1!=0)//处理加号标记
    {
        if(t[lt].lazy3 != -1)
        {
            t[lt].lazy3=(t[lt].lazy3+t[k].lazy1)%N;
        }
        else
        {
            pushdown(lt);
            t[lt].lazy1 = (t[lt].lazy1+t[k].lazy1)%N;
        }
        if(t[rt].lazy3 != -1)
        {
            t[rt].lazy3=(t[rt].lazy3+t[k].lazy1)%N;
        }
        else
        {
            pushdown(rt);
            t[rt].lazy1=(t[rt].lazy1+t[k].lazy1)%N;
        }
        t[k].lazy1=0;//记得清空
    }
}
void update(int l, int r, int num, int d, int k)
{
    if(t[k].l == l && t[k].r == r)
    {
        if(d == 1)
        {
            if(t[k].lazy3!=-1)
            {
                t[k].lazy3=(t[k].lazy3+num)%N;//如果有等号标记,就直接修改等号标记
            }
            else
            {
                pushdown(k);//否则清空该区间,进行标记
                t[k].lazy1 = (t[k].lazy1+num)%N;
            }
        }
        else if(d == 2)//同理
        {
            if(t[k].lazy3 != -1)
            {
                t[k].lazy3=(t[k].lazy3*num)%N;
            }
            else
            {
                pushdown(k);
                t[k].lazy2 = (t[k].lazy2*num) % N;
            }
        }
        else
        {
            t[k].lazy3=num%N;
            t[k].lazy1=0;
            t[k].lazy2=1;
        }
        return;
    }
    pushdown(k);//向下更新
    int mid = (t[k].l+t[k].r)>>1;
    if(r <= mid)
    {
        update(l,r,num,d,lt);
    }
    else if(l>mid)
    {
        update(l,r,num,d,rt);
    }
    else
    {
        update(l,mid,num,d,lt);
        update(mid+1,r,num,d,rt);
    }
}

int query(int l,int r,int p,int k)
{
    if(t[k].l>=l&&t[k].r<=r&&t[k].lazy3!=-1)//查到是查询区间的子区间且一段全为相同的数
    {
        int temp = 1;
        for(int i = 1; i <= p; i++)
        {
            temp = (temp*t[k].lazy3)%N;
        }
        return ((t[k].r-t[k].l+1)*temp)%N;//注意要乘上长度
    }
    pushdown(k);
    int mid = (t[k].l + t[k].r)>>1;
    if(r <= mid)
    {
        return query(l,r,p,lt)%N;
    }
    else if(l > mid)
    {
        return query(l,r,p,rt)%N;
    }
    else
    {
        return (query(l,mid,p,lt)+query(mid+1,r,p,rt))%N;
    }
}

int main()
{
    int m;
    while(~scanf("%d%d",&n,&m))
    {
        if(!n && !m)
        {
            break;
        }
        build(1,n,1);
        for(int i = 0; i < m; i++)
        {
            int d,x,y,c;
            scanf("%d%d%d%d",&d, &x, &y, &c);
            if(d >= 1 && d <= 3)
            {
                update(x,y,c,d,1);
            }
            else
            {
                printf("%d\n",query(x,y,c,1)%N);
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于 MATLAB 的 Ensemble Patch Transformation 的简单实现代码: ```matlab function transformedImage = ensemblePatchTransformation(inputImage, patchSize, ensembleSize) [rows, cols] = size(inputImage); % 初始化输出图像 transformedImage = zeros(rows, cols); % 遍历图像的每个像素 for i = 1:rows for j = 1:cols % 提取当前像素位置的图像块 patch = inputImage(max(i-patchSize, 1):min(i+patchSize, rows), ... max(j-patchSize, 1):min(j+patchSize, cols)); % 随机选择ensembleSize个图像块 selectedPatches = zeros(patchSize*2+1, patchSize*2+1, ensembleSize); for k = 1:ensembleSize randomRow = randi(size(patch, 1)); randomCol = randi(size(patch, 2)); selectedPatches(:,:,k) = patch(randomRow-patchSize:randomRow+patchSize, ... randomCol-patchSize:randomCol+patchSize); end % 对选定的图像块求平均值作为当前像素位置的输出值 transformedImage(i, j) = mean(selectedPatches(:)); end end end ``` 这段代码实现了 Ensemble Patch Transformation 的基本算法。输入参数包括原始图像 `inputImage`、图像块大小 `patchSize` 和 ensemble 大小 `ensembleSize`。函数将遍历输入图像的每个像素,并对每个像素位置提取一个图像块。然后,它从该图像块中随机选择 ensembleSize 个图像块,并计算它们的平均值作为当前像素位置的输出值。最终,函数返回一个经过 Ensemble Patch Transformation 处理后的图像。 请注意,这只是一个简单的实现示例,可能需要根据具体的需求进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值