CF438D The Child and Sequence

原题链接:http://codeforces.com/problemset/problem/438/D

The Child and Sequence

At the children’s day, the child came to Picks’s house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks.

Fortunately, Picks remembers how to repair the sequence. Initially he should create an integer array a[1],a[2],,a[n] a [ 1 ] ,   a [ 2 ] ,   ⋯ ,   a [ n ] . Then he should perform a sequence of m operations. An operation can be one of the following:

Print operation l,r l ,   r . Picks should write down the value of ri=la[i] ∑ i = l r a [ i ] .
Modulo operation l,r,x l ,   r ,   x . Picks should perform assignment a[i]=a[i] mod x a [ i ]   =   a [ i ]   m o d   x for each i (lir) i   ( l   ≤   i   ≤   r ) .
Set operation k,x k ,   x . Picks should set the value of a[k] a [ k ] to x x (in other words perform an assignment a[k]=x).
Can you help Picks to perform the whole sequence of operations?

Input

The first line of input contains two integer: n,m(1n,m105) n ,   m ( 1   ≤   n ,   m   ≤   10 5 ) . The second line contains n integers, separated by space: a[1],a[2],...,a[n](1a[i]109) a [ 1 ] ,   a [ 2 ] ,   . . . ,   a [ n ] ( 1   ≤   a [ i ]   ≤   10 9 ) — initial value of array elements.

Each of the next m lines begins with a number type (type{1,2,3}) ( t y p e ∈ { 1 , 2 , 3 } ) .

If type =  1 1 , there will be two integers more in the line: l,r(1lrn), which correspond the operation 1.
If type =  2 2 , there will be three integers more in the line: l,r,x(1lrn;1x109), which correspond the operation 2.
If type =  3 3 , there will be two integers more in the line: k,x(1kn;1x109), which correspond the operation 3.

Output

For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer.

Examples
input

5 5
1 2 3 4 5
2 3 5 4
3 3 5
1 2 5
2 1 3 3
1 1 3

output

8
5

input

10 10
6 9 6 7 6 1 10 10 9 5
1 3 9
2 7 10 9
2 5 10 8
1 4 7
3 3 7
2 7 9 9
1 2 4
1 6 6
1 5 9
3 1 10

output

49
15
23
1
9

Note

Consider the first testcase:

At first, a={1,2,3,4,5} a   =   { 1 ,   2 ,   3 ,   4 ,   5 } .
After operation 1,a={1,2,3,0,1} 1 , a   =   { 1 ,   2 ,   3 ,   0 ,   1 } .
After operation 2,a={1,2,5,0,1} 2 , a   =   { 1 ,   2 ,   5 ,   0 ,   1 } .
At operation 3,2+5+0+1=8 3 , 2   +   5   +   0   +   1   =   8 .
After operation 4,a={1,2,2,0,1} 4 , a   =   { 1 ,   2 ,   2 ,   0 ,   1 } .
At operation 5,1+2+2=5 5 , 1   +   2   +   2   =   5 .

题解

如果取模生效的话,被%的数至少会变成原来的一半,所以我们统计一下区间最大值,如果 maxmod m a x ≥ m o d 就暴力递归下去取模,最多取 log2max l o g 2 m a x 次。因为修改是单点的,所以对复杂度的影响不大,最终复杂度为 O(n log2n log2max) O ( n   l o g 2 n   l o g 2 m a x )

