Transformation 线段树好题 好题 (独立写出来对线段树不容易)
Transformation
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others)
Total Submission(s): 7144 Accepted Submission(s): 1811
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.
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.
3 3 5 7
1 2 4 4
4 1 5 2
2 2 5 8
4 3 5 3
0 0
7489
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
typedef long long LL;
#define fuck(x) cout<<"["<<x<<"]";
#define rtl rt<<1
#define rtr rt<<1|1
const int maxn = 1e5 + ;
const int mod = 1e4 + ;
const int INF = 2e9 + ;
int n, m;
struct node {
int l, r, eq, add, mul;
int mid() {
return (l + r) >> ;
}
} tree[maxn << ];
void build(int l, int r, int rt) {
tree[rt].l = l, tree[rt].r = r;
tree[rt].add = , tree[rt].mul = , tree[rt].eq = -;
if (l == r) {
tree[rt].eq = ;
return ;
}
int m = (l + r) >> ;
build(l, m, rtl);
build(m + , r, rtr);
}
void pushdown(int rt) {
if (tree[rt].l == tree[rt].r) return ;
if (tree[rt].eq != -) {
tree[rtl].eq = tree[rtr].eq = tree[rt].eq;
tree[rtl].add = tree[rtr].add = ;
tree[rtl].mul = tree[rtr].mul = ;
tree[rt].eq = -;
return ;
}
if (tree[rt].mul != ) {
if (tree[rtl].eq != -) tree[rtl].eq = (tree[rtl].eq * tree[rt].mul) % mod;
else {
pushdown(rtl);
tree[rtl].mul = (tree[rtl].mul * tree[rt].mul) % mod;
}
if (tree[rtr].eq != -) tree[rtr].eq = (tree[rtr].eq * tree[rt].mul) % mod;
else {
pushdown(rtr);
tree[rtr].mul = (tree[rtr].mul * tree[rt].mul) % mod;
}
tree[rt].mul = ;
}
if (tree[rt].add) {
if (tree[rtl].eq != -) tree[rtl].eq = (tree[rtl].eq + tree[rt].add) % mod;
else {
pushdown(rtl);
tree[rtl].add = (tree[rtl].add + tree[rt].add) % mod;
}
if (tree[rtr].eq != -) tree[rtr].eq = (tree[rtr].eq + tree[rt].add) % mod;
else {
pushdown(rtr);
tree[rtr].add = (tree[rtr].add + tree[rt].add) % mod;
}
tree[rt].add = ;
}
} void update(int L, int R, int rt, int op, int c) {
if (L <= tree[rt].l && tree[rt].r <= R ) {
if (op == ) {
tree[rt].eq = c;
tree[rt].mul = ;
tree[rt].add = ;
return ;
}
if (tree[rt].eq != -) {
if (op == ) tree[rt].eq = (tree[rt].eq + c) % mod;
else tree[rt].eq = (tree[rt].eq * c) % mod;
} else {
pushdown(rt);
if (op == ) tree[rt].add = (tree[rt].add + c) % mod;
else tree[rt].mul = (tree[rt].mul * c) % mod;
}
return ;
}
pushdown(rt);
int m = tree[rt].mid();
if (L <= m) update(L, R, rtl, op, c);
if (R > m) update(L, R, rtr, op, c);
} int query(int L, int R, int rt, int c) {
if (L <= tree[rt].l && tree[rt].r <= R && tree[rt].eq != -) {
int ans = ;
for (int i = ; i <= c ; i++)
ans = (ans * tree[rt].eq) % mod;
return (ans * ((tree[rt].r - tree[rt].l + ) % mod) % mod) % mod;
}
pushdown(rt);
int m = tree[rt].mid();
if (R <= m) return query(L, R, rtl, c);
else if (L > m) return query(L, R, rtr, c);
else return query(L, m, rtl, c) + query(m + , R, rtr, c);
}
int main() {
while(scanf("%d%d", &n, &m) != EOF) {
if (n == && m == ) break;
build(, n, );
while(m--) {
int op, x, y, c;
scanf("%d%d%d%d", &op, &x, &y, &c);
if (op == ) printf("%d\n", query(x, y, , c)%mod);
else update(x, y, , op, c);
}
}
return ;
}
Transformation 线段树好题 好题 (独立写出来对线段树不容易)的更多相关文章
- 【BZOJ 3196】二逼平衡树 线段树套splay 模板题
我写的是线段树套splay,网上很多人写的都是套treap,然而本蒟蒻并不会treap 奉上sth神犇的模板: //bzoj3196 二逼平衡树,支持修改某个点的值,查询区间第k小值,查询区间某个值排 ...
- 51nod 1593 公园晨跑 | ST表(线段树?)思维题
51nod 1593 公园晨跑 有一只猴子,他生活在一个环形的公园里.有n棵树围绕着公园.第i棵树和第i+1棵树之间的距离是 di ,而第n棵树和第一棵树之间的距离是 dn .第i棵树的高度是 hi ...
- UESTC 1591 An easy problem A【线段树点更新裸题】
An easy problem A Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others ...
- ●线段树的三个题(poj 3225,hdu 1542,hdu 1828)
●poj 3225 Help with Intervals(线段树区间问题) ○赘述题目 给出以下集合操作: 然后有初始的一个空集S,和以下题目给出的操作指令,并输入指令: 要求进行指令操作后,按格式 ...
- HDU 1166 敌兵布阵(线段树/树状数组模板题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 【线段树I:母题】hdu 1166 敌兵布阵
[线段树I:母题]hdu 1166 敌兵布阵 题目链接:hdu 1166 敌兵布阵 题目大意 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又開始忙乎了.A国在海 ...
- HDU 5029 Relief grain --树链剖分第一题
题意:给一棵树,每次给两个节点间的所有节点发放第k种东西,问最后每个节点拿到的最多的东西是哪种. 解法:解决树的路径上的修改查询问题一般用到的是树链剖分+线段树,以前不会写,后来学了一下树链剖分,感觉 ...
- BZOJ 2243 染色 | 树链剖分模板题进阶版
BZOJ 2243 染色 | 树链剖分模板题进阶版 这道题呢~就是个带区间修改的树链剖分~ 如何区间修改?跟树链剖分的区间询问一个道理,再加上线段树的区间修改就好了. 这道题要注意的是,无论是线段树上 ...
- 【树状数组 思维题】luoguP3616 富金森林公园
树状数组.差分.前缀和.离散化 题目描述 博艾的富金森林公园里有一个长长的富金山脉,山脉是由一块块巨石并列构成的,编号从1到N.每一个巨石有一个海拔高度.而这个山脉又在一个盆地中,盆地里可能会积水,积 ...
随机推荐
- [JSON].exists( keyPath )
语法:[JSON].exists( keyPath ) 返回:[True | False] 说明:检测指定键名路径是否存在 示例: Set jsonObj = toJson("{div:{' ...
- python 终极篇 --- django 路由系统
URL配置 基本格式: from django.conf.urls import url urlpattern ...
- *.hbm.xml作用是什么
实体与表的映射关系通过XML来描述的文件.在 hibernate.cfg.xml中管理,在项目启动的时候加载到内存. hbm指的是hibernate的映射文件 映射文件也称映射文档,用于向Hibern ...
- Appium基础环境搭建(windows)---基于python
1 JDK安装 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 安装注意:安装 ...
- VBA基础之Excel 工作表(Sheet)的操作(二)
二. Excel 工作表(Sheet)的操作1. Excel 添加工作表(Sheet) 方法名 参数 参数值 说明 Add Before 工作表名称 在指定的工作表前面插入新的工作表 After 工作 ...
- Python学习 - 入门篇1
前言 学习渠道:慕课网:Python入门 记录原因:人总归要向记忆低头[微笑再见.gif] 记录目标:形成简洁的知识点查阅手册 变量和数据类型 变量 赋值 在Python中,可以把任意数据类型赋值给变 ...
- ubuntu上的inpack测试
测试linpack 配置 配置linpack环境是整个过程中最麻烦的,也可能是因为我在配置的过程中出现了很多小问题吧.大概有3天的时间除了上课就在配置环境. 问题 总结起来问题和解决方法有这些 1.路 ...
- lintcode-6-合并排序数组
合并排序数组 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 挑战 你能否优化你的算法,如果其中一个 ...
- python实现post请求
今天无论如何都要留下一些什么东西... 可以说今天学到一个新的一个东西,也需要分享出来,给更多的人去使用. 今天爬取的数据里面是客户端向服务器端发送加密过的token和一些页码之类的一个数据.(我主要 ...
- lol人物模型提取(五)
修改了发过去后,那边说吊坠的绳子太细了,厚度至少1mm,推荐是2mm,需要我自己加粗,没办法又得用3ds max一根一根线地缩放了. 修改好后问报价,高精度树脂打印需要730元,还不带上色的, ...