[POI2015]Logistyka

题目大意:

一个长度为\(n(n\le10^6)\)的数列\(A_i\),初始全为\(0\)。操作共\(m(m\le10^6)\)次,包含以下两种:

  1. 将\(A_x\)修改为\(y\);
  2. 询问若每次任意选择\(x\)个正数,将它们\(-1\),能否操作\(y\)次。

思路:

用\(cnt\)表示不小于\(y\)的数的个数,\(sum\)表示小于\(y\)的数之和。

一个结论是,当\(sum\ge(x-cnt)\times y\)时可行,否则不可行。

证明参考Claris博客

时间复杂度\(\mathcal O(m\log n)\)。

源代码:

#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
inline char getalpha() {
register char ch;
while(!isalpha(ch=getchar()));
return ch;
}
const int N=1e6+1,M=1e6;
typedef long long int64;
struct Query {
bool opt;
int x,y;
};
Query q[M];
int tmp[N],w[N];
class SegmentTree {
#define _left <<1
#define _right <<1|1
#define mid ((b+e)>>1)
private:
int64 val[2][N<<2],*cnt,*sum;
void push_up(const int &p) {
cnt[p]=cnt[p _left]+cnt[p _right];
sum[p]=sum[p _left]+sum[p _right];
}
public:
SegmentTree() {
cnt=val[0];
sum=val[1];
}
void modify(const int &p,const int &b,const int &e,const int &x,const int &y) {
if(b==e) {
cnt[p]+=y;
sum[p]+=tmp[x]*y;
return;
}
if(x<=mid) modify(p _left,b,mid,x,y);
if(x>mid) modify(p _right,mid+1,e,x,y);
push_up(p);
}
int64 query(const int &p,const int &b,const int &e,const int &l,const int &r,const bool &t) const {
if(b==l&&e==r) return val[t][p];
int64 ret=0;
if(l<=mid) ret+=query(p _left,b,mid,l,std::min(mid,r),t);
if(r>mid) ret+=query(p _right,mid+1,e,std::max(mid+1,l),r,t);
return ret;
}
#undef _left
#undef _right
#undef mid
};
SegmentTree t;
int main() {
const int n=getint(),m=getint();
for(register int i=0;i<m;i++) {
const char opt=getalpha();
const int x=getint(),y=getint();
q[i]=(Query){opt=='Z',x,y};
tmp[++tmp[0]]=y;
}
std::sort(&tmp[1],&tmp[tmp[0]]+1);
tmp[0]=std::unique(&tmp[1],&tmp[tmp[0]]+1)-&tmp[1];
for(register int i=0;i<m;i++) {
q[i].y=std::lower_bound(&tmp[1],&tmp[tmp[0]]+1,q[i].y)-tmp;
const int &x=q[i].x,&y=q[i].y;
if(q[i].opt) {
const int cnt=t.query(1,1,tmp[0],y,tmp[0],0);
const int64 sum=y!=1?t.query(1,1,tmp[0],1,y-1,1):0;
puts(sum>=(int64)(x-cnt)*tmp[y]?"TAK":"NIE");
} else {
if(w[x]) t.modify(1,1,tmp[0],w[x],-1);
w[x]=y;
if(w[x]) t.modify(1,1,tmp[0],w[x],1);
}
}
return 0;
}

