Transformation

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=4578

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 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

题意

给你一个序列,支持四种操作

1.区间加法

2.区间乘法

3.区间减法

4.求和,平方和,立方和 即\(\large \sum_{i=l}^{r}{a_i^p}(1\le p\le 3)\)

题解

一开始看到这道题,觉得可以用数学公式搞搞,搞了半天确实搞出了个公式,用sum1,sum2,sum3分别存和,平方和,立方和,然后合并的时候再搞

搞。但是感觉很麻烦,于是先上网查了查正解是不是有什么巧妙的方法。但是看完网上题解,我才发现都是用的玄学复杂度。

于是我就愉快地也跟着各位大佬一样玄学操作啦。

具体操作:还是用线段树,遇到一段连续相同的区间就可以马上得到答案,其余部分直接暴力就行,我寻思着只要先把每个数都变得不一样然后求所有数的立方和,直接就暴了(别想那么多,这题纯属娱乐)。

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x7f7f7f7f
#define N 100050
#define mo 10007
ll n,m;
struct Node{ll l,r,lazy;};
struct segmentTree
{
Node tr[N<<2];
void push_up(ll x);
void push_down(ll x);
void bt(ll x,ll l,ll r);
void add(ll x,ll l,ll r,ll tt);
void multiply(ll x,ll l,ll r,ll tt);
void cover(ll x,ll l,ll r,ll tt);
ll query(ll x,ll l,ll r,ll tt);
}seg;
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
void segmentTree::push_up(ll x)
{
if(tr[x].l==tr[x].r)return;
Node &a=tr[x<<1],&b=tr[x<<1|1];
if (a.lazy==b.lazy&&tr[x].lazy==-1)tr[x].lazy=a.lazy;
}
void segmentTree::push_down(ll x)
{
if (tr[x].lazy==-1)return;
tr[x<<1].lazy=tr[x].lazy;
tr[x<<1|1].lazy=tr[x].lazy;
tr[x].lazy=-1;
}
void segmentTree::bt(ll x,ll l,ll r)
{
tr[x]=Node{l,r,0};
if (l==r)return;
ll mid=(l+r)>>1;
bt(x<<1,l,mid);
bt(x<<1|1,mid+1,r);
}
void segmentTree::add(ll x,ll l,ll r,ll tt)
{
if (l<=tr[x].l&&tr[x].r<=r&&tr[x].lazy!=-1)
{
tr[x].lazy+=tt;
tr[x].lazy%=mo;
return;
}
ll mid=(tr[x].l+tr[x].r)>>1;
push_down(x);
if (l<=mid)add(x<<1,l,r,tt);
if (mid<r)add(x<<1|1,l,r,tt);
push_up(x);
}
void segmentTree::multiply(ll x,ll l,ll r,ll tt)
{
if (l<=tr[x].l&&tr[x].r<=r&&tr[x].lazy!=-1)
{
tr[x].lazy*=tt;
tr[x].lazy%=mo;
return;
}
ll mid=(tr[x].l+tr[x].r)>>1;
push_down(x);
if (l<=mid)multiply(x<<1,l,r,tt);
if (mid<r)multiply(x<<1|1,l,r,tt);
push_up(x);
}
void segmentTree::cover(ll x,ll l,ll r,ll tt)
{
if (l<=tr[x].l&&tr[x].r<=r&&tr[x].lazy!=-1)
{
tr[x].lazy=tt%mo;
return;
}
ll mid=(tr[x].l+tr[x].r)>>1;
push_down(x);
if (l<=mid)cover(x<<1,l,r,tt);
if (mid<r)cover(x<<1|1,l,r,tt);
push_up(x);
}
ll segmentTree::query(ll x,ll l,ll r,ll tt)
{
if (l<=tr[x].l&&tr[x].r<=r&&tr[x].lazy!=-1)
{
ll ans=1;
for(ll i=1;i<=tt;i++)ans*=tr[x].lazy;
ans*=(tr[x].r-tr[x].l+1);
return ans%mo;
}
ll mid=(tr[x].l+tr[x].r)>>1,ans=0;
push_down(x);
if(l<=mid)ans+=query(x<<1,l,r,tt);
if(mid<r)ans+=query(x<<1|1,l,r,tt);
push_up(x);
return ans%mo;
}
void work()
{
read(n); read(m);
if (n+m==0)exit(0);
seg.bt(1,1,n);
for(ll i=1;i<=m;i++)
{
ll id,x,y,tt;
read(id); read(x); read(y); read(tt);
if (id==1)seg.add(1,x,y,tt);
if (id==2)seg.multiply(1,x,y,tt);
if (id==3)seg.cover(1,x,y,tt);
if (id==4)printf("%lld\n",seg.query(1,x,y,tt));
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
while(1)work();
}

HDU 4578 线段树玄学算法?的更多相关文章

  1. hdu 4578 线段树(标记处理)

    Transformation Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others) ...

  2. HDU - 4578 线段树+三重操作

    这道题自己写了很久,还是没写出来,也看了很多题解,感觉多数还是看的迷迷糊糊,最后面看到一篇大佬的才感觉恍然大悟. 先上一篇大佬的题解:https://blog.csdn.net/aqa20372995 ...

  3. hdu 4578 线段树 ****

    链接:点我  1

  4. K - Transformation HDU - 4578 线段树经典题(好题)

    题意:区间  加   变成定值 乘  区间查询:和 平方和 立方和 思路:超级超级超级麻烦的一道题  设3个Lazy 标记分别为  change 改变mul乘 add加  优先度change>m ...

  5. HDU 4578 线段树复杂题

    题目大意: 题意:有一个序列,有四种操作: 1:区间[l,r]内的数全部加c. 2:区间[l,r]内的数全部乘c. 3:区间[l,r]内的数全部初始为c. 4:询问区间[l,r]内所有数的P次方之和. ...

  6. hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  7. hdu 3974 线段树 将树弄到区间上

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. hdu 3436 线段树 一顿操作

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  9. hdu 3397 线段树双标记

    Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

