SP3946 K-th Number

(/2和>>1不一样!!)

  1. #include <algorithm>
  2. #include <bitset>
  3. #include <complex>
  4. #include <deque>
  5. #include <exception>
  6. #include <fstream>
  7. #include <functional>
  8. #include <iomanip>
  9. #include <ios>
  10. #include <iosfwd>
  11. #include <iostream>
  12. #include <istream>
  13. #include <iterator>
  14. #include <limits>
  15. #include <list>
  16. #include <locale>
  17. #include <map>
  18. #include <memory>
  19. #include <new>
  20. #include <numeric>
  21. #include <ostream>
  22. #include <queue>
  23. #include <set>
  24. #include <sstream>
  25. #include <stack>
  26. #include <stdexcept>
  27. #include <streambuf>
  28. #include <string>
  29. #include <typeinfo>
  30. #include <utility>
  31. #include <valarray>
  32. #include <vector>
  33.  
  34. #if __cplusplus >= 201103L
  35. #include <array>
  36. #include <atomic>
  37. #include <chrono>
  38. #include <condition_variable>
  39. #include <forward_list>
  40. #include <future>
  41. #include <initializer_list>
  42. #include <mutex>
  43. #include <random>
  44. #include <ratio>
  45. #include <regex>
  46. #include <scoped_allocator>
  47. #include <system_error>
  48. #include <thread>
  49. #include <tuple>
  50. #include <typeindex>
  51. #include <type_traits>
  52. #include <unordered_map>
  53. #include <unordered_set>
  54. #endif
  55. #define INF 1000000007
  56. #define MAXN 200010
  57. using namespace std;
  58. struct Node {
  59. int x,y,a,b,c;
  60. }q[MAXN],a[MAXN],b[MAXN];
  61. int t[MAXN],ans[MAXN],n,m,x,y,z,cnt,k;
  62. inline int read() {
  63. char ch;
  64. bool f=false;
  65. int res=0;
  66. while (((ch=getchar())<'0'||ch>'9')&&ch!='-');
  67. if (ch=='-')
  68. f=true;
  69. else
  70. res=ch-'0';
  71. while ((ch=getchar())>='0'&&ch<='9')
  72. res=(res<<3)+(res<<1)+ch-'0';
  73. return f?~res+1:res;
  74. }
  75. inline int lowbit(int x) {
  76. return x&(-x);
  77. }
  78. inline void add(int x,int y) {
  79. while (x<=n) {
  80. t[x]+=y,x+=lowbit(x);
  81. }
  82. }
  83. inline int sum(int x) {
  84. int summ=0;
  85. while (x>0) {
  86. summ+=t[x],x-=lowbit(x);
  87. }
  88. return summ;
  89. }
  90. inline void Build(int x,int i){
  91. cnt++;
  92. q[cnt].x=x,q[cnt].b=1,q[cnt].c=i;
  93. }
  94. inline void Build1(int x,int y,int k,int i){
  95. cnt++;
  96. q[cnt].x=x,q[cnt].y=y,q[cnt].a=k,q[cnt].b=2,q[cnt].c=i;
  97. }
  98. void sc(int t,int w,int l,int r) {
  99. if (t>=w)
  100. return;
  101. if (l==r) {
  102. for (int i=t;i<=w;++i)
  103. if (q[i].b==2)
  104. ans[q[i].c]=l;
  105. return;
  106. }
  107. int mid=(l+r)>>1,t1=0,w1=0;
  108. for (int i=t;i<=w;++i)
  109. if (q[i].b==1) {
  110. if (q[i].x<=mid)
  111. add(q[i].c,1),a[++t1]=q[i];
  112. else
  113. b[++w1]=q[i];
  114. }
  115. else {
  116. int tw=sum(q[i].y)-sum(q[i].x-1);
  117. if (tw>=q[i].a)
  118. a[++t1]=q[i];
  119. else {
  120. q[i].a=q[i].a-tw;
  121. b[++w1]=q[i];
  122. }
  123. }
  124. for (int i=1;i<=t1;++i)
  125. if (a[i].b==1)
  126. add(a[i].c,-1);
  127. for (int i=1;i<=t1;++i)
  128. q[t+i-1]=a[i];
  129. for (int i=1;i<=w1;++i)
  130. q[t+t1+i-1]=b[i];
  131. sc(t,t+t1-1,l,mid);
  132. sc(t+t1,w,mid+1,r);
  133. }
  134. int main() {
  135. n=read(),m=read();
  136. for (int i=1;i<=n;++i) {
  137. x=read();
  138. Build(x,i);
  139. }
  140. for (int i=1;i<=m;++i) {
  141. x=read(),y=read(),k=read();
  142. Build1(x,y,k,i);
  143. }
  144. sc(1,cnt,-INF,INF);
  145. for (int i=1;i<=m;++i)
  146. printf("%d\n",ans[i]);
  147. return 0;
  148. }

