题目链接:https://ac.nowcoder.com/acm/contest/1084/B

题意

5e5的区间,5e5个询求[l,r]区间内出现过的数的和

思路

1s时限,莫队显然会T
我们可以将询问按r排序,维护每个数最后出现的位置,并用树状数组维护前缀和即可

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>
#include<unordered_map>#define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1using namespace std;typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;const db eps = 1e-6;
const int mod = 998244353;
const int maxn = 5e5+100;
const int maxm = 2e6+100;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0);int n,m;
int a[maxn];
ll tree[maxn];
int lowbit(int x){return x&(-x);}
void add(int x, ll c){for(int i = x; i <= n; i+=lowbit(i)){tree[i]+=c;}
}
ll sum(int x){ll ans = 0;for(int i = x; i; i-=lowbit(i)){ans+=tree[i];}return ans;
}
struct node{int l, r;int id;node(){}node(int l, int r, int id):l(l),r(r),id(id){}
}q[maxn];
int cnt[maxn];
int id[maxn];
ll ans[maxn];
bool cmp(node a, node b){return a.r<b.r;}
int main(){scanf("%d %d", &n, &m);for(int i = 1; i <= n; i++){scanf("%d", &a[i]);}for(int i = 1; i <= m; i++){int l,r;scanf("%d %d", &l, &r);q[i]=node(l,r,i);}sort(q+1,q+1+m,cmp);int lst = 1;for(int i = 1; i <= m; i++){for(int j = lst; j <= q[i].r; j++){if(id[a[j]]){add(id[a[j]],-a[j]);}id[a[j]]=j;add(j,a[j]);}lst=q[i].r+1;ans[q[i].id]=sum(q[i].r)-sum(q[i].l-1);}for(int i = 1; i <= m; i++){printf("%lld\n",ans[i]);}return 0;
}
/*
3 3
1 1 1
1 3
2 3
2 2 */

转载于:https://www.cnblogs.com/wrjlinkkkkkk/p/11527631.html

牛客练习赛52 B Galahad (树状数组)相关推荐

  1. 牛客练习赛52 B.Galahad (树状数组)

    B.Galahad 链接:https://ac.nowcoder.com/acm/contest/1084/B 来源:牛客网 题目描述 魔女要测试骑士的能力,要求他维护一个长度为 的序列,每次要询问一 ...

  2. 牛客练习赛69E-子串【树状数组】

    正题 题目链接:https://ac.nowcoder.com/acm/contest/7329/E 题目大意 给出一个nnn的排列,求有多少个区间[l,r][l,r][l,r]使得最大值是rrr,最 ...

  3. 牛客 - tokitsukaze and Inverse Number(树状数组+逆序对定理)

    题目链接:点击查看 题目大意:给出一个长度为 n 的排列 a,需要执行 q 次操作,每次操作会将区间 [ l , r ] 内的数循环右移 k 次,现在需要回答每次操作后排列的逆序对数,只需要回答奇偶即 ...

  4. 牛客竞赛数据结构专题班树状数组、线段树练习题

    F.little w and Discretization 题意:找区间[l,r]内离散化后和原来的值不同大小的数的个数 思路:先求区间mex,同时记录区间有多少个数,再 用区间长度减去(区间内小于m ...

  5. 牛客练习赛52 | C | [烹饪] (DP,裴蜀定理,gcd)

    牛客练习赛52 C 烹饪 链接:https://ac.nowcoder.com/acm/contest/1084/C来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 327 ...

  6. 牛客练习赛52.Galahad(树状数组维护区间不相同数的和)

    链接:https://ac.nowcoder.com/acm/contest/1084/B 来源:牛客网 Galahad 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K ...

  7. 牛客练习赛52 B:Galahad(树状数组维护区间不同元素和(个数))

    [题目] 查询区间和,如果区间元素重复出现则计数一次. [题解] 按区间的右端点建立树状数组,维护区间[1,R]的每个元素的最右位置.按查询区间的右端点排序,依次处理,每次更新当前值的最右位置即可. ...

  8. 牛客练习赛52 BGalahad 树状数组

    传送门 题意: 求一个区间的和,但如果某一个数在这个区间出现了多次,这个数只能被计算一次. 官方题解:按右端点从小到大排序.建立树状数组ccc,维护贡献的前缀和. 由于权值ai 满足1≤ai≤500  ...

  9. 计蒜客-青出于蓝胜于蓝 dfs+树状数组

    题目描述: 武当派一共有 n人,门派内 n 人按照武功高低进行排名,武功最高的人排名第 1,次高的人排名第 2,... 武功最低的人排名第 n.现在我们用武功的排名来给每个人标号,除了祖师爷,每个人都 ...

最新文章

  1. 磁珠 磁环 双向二极管 TVS二极管
  2. 近期活动盘点:三创对接会——先进制造专场
  3. [Swift]LeetCode206. 反转链表 | Reverse Linked List
  4. 怎么用PHP修改文字大小,如何利用PHP和CSS改变网页文字大小
  5. Gartner数据劲爆:阿里全球第三,华为中国第二!
  6. 数据结构基础(18) --哈希表的设计与实现
  7. 工作笔记-关于工具函数的编写问题
  8. project 模板_施工进度横道图模板,全套电子版,工作效率大大提高!
  9. keepalived nginx 双机热备图文讲解
  10. 38个Pandas实用技巧
  11. ipop映射到ftp服务器,设备作为FTP客户端进行文件传输-IPOP
  12. 中科大一所学校撑起中国人工智能半壁江山
  13. 计算机报名无法支付怎么弄,软考官网报名成功了提示不能进行网上支付
  14. 2017年英语六级翻译
  15. 成功入园啦~ BoomShakalaka
  16. 纯C语言实现动态爱心(详解,初学者也能看懂)
  17. selenium自动登录知网下载论文
  18. Mysql中INSTANT使用
  19. 出线资格 finals berth
  20. BIOS密码清除方法--unlock6的使用.

热门文章

  1. 多源数据 单源数据是什么意思
  2. html5考试总结300字,期末考试总结反思300字
  3. php 对文件加密,PHP文件加密
  4. Android 系统序列号从哪里来,以及客制化序列号
  5. 如何在Debian 9上安装和使用Docker
  6. concurrent.futures调研
  7. 通用搜索引擎的垂直化倾向
  8. 网页动态效果——随鼠标移动的动态触击式线条
  9. 对浏览器内核的理解和常见的浏览器内核
  10. RH850从0搭建Autosar开发环境系列讲解 - 总目录