题目链接:https://codeforces.com/gym/101991/problem/G

题意:给出 n 个数,q 次询问区间[ li,ri ]之间有多少个 GCD = di 的连续子区间。

题解:类似HDU 5726,可以先看一下这个blog:https://blog.csdn.net/u013569304/article/details/51987053

考虑离线,先预处理出[ 1,n ]之间所有的GCD,同时需要记录每种 GCD 的区间,方法是固定一个右端点R,对于区间[ L,R ],假设 GCD(L,R)= D,可以找到使得GCD(L,R)突变的点 pos,即 x ∈ [ L,pos ] 都有 GCD(x,R) = D,然后利用线段树可以统计出 GCD = D 的区间个数。

 #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mst(a,b) memset((a),(b),sizeof(a))
#define mp(a,b) make_pair(a,b)
#define pi acos(-1)
#define pii pair<int,int>
#define pb push_back
const int INF = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = 1e5 + ;
const int maxm = 1e6 + ;
const ll mod = 1e9 + ; int a[maxn]; struct node {
int l,r,id;
}; bool cmp(node x,node y) {
if(x.id != y.id) return x.id < y.id;
return x.l > y.l;
} vector<node>vec[maxm];
vector<pii>now,nex; ll sum[maxn<<],ans[maxn];
int lazy[maxn<<]; inline void init(int rt,int l,int r) {
if(!sum[rt] && !lazy[rt]) return ;
sum[rt] = lazy[rt] = ;
if(l == r) return ;
int mid = (l + r) >> ;
init(rt<<,l,mid);
init(rt<<|,mid + ,r);
} inline void pushdown(int rt,int l,int r) {
if(lazy[rt]) {
int mid = (l + r) >> ;
lazy[rt<<] += lazy[rt];
lazy[rt<<|] += lazy[rt];
sum[rt<<] += 1ll * (mid - l + ) * lazy[rt];
sum[rt<<|] += 1ll * (r - mid) * lazy[rt];
lazy[rt] = ;
}
} inline void update(int rt,int l,int r,int ql,int qr) {
if(ql <= l && qr >= r) {
lazy[rt]++;
sum[rt] += (ll)(r - l + );
return ;
}
pushdown(rt,l,r);
int mid = (l + r) >> ;
if(qr <= mid) update(rt<<,l,mid,ql,qr);
else if(ql > mid) update(rt<<|,mid + ,r,ql,qr);
else {
update(rt<<,l,mid,ql,mid);
update(rt<<|,mid + ,r,mid + ,qr);
}
sum[rt] = sum[rt<<] + sum[rt<<|];
} inline ll query(int rt,int l,int r,int ql,int qr) {
if(ql <= l && qr >= r) return sum[rt];
pushdown(rt,l,r);
int mid = (l + r) >> ;
if(qr <= mid) return query(rt<<,l,mid,ql,qr);
else if(ql > mid) return query(rt<<|,mid + ,r,ql,qr);
else return query(rt<<,l,mid,ql,mid) + query(rt<<|,mid + ,r,mid + ,qr);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#else
freopen("gcdrng.in", "r", stdin);
#endif
int t;
scanf("%d",&t);
while(t--) {
int n,q;
scanf("%d%d",&n,&q);
for(int i = ; i <= n; i++) scanf("%d",&a[i]);
for(int i = ; i <= q; i++) {
int l,r,d;
scanf("%d%d%d",&l,&r,&d);
vec[d].push_back({-i,l,r});
}
nex.clear();
for(int i = ; i <= n; i++) {
now.clear();
now.push_back(mp(a[i],));
for(int j = ; j < nex.size(); j++) {
pii p = nex[j];
int g = __gcd(now[now.size() - ].first,p.first);
if(g == now[now.size() - ].first) now[now.size() - ].second += p.second;
else now.push_back(mp(g,p.second));
}
int r = i;
for(int j = ; j < now.size(); j++) {
pii p = now[j];
vec[p.first].push_back({r - p.second + ,r,i});
r -= p.second;
}
nex = now;
}
for(int i = ; i <= 1e6; i++) {
init(,,n);
sort(vec[i].begin(),vec[i].end(),cmp);
for(int j = ; j < vec[i].size(); j++) {
node p = vec[i][j];
if(p.l > ) update(,,n,p.l,p.r);
else ans[-p.l] = query(,,n,p.r,p.id);
}
vec[i].clear();
}
for(int i = ; i <= q; i++) printf("%lld\n",ans[i]);
}
return ;
}