[POI2015]Logistyka的更多相关文章

  1. 【BZOJ4378】[POI2015]Logistyka 树状数组

    [BZOJ4378][POI2015]Logistyka Description 维护一个长度为n的序列,一开始都是0,支持以下两种操作:1.U k a 将序列中第k个数修改为a.2.Z c s 在这 ...

  2. BZOJ4378 : [POI2015]Logistyka

    对于每个询问,设不小于$s$的个数为$cnt$,小于$s$的和为$sum$. 那么如果可以进行$s$轮,当且仅当$sum\geq (c-cnt)\times s$. 权值线段树维护,时间复杂度$O(m ...

  3. BZOJ4378[POI2015]Logistyka——树状数组

    题目描述 维护一个长度为n的序列,一开始都是0,支持以下两种操作:1.U k a 将序列中第k个数修改为a.2.Z c s 在这个序列上,每次选出c个正数,并将它们都减去1,询问能否进行s次操作.每次 ...

  4. bzoj 4378: [POI2015]Logistyka ——树桩数组+离散化

    Description 维护一个长度为n的序列,一开始都是0,支持以下两种操作:1.U k a 将序列中第k个数修改为a.2.Z c s 在这个序列上,每次选出c个正数,并将它们都减去1,询问能否进行 ...

  5. 【bzoj4378】[POI2015]Logistyka 离散化+树状数组

    题目描述 维护一个长度为n的序列,一开始都是0,支持以下两种操作:1.U k a 将序列中第k个数修改为a.2.Z c s 在这个序列上,每次选出c个正数,并将它们都减去1,询问能否进行s次操作.每次 ...

  6. @bzoj - 4378@ [POI2015] Logistyka

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 维护一个长度为 n 的序列,一开始都是 0,支持以下两种操作: ...

  7. [Poi2015]

    [POI2015]Łasuchy 一看以为是sb题 简单来说就是每个人获得热量要尽量多 不能找别人 首先这道题好像我自己找不到NIE的情况 很容易想到一个优化 如果一个数/2>另一个数 那么一定 ...

  8. POI2015题解

    POI2015题解 吐槽一下为什么POI2015开始就成了破烂波兰文题目名了啊... 咕了一道3748没写打表题没什么意思,还剩\(BZOJ\)上的\(14\)道题. [BZOJ3746][POI20 ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. Spring Boot1.5X升级到2.0

    配置文件 大量的Servlet专属的server.* properties被移到了server.servlet下 拦截器 public class MyWebMvcConfigurerAdapter ...

  2. Java学习笔记——继承、接口、多态

    浮点数的运算需要注意的问题: BigDecimal operand1 = new BigDecimal("1.0"); BigDecimal operand2 = new BigD ...

  3. 当遇到not a dynamic executable时怎么做

    当我使用ldd查找Drcom所缺少的32为库的时候提示not a dynamic executable 最后网上找到答案 来自http://forum.ubuntu.org.cn/viewtopic. ...

  4. Android Build.VERSION.SDK_INT兼容介绍

    尽管Android向下兼容不好,但是一个程序还是可以在多个平台上跑的.向下兼容不好,接口改变,新的平台上不能用旧的API,旧的平台更不可能用新的API,不等于一个平台需要一个APK.可以在高SDK上开 ...

  5. XSS练习小游戏和答案参考

    源码:https://files.cnblogs.com/files/Eleven-Liu/xss%E7%BB%83%E4%B9%A0%E5%B0%8F%E6%B8%B8%E6%88%8F.zip 感 ...

  6. 10 The Go Programming Language Specification go语言规范 重点

    The Go Programming Language Specification go语言规范 Version of May 9, 2018 Introduction 介绍 Notation 符号 ...

  7. Java Web 1-开发环境搭建(未完待续)

    Java Web包含什么?前台.后台,前台的HTML.JSP,后台的Servlet.(目前所知) 开发环境: JDK,MySQL,Tomcat,Eclipse @ Windows 10 说明:本文总结 ...

  8. yum命令安装软件时,出现--centos 7 安装apache 出现 Could not resolve host: mirrorlist.centos.org; 未知的错误"--CentOS网络设置 couldn't resolve host 'mirrorlist.centos.org问题解决

    CentOS网络设置 couldn't resolve host 'mirrorlist.centos.org问题解决 今天在虚拟机上安装完CentOS6.5之后,首次使用时yum命令安装软件时,出现 ...

  9. 洛谷P3367并查集

    传送门 #include <iostream> #include <cstdio> #include <cstring> #include <algorith ...

  10. (转)链接服务器——获取EXCEL数据

    测试目的:验证利用链接服务器.分布式查询获取EXCEL中的数据测试环境:Microsoft SQL Server 2005 - 9.00.3080.00 (X64)  Enterprise Editi ...