Gym 100960G (set+树状数组)
Problem Youngling Tournament
题目大意
给一个序列a[i],每次操作可以更改一个数,每次询问 将序列排序后有多少个数a[i]>=sum[i-1]。
n<=10^5,q<=5*10^4,a[i]<=10^12
解题分析
可以发现,在最优情况下,该序列长度最多为logn。
将询问离线后,用multiset来维护a[i],用树状数组来维护sum[i]。树状数组所存下标为离散化后的a[i]。
每次查询时跳跃式地在set中查找。
时间复杂度 O((n+m)log(n+m)+mlognlogn)
参考程序
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std; #define N 100008
#define M 50008
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define clr(x,v) memset(x,v,sizeof(x));
#define bitcnt(x) __builtin_popcount(x)
#define rep(x,y,z) for (int x=y;x<=z;x++)
#define repd(x,y,z) for (int x=y;x>=z;x--)
const int mo = ;
const int inf = 0x3f3f3f3f;
const int INF = ;
/**************************************************************************/ int n,m,tot,flag;
LL a[N],c[N],x[N*];
int b[N];
multiset<long long> S;
struct Binary_Indexed_Tree{
LL a[N*];
void clear(){
clr(a,);
}
void insert(int x,LL val){
for (int i=x;i<=N<<;i+=i & (-i))
a[i]+=val;
}
LL sigma(int x){
LL res=;
for (int i=x;i>;i-=i & (-i))
res+=a[i];
return res;
}
LL query(int x,int y){
return sigma(y)-sigma(x-);
}
}T;
int id(LL xy){
return lower_bound(x+,x+tot+,xy)-x;
}
set <long long> :: iterator it,it1;
void query(){
int ans=; LL tmp;
it = S.begin();
it1 = it ; it1++;
if (*it==*it1) ans++;
tmp=T.sigma(id(*it));
while (){
it = S.lower_bound(tmp);
if (it==S.begin()) it++;
if (it==S.end()) break;
it1 = it; it1--;
tmp=T.sigma(id(*it1));
if (*it>=tmp) ans++;
tmp=T.sigma(id(*it));
}
printf("%d\n",ans); } int main(){
scanf("%d",&n);
rep(i,,n){
scanf("%lld",&a[i]);
x[++tot]=a[i];
}
scanf("%d",&m);
rep(i,,m){
scanf("%d%lld",&b[i],&c[i]);
x[++tot]=c[i];
}
sort(x+,x+tot+);
tot=unique(x+,x+tot+)-x;
T.clear();
rep(i,,n){
T.insert(id(a[i]),a[i]);
S.insert(a[i]);
}
query();
rep(i,,m){
S.erase(S.find(a[b[i]]));
T.insert(id(a[b[i]]),-a[b[i]]);
S.insert(c[i]);
T.insert(id(c[i]),c[i]);
a[b[i]]=c[i];
query();
} }
Gym 100960G (set+树状数组)的更多相关文章
- Gym - 101755G Underpalindromity (树状数组)
Let us call underpalindromity of array b of length k the minimal number of times one need to increme ...
- CF Gym 100463A (树状数组求逆序数)
题意:给你一个序列,和标准序列连线,求交点数. 题解:就是求逆序对个数,用树状数组优化就行了.具体过程就是按照顺序往树状数组了插点(根据点的大小),因为第i大的点应该排在第i位,插进去的时候他前面本该 ...
- GYM 100741A Queries(树状数组)
A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...
- GYM 101889F(树状数组)
bit扫描坐标套路题,注意有重复的点,莽WA了. const int maxn = 1e5 + 5; struct node { ll B, F, D; bool operator < (con ...
- Codeforces Gym 100114 H. Milestones 离线树状数组
H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...
- Gym 101908C - Pizza Cutter - [树状数组]
题目链接:https://codeforces.com/gym/101908/problem/C 题意: 一块正方形披萨,有 $H$ 刀是横切的,$V$ 刀是竖切的,不存在大于等于三条直线交于一点.求 ...
- Codeforces Gym 100269F Flight Boarding Optimization 树状数组维护dp
Flight Boarding Optimization 题目连接: http://codeforces.com/gym/100269/attachments Description Peter is ...
- Gym - 100269F Flight Boarding Optimization(dp+树状数组)
原题链接 题意: 现在有n个人,s个位置和你可以划分长k个区域你可以把s个位置划分成k个区域,这样每个人坐下你的代价是该区域内,在你之前比你小的人的数量问你怎么划分这s个位置(当然,每个区域必须是连续 ...
- 【容斥原理】【推导】【树状数组】Gym - 101485G - Guessing Camels
题意:给你三个1~n的排列a,b,c,问你在 (i,j)(1<=i<=n,1<=j<=n,i≠j),有多少个有序实数对(i,j)满足在三个排列中,i都在j的前面. 暴力求的话是 ...
随机推荐
- Entity Framework 第九篇 关于自增列的事务处理
如果一个表带有自增列的,那么在事务处理的过程中,如果抑制了提交,自增的序号就不会得到,如果我们需要得到那怎么办呢?可以临时提交,但是既然提交了就要考虑到事务回滚,否则无法满足数据的一致性 public ...
- Android官方数据绑定框架DataBinding
数据绑定框架给我们带来了更大的方便性,以前我们可能需要在Activity里写很多的findViewById,烦人的代码也增加了我们代码的耦合性,现在我们马上就可以抛弃那么多的findViewById. ...
- Java垃圾回收以及内存分配
http://www.cnblogs.com/ggjucheng/p/3977384.html http://www.blogjava.net/qcyycom/archive/2012/04/14/3 ...
- 对css中clear元素的理解
clear:left;表示左侧不能有浮动元素. clear:right;表示右侧不能有浮动元素. clear:both;表示左右两侧都不能有浮动元素. 但在使用时,还得考虑css优先级问题.相同类型选 ...
- c++ ado 调用存储过程并得到输出参数和返回值
// AccessSqlserverByAdo.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h ...
- lua中string.find()函数作用于汉字字符串
lua中有这样一个库函数,string,find(),作用是在一个字符串中找到目标字符串的起始和结束位置(从1开始计数) 如:a,b=string.find("hello world&quo ...
- 忙了好一阵,今天随便写篇关于canvas的小东西
前几天在朋友圈发了几条3D demo视频,其中就有3D空间组成各种图形.如上! 那么这些图形的每个mesh的坐标可不是手动去写,如果你愿意我当然不拦着!所以今天这篇就来介绍如何获得这些图形的坐标数据. ...
- 关于xcode不同版本打开相同工程问题
今天刚下好了xcode7正式版,于是乎用其创建一个工程.随后关闭此工程用xcode6.3打开此工程.发现报错不能运行,随后网上查资料,可惜中文版的资料几乎可以说是没有,因此写下此文,以方便其他遇到此情 ...
- GIT warning: LF will be replaced by CRLF.
git config --global core.autocrlf false git config --global core.autocrlf false
- i2c设备驱动移植笔记(二)
说明:上一篇博客写了我在移植android驱动之TEF6606的苦逼遭遇,即驱动层向应用层提供接口支持,查找了两天的资料,不得不放弃,转而进行IIC下移植RTC设备的实验. 第一步:查找设备的数据手册 ...