2018 Arab Collegiate Programming Contest (ACPC 2018) G. Greatest Chicken Dish (线段树+GCD)的更多相关文章

  1. 2018 Arab Collegiate Programming Contest (ACPC 2018) E - Exciting Menus AC自动机

    E - Exciting Menus 建个AC自动机求个fail指针就好啦. #include<bits/stdc++.h> #define LL long long #define fi ...

  2. 2018 Arab Collegiate Programming Contest (ACPC 2018) H - Hawawshi Decryption 数学 + BSGS

    H - Hawawshi Decryption 对于一个给定的生成数列 R[ 0 ] 已知, (R[ i - 1 ] * a + b) % p = R[ i ] (p 是 质数), 求最小的 x 使得 ...

  3. 2018 German Collegiate Programming Contest (GCPC 18)

    2018 German Collegiate Programming Contest (GCPC 18) Attack on Alpha-Zet 建树,求lca 代码: #include <al ...

  4. (寒假GYM开黑)2018 German Collegiate Programming Contest (GCPC 18)

    layout: post title: 2018 German Collegiate Programming Contest (GCPC 18) author: "luowentaoaa&q ...

  5. 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举

    2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举 ...

  6. (寒假GYM开黑)2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)

    layout: post title: 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) author: &qu ...

  7. 2018 China Collegiate Programming Contest Final (CCPC-Final 2018)-K - Mr. Panda and Kakin-中国剩余定理+同余定理

    2018 China Collegiate Programming Contest Final (CCPC-Final 2018)-K - Mr. Panda and Kakin-中国剩余定理+同余定 ...

  8. 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)-E. Explosion Exploit-概率+状压dp

    2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)-E. Explosion Exploit-概率+状压dp [P ...

  9. The Ninth Hunan Collegiate Programming Contest (2013) Problem G

    Problem G Good Teacher I want to be a good teacher, so at least I need to remember all the student n ...

随机推荐

  1. 使用redis调用lua脚本的方式对接口进行限流

    java端实现: //初始化一个redis可执行的lua DefaultRedisScript<List> defaultRedisScript = new DefaultRedisScr ...

  2. pytorch1.0神经网络保存、提取、加载

    pytorch1.0网络保存.提取.加载 import torch import torch.nn.functional as F # 包含激励函数 import matplotlib.pyplot ...

  3. C++ 配置文件解析类 ParseConfig

    依赖项: 依赖于 ProcessString 类,可从该篇博客获取「字符串处理类 ProcessString (包含常用字符串处理函数)」 ParseConfig.h //Linux & C+ ...

  4. Jmeter-后置处理器--json提取器

    Token提取: 将token放入全局变量: 将token值设为全局变量,${__setProperty(newtoken,${token},)}  添加请求头部管理器作为全局使用,将变量token使 ...

  5. PAT甲级题分类汇编——计算

    本文为PAT甲级分类汇编系列文章. 计算类,指以数学运算为主或为背景的题. 题号 标题 分数 大意 1058 A+B in Hogwarts 20 特殊进制加法 1059 Prime Factors ...

  6. Python3遍历指定文件夹下所有文件及文件夹

    采用os模块儿: import os def get_filelist(dir): for home, dirs, files in os.walk(dir): print("####### ...

  7. 处理ajax数据;数据渲染(细节)

    AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. 什么是 AJAX ? AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通 ...

  8. Hibernate持久化,生命周期

    一 .生命周期   1.1  . 说明 持久化类就是我们所说的实体类,实体类(持久化类)对象是有状态的. 为什么实体类对象会有状态? 答:由于HIbernate框架是一个先映射,后操作的框架.所谓的状 ...

  9. AdventureWorks 安装和配置[转自 微软msdn]

    AdventureWorks 安装和配置 2018/06/19 适用对象:SQL ServerAzure SQL 数据库Azure SQL 数据仓库并行数据仓库 AdventureWorks 下载链接 ...

  10. windows + Eclipse 汉化

    https://www.eclipse.org/babel/downloads.php 下载Eclipse 对应版本 汉化包解压 复制文件夹里的内容到eclipse 文件夹下对应的文件里 重启ecli ...