题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5634

题意:给定一个长度为n的序列,有m次操作。操作有3种:

1 l,r :区间[l,r]的值变成phi[val[i]](l<=i<=r; phi是欧拉值)

2 l,r,x:区间[l,r]的值变成x

3 l,r:求区间[l,r]的和

思路:操作2和3就是传统的简单线段树,操作2对应区间覆盖,操作3对应区间求和,重点在于操作1,由于一个数经过不超过log次求phi后会变成1,所以可以在线段树是用一个same标记,如果整个区间的数都相同则操作1就转换成操作2的区间覆盖了。如果操作的区间[l,r]已经包含住当前递归的子树区间但是子树的same标记为假则继续递归到子树的same标记为真为止,最多递归到叶子结点。

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<time.h>
#include<cmath>
using namespace std;
typedef long long int LL;
#define L(k)(k<<1)
#define R(k)(k<<1|1)
const LL INF = ;
const int MAXN = 3e5 + ;
const int MAXX = 1e7 + ;
struct Node{
int l, r, val;
LL sum;
bool same;
Node(int _l = , int _r = , int _val = , LL _sum = , bool _same = false){
l = _l; r = _r; sum = _sum; same = _same; val = _val;
}
}Seg[MAXN * ];
int val[MAXN], Phi[MAXX];
void pushUp(int k){
Seg[k].sum = Seg[L(k)].sum + Seg[R(k)].sum;
if (Seg[L(k)].val == Seg[R(k)].val&&Seg[L(k)].same&&Seg[R(k)].same&&Seg[L(k)].val != -){
Seg[k].same = true;
Seg[k].val = Seg[L(k)].val;
}
else{
Seg[k].same = false;
Seg[k].val = -;
}
}
void pushDown(int k){
if (Seg[k].same){
Seg[L(k)].same = Seg[R(k)].same = true;
Seg[L(k)].val = Seg[R(k)].val = Seg[k].val;
Seg[L(k)].sum = 1LL * (Seg[L(k)].r - Seg[L(k)].l + )*Seg[L(k)].val;
Seg[R(k)].sum = 1LL * (Seg[R(k)].r - Seg[R(k)].l + )*Seg[R(k)].val;
}
}
void Build(int st, int ed, int k){
Seg[k].l = st; Seg[k].r = ed; Seg[k].same = false; Seg[k].val = -;
if (st == ed){
Seg[k].val = val[st];
Seg[k].sum = val[st];
Seg[k].same = true;
return;
}
int mid = (st + ed) >> ;
Build(st, mid, L(k)); Build(mid + , ed, R(k));
pushUp(k);
}
void Change(int st, int ed, int val, int k){
if (Seg[k].l == st&&Seg[k].r == ed){
Seg[k].same = true;
Seg[k].val = val;
Seg[k].sum = 1LL * (ed - st + )*val;
return;
}
pushDown(k);
if (Seg[L(k)].r >= ed){
Change(st, ed, val, L(k));
}
else if (Seg[R(k)].l <= st){
Change(st, ed, val, R(k));
}
else{
Change(st, Seg[L(k)].r, val, L(k));
Change(Seg[R(k)].l, ed, val, R(k));
}
pushUp(k);
}
void Modify(int st, int ed, int k){
if (Seg[k].l == st&&Seg[k].r == ed&&Seg[k].same){
Seg[k].val = Phi[Seg[k].val];
Seg[k].sum = 1LL * (ed - st + )*Seg[k].val;
return;
}
pushDown(k);
if (Seg[L(k)].r >= ed){
Modify(st, ed, L(k));
}
else if (Seg[R(k)].l <= st){
Modify(st, ed, R(k));
}
else{
Modify(st, Seg[L(k)].r, L(k));
Modify(Seg[R(k)].l, ed, R(k));
}
pushUp(k);
}
LL Query(int st, int ed, int k){
if (Seg[k].l == st&&Seg[k].r == ed){
return Seg[k].sum;
}
pushDown(k);
if (Seg[L(k)].r >= ed){
return Query(st, ed, L(k));
}
else if (Seg[R(k)].l <= st){
return Query(st, ed, R(k));
}
else{
return Query(st, Seg[L(k)].r, L(k)) + Query(Seg[R(k)].l, ed, R(k));
}
pushUp(k);
}
void GetPhi(){ //预处理欧拉值
memset(Phi, , sizeof(Phi));
Phi[] = ;
for (LL i = ; i < MAXX; i++){
if (!Phi[i]){
for (LL j = i; j < MAXX; j += i){
if (!Phi[j]) Phi[j] = j;
Phi[j] = Phi[j] / i*(i - );
}
}
}
}
int main(){
//#ifdef kirito
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
// int start = clock();
int n, t, m; GetPhi();
scanf("%d", &t);
while (t--){
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++){
scanf("%d", &val[i]);
}
Build(, n, );
for (int i = ; i <= m; i++){
int tpe, l, r, x;
scanf("%d%d%d", &tpe, &l, &r);
if (tpe == ){
Modify(l, r, );
}
else if (tpe == ){
scanf("%d", &x);
Change(l, r, x, );
}
else{
printf("%lld\n", Query(l, r, ));
}
}
}
//#ifdef LOCAL_TIME
// cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
return ;
}