ZOJ 2112 Dynamic Rankings

  1. #include <algorithm>
  2. #include <bitset>
  3. #include <complex>
  4. #include<cstring>
  5. #include <deque>
  6. #include <exception>
  7. #include <fstream>
  8. #include <functional>
  9. #include <iomanip>
  10. #include <ios>
  11. #include <iosfwd>
  12. #include <iostream>
  13. #include <istream>
  14. #include <iterator>
  15. #include <limits>
  16. #include <list>
  17. #include <locale>
  18. #include <map>
  19. #include <memory>
  20. #include <new>
  21. #include <numeric>
  22. #include <ostream>
  23. #include <queue>
  24. #include <set>
  25. #include <sstream>
  26. #include <stack>
  27. #include <stdexcept>
  28. #include <streambuf>
  29. #include <string>
  30. #include <typeinfo>
  31. #include <utility>
  32. #include <valarray>
  33. #include <vector>
  34.  
  35. #if __cplusplus >= 201103L
  36. #include <array>
  37. #include <atomic>
  38. #include <chrono>
  39. #include <condition_variable>
  40. #include <forward_list>
  41. #include <future>
  42. #include <initializer_list>
  43. #include <mutex>
  44. #include <random>
  45. #include <ratio>
  46. #include <regex>
  47. #include <scoped_allocator>
  48. #include <system_error>
  49. #include <thread>
  50. #include <tuple>
  51. #include <typeindex>
  52. #include <type_traits>
  53. #include <unordered_map>
  54. #include <unordered_set>
  55. #endif
  56. #define INF 1000000007
  57. #define MAXN 2000010
  58. using namespace std;
  59. struct Node {
  60. int x,y,a,b,c;
  61. }q[MAXN],a[MAXN],b[MAXN];
  62. int t[MAXN],ans[MAXN],n,m,x,y,z,cnt,k;
  63. int aa[MAXN];
  64. char ch;
  65. int ansn;
  66. inline int read() {
  67. char ch;
  68. bool f=false;
  69. int res=0;
  70. while (((ch=getchar())<'0'||ch>'9')&&ch!='-');
  71. if (ch=='-')
  72. f=true;
  73. else
  74. res=ch-'0';
  75. while ((ch=getchar())>='0'&&ch<='9')
  76. res=(res<<3)+(res<<1)+ch-'0';
  77. return f?~res+1:res;
  78. }
  79. inline int lowbit(int x) {
  80. return x&(-x);
  81. }
  82. inline void add(int x,int y) {
  83. while (x<=n) {
  84. t[x]+=y,x+=lowbit(x);
  85. }
  86. }
  87. inline int sum(int x) {
  88. int summ=0;
  89. while (x>0) {
  90. summ+=t[x],x-=lowbit(x);
  91. }
  92. return summ;
  93. }
  94. inline void Build(int x,int i) {
  95. cnt++;
  96. q[cnt].x=x,q[cnt].b=1,q[cnt].c=i;
  97. }
  98. inline void Build1(int x,int y,int k,int i) {
  99. cnt++,ansn++;
  100. q[cnt].x=x,q[cnt].y=y,q[cnt].a=k,q[cnt].b=0,q[cnt].c=ansn;
  101. }
  102. inline void Build2(int x,int y,int i) {
  103. cnt++;
  104. q[cnt].x=aa[x],q[cnt].b=-1,q[cnt].c=x;
  105. }
  106. inline void Build3(int x,int y,int i) {
  107. cnt++;
  108. q[cnt].x=aa[x],q[cnt].b=1,q[cnt].c=x;
  109. }
  110. void sc(int t,int w,int l,int r) {
  111. if (t>w)
  112. return;
  113. if (l==r) {
  114. for (int i=t;i<=w;++i)
  115. if (q[i].b==0)
  116. ans[q[i].c]=l;
  117. return;
  118. }
  119. int mid=(l+r)>>1,t1=0,w1=0;
  120. for (int i=t;i<=w;++i)
  121. if (q[i].b) {
  122. if (q[i].x<=mid)
  123. /*add(q[i].c,1),*/add(q[i].c,q[i].b),a[++t1]=q[i];
  124. else
  125. b[++w1]=q[i];
  126. }
  127. else {
  128. int tw=sum(q[i].y)-sum(q[i].x-1);
  129. if (tw>=q[i].a)
  130. a[++t1]=q[i];
  131. else {
  132. q[i].a=q[i].a-tw;
  133. b[++w1]=q[i];
  134. }
  135. }
  136. for (int i=1;i<=t1;++i)
  137. if (a[i].b)
  138. add(a[i].c,-a[i].b);
  139. for (int i=1;i<=t1;++i)
  140. q[t+i-1]=a[i];
  141. /*for (int i=1;i<=t1;++i)
  142. printf("%d %d %d %d %d ",a[i].x,a[i].y,a[i].a,a[i].b,a[i].c);
  143. printf("\n");*/
  144. for (int i=1;i<=w1;++i)
  145. q[t+t1+i-1]=b[i];
  146. sc(t,t+t1-1,l,mid);
  147. sc(t+t1,w,mid+1,r);
  148. }
  149. int main() {
  150. int T=read();
  151. while (T--){
  152. memset(q,0,sizeof q);
  153. memset(a,0,sizeof a);
  154. memset(b,0,sizeof b);
  155. memset(ans,0,sizeof ans);
  156. n=read(),m=read(),cnt=0,ansn=0;
  157. for (int i=1;i<=n;++i) {
  158. aa[i]=read();
  159. Build(aa[i],i);
  160. }
  161. for (int i=1;i<=m;++i) {
  162. /*scanf("%c",&ch);*/cin>>ch;x=read(),y=read();
  163. if (ch=='Q'){
  164. k=read();
  165. Build1(x,y,k,i);
  166. }
  167. else {
  168. Build2(x,y,i);
  169. aa[x]=y;
  170. Build3(x,y,i);
  171. }
  172. }
  173. //for (int i=1;i<=cnt;++i)
  174. // printf("%d %d %d %d %d\n",q[i].x,q[i].y,q[i].a,q[i].b,q[i].c);
  175. sc(1,cnt,-INF,INF);
  176. for (int i=1;i<=ansn;++i)
  177. printf("%d\n",ans[i]);
  178. }
  179. return 0;
  180. }

  

