SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集
【题目分析】
区间开方+区间求和。
由于区间开方次数较少,直接并查集维护下一个不是1的数的位置,然后暴力修改,树状数组求和即可。
这不是BZOJ上上帝造题7分钟嘛
【代码】
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
long long a[100001],f[100001],n,q,op,l,r;
long long t[100001];
inline long long gf(long long k)
{
if (f[k]==k) return k;
else return f[k]=gf(f[k]);
}
inline void add(long long x,long long f)
{
for (;x<=n;x+=x&(-x))
t[x]+=(ll)f;
}
inline long long gs(long long x)
{
long long ret=0;
for (;x;x-=x&(-x)) ret+=t[x];
return ret;
}
int main()
{
int kas=0;
while (scanf("%lld",&n)!=EOF)
{
printf("Case #%d:\n",++kas);
memset(t,0,sizeof t);
for (long long i=1;i<=n;++i) scanf("%lld",&a[i]),f[i]=i;
f[n+1]=n+1;
for (long long i=1;i<=n;++i) add(i,a[i]);
scanf("%lld",&q);
for (long long zz=1;zz<=q;++zz)
{
scanf("%lld",&op);
if (op==0)
{
scanf("%lld%lld",&l,&r);
if (l>r) swap(l,r);
long long i=l;
while (i<=r)
{
i=gf(f[i]);
if (i>r) break;
long long tmp=a[i];
a[i]=(long long)sqrt(a[i]);
add(i,a[i]-tmp);
if (a[i]==1) f[i]=f[i]+1;
gf(f[i]);
i++;
}
}
else
{
scanf("%lld%lld",&l,&r);
if (l>r) swap(l,r);
printf("%lld\n",gs(r)-gs(l-1));
}
}
printf("\n");
}
}
SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集的更多相关文章
- SPOJ GSS4 Can you answer these queries IV
Time Limit: 500MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description You are g ...
- GSS4 - Can you answer these queries IV(线段树懒操作)
GSS4 - Can you answer these queries IV(线段树懒操作) 标签: 线段树 题目链接 Description recursion有一个正整数序列a[n].现在recu ...
- 线段树 SP2713 GSS4 - Can you answer these queries IV暨 【洛谷P4145】 上帝造题的七分钟2 / 花神游历各国
SP2713 GSS4 - Can you answer these queries IV 「题意」: n 个数,每个数在\(10^{18}\) 范围内. 现在有「两种」操作 0 x y把区间\([x ...
- GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 (线段树)
GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 GSS4 - Can you answer these qu ...
- SP2713 GSS4 - Can you answer these queries IV(线段树)
传送门 解题思路 大概就是一个数很少次数的开方会开到\(1\),而\(1\)开方还是\(1\),所以维护一个和,维护一个开方标记,维护一个区间是否全部为\(1/0\)的标记.然后每次修改时先看是否有全 ...
- GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...
- CodeForces - 369E Valera and Queries(树状数组)
CodeForces - 369E Valera and Queries 题目大意:给出n个线段(线段的左端点和右端点坐标)和m个查询,每个查询有cnt个点,要求给出有多少条线段包含至少其中一个点. ...
- 题解【SP2713】GSS4 - Can you answer these queries IV
题目描述 You are given a sequence \(A\) of \(N(N \leq 100,000)\) positive integers. There sum will be le ...
- Spoj 2713 Can you answer these queries IV 水线段树
题目链接:点击打开链接 题意: 给定n长的序列 以下2个操作 0 x y 给[x,y]区间每一个数都 sqrt 1 x y 问[x, y] 区间和 #include <stdio.h> # ...
随机推荐
- freebsd安装snmp
pkg_add http://ip地址/pub/FreeBSD/ports/packages/Latest/net-snmp.tbzFetching http://ip地址/pub/FreeBSD/p ...
- angulajs中引用chart.js做报表,修改线条样式
目前还有个问题,在手机上看,当折线y轴值超过1000,会有点问题 1.下载chart js,可以用bower 命令下载 http://www.chartjs.org/docs/#line-chart- ...
- codevs 2277 爱吃皮蛋的小明(水题日常)
时间限制: 1 s 空间限制: 32000 KB 题目等级 : 白银 Silver 题目描述 Description 小明特别爱吃蛋,特别是皮蛋.他一次可以吃一个蛋或者两个蛋(整个吞下去),而且他 ...
- Caused by: java.lang.NoClassDefFoundError: com/sun/tools/javac/util/List at
折腾了一下的时间,都没有找到解决的方案,在网上搜了一下答案都是让清理编译环境和重新打包之类的.就这样折腾一下,还没有解决问题.之所以会抛出找不到类的问题,需要排查你使用这个包的类是否存在,存在之后 查 ...
- (转)MyBatis框架的学习(七)——MyBatis逆向工程自动生成代码
http://blog.csdn.net/yerenyuan_pku/article/details/71909325 什么是逆向工程 MyBatis的一个主要的特点就是需要程序员自己编写sql,那么 ...
- Navicat连接Oracle详细教程
Navicat Premium算是比较好的一个可视化数据库管理工具了,短小精悍,一个工具解决三种数据库的连接问题,真正做到了集成管理,对MySQL,SQLServer而言,连接比较简单,就不赘述了,现 ...
- soapui测试https双向验证p12项目
1.准备好p12 和jsk秘钥文件 2.配置soapui ssl 其中: 1:jks就是放在trustStore那里,密码填写为 106075 2:p12放到keystore,密码填写:180000 ...
- mac文件夹怎么重命名?苹果电脑文件夹重命名快捷键
windows系统下给文件夹重命名相信很多朋友都很熟悉,那么Mac OS系统怎么给文件重命名呢,相信很多刚刚入手Mac OS系统的亲们都会有次疑问,下面小编告诉你Mac OS系统的文件夹到底要怎样才能 ...
- vue ssr
https://mp.weixin.qq.com/s/v1c69bJ5PxGcqt-ZU4FVXw https://juejin.im/entry/590ca74b2f301e006c10465f h ...
- python爬虫(爬取段子)
python爬取段子 爬取某个网页的段子 第一步 不管三七二十一我们先导入模块 #http://baijiahao.baidu.com/s?id=1598724756013298998&wfr ...