HDU 5634 线段树的更多相关文章

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

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

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

  3. hdu 3436 线段树 一顿操作

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

  4. hdu 3397 线段树双标记

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

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

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

  6. hdu 4533 线段树(问题转化+)

    威威猫系列故事——晒被子 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  7. hdu 2871 线段树(各种操作)

    Memory Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  8. hdu 4052 线段树扫描线、奇特处理

    Adding New Machine Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. hdu 1542 线段树扫描(面积)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

随机推荐

  1. hdu 6055 : Regular polygon (2017 多校第二场 1011) 【计算几何】

    题目链接 有个结论: 平面坐标系上,坐标为整数的情况下,n个点组成正n边形时,只可能组成正方形. 然后根据这个结论来做. 我是先把所有点按照 x为第一关键字,y为第二关键字 排序,然后枚举向量 (p[ ...

  2. File类常用的方法与字节流类方法简介

    File类常用的方法 获取功能的方法 public String getAbsolutePath() :返回此File的绝对路径名字符串. public String getPath() :将此Fil ...

  3. Proto3语法翻译

    本文主要对proto3语法翻译.参考网址:https://developers.google.com/protocol-buffers/docs/proto3 defining a message t ...

  4. Jenkins必备插件

    1.汉化插件 https://plugins.jenkins.io/localization-zh-cn 2.邮件发送 https://plugins.jenkins.io/email-ext 3.G ...

  5. Incorporating ASP.NET MVC and SQL Server Reporting Services, Part 2

    In the last issue, I introduced you to the basics of incorporating SQL Server Reporting Services int ...

  6. 学习笔记11 EF查询相当于sql 中的 where in

    两种写法 1. int[] Ids={1,2,3} DBContainer db=new DBContainer(); var list=db.表明.where(a=>Ids.Contains( ...

  7. web复制到剪切板js

    web复制到剪切板 clipboard.js 好使!开源项目,下载地址: https://github.com/zenorocha/clipboard.js 使用方法: 引入 clipboard.mi ...

  8. p5342 [TJOI2019]甲苯先生的线段树

    分析  代码 #include<bits/stdc++.h> using namespace std; #define int long long ],yy[],cnt1,cnt2; ][ ...

  9. http代理工具delphi源码

    http://www.caihongnet.com/content/xingyexinwen/2013/0721/730.html http代理工具delphi源码 以下代码在 DELPHI7+IND ...

  10. Using Xmanager to connect to remote CentOS 7 via XDMCP

    Gnome in CentOS 7 tries to use local hardware acceleration and this becomes a problem when trying to ...