整体二分(SP3946 K-th Number ZOJ 2112 Dynamic Rankings)的更多相关文章

  1. 整体二分&cdq分治 ZOJ 2112 Dynamic Rankings

    题目:单点更新查询区间第k大 按照主席树的思想,要主席树套树状数组.即按照每个节点建立主席树,然后利用树状数组的方法来更新维护前缀和.然而,这样的做法在实际中并不能AC,原因即卡空间. 因此我们采用一 ...

  2. 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )

    在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...

  3. ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)

    题目大意 给定一个数列,编号从 1 到 n,现在有 m 个操作,操作分两类: 1. 修改数列中某个位置的数的值为 val 2. 询问 [L, R] 这个区间中第 k 大的是多少 n<=50,00 ...

  4. ZOJ 2112 Dynamic Rankings(带修改的区间第K大,分块+二分搜索+二分答案)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  5. zoj 2112 Dynamic Rankings 动态第k大 线段树套Treap

    Dynamic Rankings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...

  6. ZOJ 2112 Dynamic Rankings (动态第 K 大)(树状数组套主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  7. ZOJ -2112 Dynamic Rankings 主席树 待修改的区间第K大

    Dynamic Rankings 带修改的区间第K大其实就是先和静态区间第K大的操作一样.先建立一颗主席树, 然后再在树状数组的每一个节点开线段树(其实也是主席树,共用节点), 每次修改的时候都按照树 ...

  8. zoj 2112 Dynamic Rankings(主席树&amp;动态第k大)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  9. ZOJ 2112 Dynamic Rankings (动态第k大,树状数组套主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

随机推荐

  1. 系统前端基本文件+ajax部分理解

    静态页面: 一.static: css dist fonts images js model 二.templates: html ajax搜索操作: <html> <head> ...

  2. SQL Server非域(跨域)环境下镜像(Mirror)的搭建步骤及注意事项

    在实际的生产环境下,我们经常需要跨域进行数据备份,而创建Mirror是其中一个方案.但跨域创建Mirror要相对复杂的多,需要借助证书进行搭建. 下面我们将具体的步骤总结如下: 第一部分 创建证书 S ...

  3. 网络编程 单纯UDP通信

    网络编程 单纯UDP通信 1,UDP发送端 2,UDP接收端 UDP发送端: #include <stdio.h> #include <unistd.h> #include & ...

  4. 自动化测试之路3-selenium3+python3环境搭建

    1.首先安装火狐浏览器  有单独文章分享怎么安装 2.搭建python环境 安装python,安装的时候把path选好,就不用自己在配置,安装方法有单独文档分享 安装好以后cmd打开输入python查 ...

  5. Django学习开发--笔记一(从零开始)

    创建django项目注: 首先需在python中下载django 命令:pip install django1.任意文件中创建django项目 diango-admin startproject my ...

  6. SQLServer删除数据列

    删除数据列 开发或者生产过程中多建.错误或者重复的数据列需要进行删除操作. 使用SSMS数据库管理工具删除数据列 方式一 1.打开数据库->选择数据表->展开数据表->展开数据列-& ...

  7. Python面试常见的问题

    So if you are looking forward to a Python Interview, here are some most probable questions to be ask ...

  8. python3 Counter模块

    from collections import Counter c = Counter("周周周周都方法及")print(c)print(type(c))print('__iter ...

  9. Set.js--创建无重复值的无序集合

    Set 集合,不同于 Array,是一种没有重复值的集合. 以下代码出自于<JavaScript 权威指南(第六版)>P217,注意:这里并不是指 es6 / es2015 中的 Set ...

  10. iOS开发基础篇-transform属性

    一. transform 属性 在OC中,通过 transform 属性可以修改对象的平移.缩放比例和旋转角度. 1)创建“基于控件初始位置”的形变  CGAffineTransformMakeRot ...