代码
#include<bits/stdc++.h>
#define ll long long
#define ls v<<1
#define rs ls|1
using namespace std;
const int M=4e5+5;
ll que[M],mx[M],sum[M],n,m;
void up(int v){mx[v]=max(mx[ls],mx[rs]),sum[v]=sum[ls]+sum[rs];}
void build(int v,int le,int ri)
{
    if(le==ri){mx[v]=sum[v]=que[le];return;}
    int mid=le+ri>>1;
    build(ls,le,mid);build(rs,mid+1,ri);
    up(v);
}
ll ask(int v,int le,int ri,int lb,int rb)
{
    if(lb<=le&&ri<=rb)return sum[v];
    int mid=le+ri>>1;ll ans=0;
    if(lb<=mid)ans=ask(ls,le,mid,lb,rb);
    if(mid<rb)ans+=ask(rs,mid+1,ri,lb,rb);
    return ans;
}
void mod(int v,int le,int ri,int lb,int rb,int d)
{
    if(le==ri){mx[v]=sum[v]=mx[v]%d;return;}
    int mid=le+ri>>1;
    if(lb<=mid&&mx[ls]>=d)mod(ls,le,mid,lb,rb,d);
    if(mid<rb&&mx[rs]>=d)mod(rs,mid+1,ri,lb,rb,d);
    up(v);
}
void alter(int v,int le,int ri,int pos,int d)
{
    if(le==ri){sum[v]=mx[v]=d;return;}
    int mid=le+ri>>1;
    if(pos<=mid)alter(ls,le,mid,pos,d);
    else alter(rs,mid+1,ri,pos,d);
    up(v);
}
void in(){scanf("%d%d",&n,&m);for(int i=1;i<=n;++i)scanf("%d",&que[i]);}
void ac()
{
    build(1,1,n);int a,b,c,op;
    for(int i=1;i<=m;++i)
    {
        scanf("%d%d%d",&op,&a,&b);
        if(op==1)printf("%I64d\n",ask(1,1,n,a,b));
        else if(op==2)scanf("%d",&c),mod(1,1,n,a,b,c);
        else alter(1,1,n,a,b);
    }
}
int main(){in();ac();}
U572266 CF437C The Child and Toy(平替) 提交2 通过1 时间限制1.00s 内存限制512.00MB 题目编号U572266 提供者77ai66 难度 普及/提高− 历史分数 暂无 提交记录 标签 推荐题目 暂无 复制 Markdown 展开 进入 IDE 模式 题目描述 On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as vi​ . The child spend vf1​+vf2​+...+vfk​ energy for removing part i where f1​,f2​,...,fk​ are the parts that are directly connected to the i -th and haven't been removed. Help the child to find out, what is the minimum total energy he should spend to remove all n parts. 输入格式 The first line contains two integers n and m ( 1<=n<=1000 ; 0<=m<=2000 ). The second line contains n integers: v1​,v2​,...,vn​ ( 0<=vi​<=105 ). Then followed m lines, each line contains two integers xi​ and yi​ , representing a rope from part xi​ to part yi​ ( 1<=xi​,yi​<=n; xi​=yi​ ). Consider all the parts are numbered from 1 to n . 输出格式 Output the minimum total energy the child should spend to remove all n parts of the toy. 输入输出样例 输入 #1 4 3 10 20 30 40 1 4 1 2 2 3 输出 #1 40 输入 #2 4 4 100 100 100 100 1 2 2 3 2 4 3 4 输出 #2 400 输入 #3 7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4 输出 #3 160 说明/提示 One of the optimal sequence of actions in the first sample is: First, remove part 3 , cost of the action is 20 . Then, remove part 2 , cost of the action is 10 . Next, remove part 4 , cost of the action is 10 . At last, remove part 1 , cost of the action is 0 . So the total energy the child paid is 20+10+10+0=40 , which is the minimum. In the second sample, the child will spend 400 no matter in what order he will remove the parts. c++\
06-19
Positional encoding is a crucial technique in models like transformers to represent the order of a sequence. In a sequence, each token's position matters as the context and meaning can change based on where a token is placed. The most well - known way of positional encoding is used in the original Transformer architecture. For a sequence of length \(L\) and embedding dimension \(d\), positional encodings are added to the token embeddings. The positional encoding \(PE\) at position \(pos\) for dimension \(i\) is calculated as follows: For even dimensions (\(i = 2k\)): \[PE_{(pos,2k)}=\sin\left(\frac{pos}{10000^{\frac{2k}{d}}}\right)\] For odd dimensions (\(i = 2k + 1\)): \[PE_{(pos,2k + 1)}=\cos\left(\frac{pos}{10000^{\frac{2k}{d}}}\right)\] Here is a Python code example using PyTorch to implement this positional encoding: ```python import torch import torch.nn as nn import math def get_positional_encoding(seq_len, d_model): pe = torch.zeros(seq_len, d_model) position = torch.arange(0, seq_len, dtype=torch.float).unsqueeze(1) div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model)) pe[:, 0::2] = torch.sin(position * div_term) pe[:, 1::2] = torch.cos(position * div_term) return pe seq_length = 10 embedding_dim = 512 positional_encoding = get_positional_encoding(seq_length, embedding_dim) print(positional_encoding) ``` This positional encoding scheme has several advantages. It allows the model to learn relative positions between tokens because the sine and cosine functions have a periodic nature. Also, it can handle sequences of arbitrary lengths since the calculations are based on the position within the sequence and the embedding dimension.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值