Description

 

Input

 

Output

每次x=1时,每行一个整数,表示这次旅行的开心度

Sample Input

4

1 100 5 5

5

1 1 2

2 1 2

1 1 2

2 2 3

1 1 4

Sample Output

101

11

11

【分析】
开始看一眼觉得线段树可做。
后来看题解用树状数组瞬秒......orzzz,注意到任何一个int都可以在很小的次数下变为1,所以直接暴力单点修改,将变成1的数parent设为它右边的数。
注意,输入中可能会有很多的0....直接写优化不好会T。
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <map> const int MAXN = + ;
const int MAX = + ;
using namespace std;
typedef long long ll;
int n;
ll C[MAXN];
int data[MAXN], parent[MAXN]; int SQRT(int x){return (int)floor(sqrt((double)x * 1.0));}
int lowbit(int x){return x & -x;}
void add(int x, int val){
while (x <= n){
C[x] += val;
x += lowbit(x);
}
return;
}
ll sum(int x){
ll cnt = ;
while (x > ){
cnt += C[x];
x -= lowbit(x);
}
return cnt;
}
int find(int x) {return parent[x] == x? x : parent[x] = find(parent[x]);}
void init(){
memset(C, , sizeof(C));
scanf("%d", &n);
for (int i = ; i <= n; i++){
scanf("%d", &data[i]);
add(i, data[i]);
parent[i] = i;
if (data[i] == || data[i] == ) parent[i] = i + ;
}
parent[n + ] = n + ;
}
void work(){
int q;
scanf("%d", &q);
for (int i = ; i <= q; i++){
int t, x, y;
scanf("%d%d%d", &t, &x, &y);
if (t == ) printf("%lld\n", sum(y) - sum(x - ));
else{
for (int i = x; i <= y; i = find(i + )){
int tmp = data[i];
data[i] = SQRT(data[i]);
add(i, data[i] - tmp);
if (data[i] <= ) parent[i] = find(i + );
}
}
}
} int main(){
int T;
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
init();
work();
return ;
}

【BZOJ3211】【并查集+树状数组】花神游历各国的更多相关文章

  1. BZOJ-3211花神游历各国 并查集+树状数组

    一开始想写线段树区间开方,简单暴力下,但觉得变成复杂度稍高,懒惰了,编了个复杂度简单的 3211: 花神游历各国 Time Limit: 5 Sec Memory Limit: 128 MB Subm ...

  2. BZOJ3211 花神游历各国 并查集 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3211 题意概括 有n个数形成一个序列. m次操作. 有两种,分别是: 1. 区间开根(取整) 2. ...

  3. hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)

    hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...

  4. 【bzoj4869】[Shoi2017]相逢是问候 扩展欧拉定理+并查集+树状数组

    题目描述 Informatik verbindet dich und mich. 信息将你我连结. B君希望以维护一个长度为n的数组,这个数组的下标为从1到n的正整数.一共有m个操作,可以分为两种:0 ...

  5. 【BZOJ3211】花神游历各国 并查集+树状数组

    [BZOJ3211]花神游历各国 Description Input Output 每次x=1时,每行一个整数,表示这次旅行的开心度 Sample Input 41 100 5 551 1 22 1 ...

  6. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

  7. la4730(并查集+树状数组)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=30& ...

  8. HDU 4750 Count The Pairs ★(图+并查集+树状数组)

    题意 给定一个无向图(N<=10000, E<=500000),定义f[s,t]表示从s到t经过的每条路径中最长的边的最小值.Q个询问,每个询问一个t,问有多少对(s, t)使得f[s, ...

  9. 【BZOJ4382】[POI2015]Podział naszyjnika 堆+并查集+树状数组

    [BZOJ4382][POI2015]Podział naszyjnika Description 长度为n的一串项链,每颗珠子是k种颜色之一. 第i颗与第i-1,i+1颗珠子相邻,第n颗与第1颗也相 ...

随机推荐

  1. Spark RDD Persistence

    Spark最为重要的特性之一就是可以在多个操作(Action)之间,将一个或多个RDD关联的数据集(Dataset)以分区(Partition)为单位进行持久化(Persist)或缓存(Cache), ...

  2. 20140704笔试面试总结(java)

    1.java数组定义 1.与其他高级语言不同,Java在数组声明时并不为数组分配存储空间,因此,在声明的[]中不能指出数组的长度 2.为数组分配空间的两种方法:数组初始化和使用new运算符 3.未分配 ...

  3. word中MathType公式不能 二次编辑解决方案

    问题:当新建文档然后插入公式,此时可以利用mathtype进行编辑,保存后推出第二次打开,双击公式却发现不能编辑公式. 解决方案: ////////////////////////////////// ...

  4. UVa11090 Going in Cycle!!

    UVa11090 Going in Cycle!! 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34650 [思路] ...

  5. ANDROID Porting系列二、配置一个新产品

    ANDROID Porting系列二.配置一个新产品 详细说明 下面的步骤描述了如何配置新的移动设备和产品的makefile运行android. 1.         目录//vendor/创建一个公 ...

  6. lightoj 1030 概率dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 #include<cstdio> #include<cstri ...

  7. DUBBO Thread pool is EXHAUSTED!

    一.问题 在测试环境遇到的异常信息,如下: 16-10-17 00:00:00.033 [New I/O server worker #1-6] WARN  com.alibaba.dubbo.com ...

  8. vs 点击就设置项目为默认启动项

    装好系统没注意 做项目的时候解决方案比较多 发现点击哪个项目哪个项目就成了默认启动项目 这个开始没觉出来 最后发现挺烦人的 想想难道是我装vs装的么 我于是卸载了全新安装了 还是一个吊样 无意间试了下 ...

  9. Unity 读取、写入自定义路径文件,调用System.Windows.Forms

    调用System.Windows.Forms DLL 首先在Unity新建Plugins文件夹添加System.Windows.Forms.dll 然后代码中添加引用 using System; us ...

  10. Linux下安装 TestLink常见问题解决方法

    Read/write permissions For security reason we suggest that directories tagged with [S] on following ...