随机推荐

  1. 爬虫之操作excel

    几种常用模块的使用方法 注释:Excel 2003 即XLS文件有大小限制即65536行256列,所以不支持大文件,而Excel 2007以上即XLSX文件的限制则为1048576行16384列 下面 ...

  2. CF1053E Euler tour

    题意 给出一个某些位置不全的欧拉序,求出一个符合条件的,或输出不行 传送门 \(n \le 5*10^5\) 思路 终于不是一道神仙\(dp\) 变成了一道神仙构造 以下简称两相同数围成的是一个区间, ...

  3. P5149 会议座位

    P5149 会议座位 题意: 其实还是求逆序对数. 解法: 用离散化统计每个数,再用树状数组求逆序对. CODE: #include<iostream> #include<cstdi ...

  4. 【Linux】安装 node.js

    1.在 linux 上安装 node.js 有几种方式,这里 教的是最简单的一种,因为其他都比较坑. 2.先去 node.js 官网 获取到已经编译好的安装包的地址, 3.使用 wget 去下载上面获 ...

  5. windows 连接 Linux 云服务器

    1.在我们购买了 阿里云 或者 腾讯云后,如果选择使用的是 Linux 系统,在 windows 上要远程连接,需要用到的是 putty 这一个软件 putty 官网:https://www.putt ...

  6. 【SpringBoot】转载 springboot使用thymeleaf完成数据的页面展示

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/weixin_36380516/artic ...

  7. <JavaScript> 关于闭包和this对象

    1.this指向windows是如何得出的 var name = "The Window"; var object = { name : "My Object" ...

  8. android在点击EditText的时候始终不弹出软件键盘

    场景描述:正常情况下,当点击EditText时,软键盘会弹出来.现在的要求是当点击EditText时,弹日期选择对话框,选择的结果显示在EditText上.若不处理,当点击EditText时,软键盘和 ...

  9. python执行系统命令后获取返回值的几种方式集合

    python执行系统命令后获取返回值的几种方式集合 今天小编就为大家分享一篇python执行系统命令后获取返回值的几种方式集合,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看吧 第一种情 ...

  10. Python排序算法之选择排序定义与用法示例

    Python排序算法之选择排序定义与用法示例 这篇文章主要介绍了Python排序算法之选择排序定义与用法,简单描述了选择排序的功能.原理,并结合实例形式分析了Python定义与使用选择排序的相关操作技 ...