死活TLE....求助

update 4.3 23:08 求助了tls之后终于过了...分治里次数界写崩了...r-l+1就行...

分治的做法很神奇!本题的限制在于操作类型与权值相对大小有关,而用[l,mid]更新[mid+1,r]正好适应了本题的要求

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. using namespace std;
  7. typedef long long ll;
  8. const int N = (1<<17) + 5;
  9. const double PI = acos(-1.0);
  10. inline int read() {
  11. char c=getchar();int x=0,f=1;
  12. while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
  13. while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
  14. return x*f;
  15. }
  16. struct meow{
  17. double x, y;
  18. meow(double x=0, double y=0):x(x), y(y){}
  19. };
  20. meow operator + (meow a, meow b) {return meow(a.x + b.x, a.y + b.y);}
  21. meow operator - (meow a, meow b) {return meow(a.x - b.x, a.y - b.y);}
  22. meow operator * (meow a, meow b) {return meow(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);}
  23. meow conj(meow a) {return meow(a.x, -a.y);}
  24. typedef meow cd;
  25. namespace fft {
  26. int maxlen = 1<<17, rev[N];
  27. cd omega[N], omegaInv[N];
  28. void init() {
  29. for(int i=0; i<maxlen; i++) {
  30. omega[i] = cd(cos(2*PI/maxlen*i), sin(2*PI/maxlen*i));
  31. omegaInv[i] = conj(omega[i]);
  32. }
  33. }
  34. void dft(cd *a, int n, int flag) {
  35. cd *w = flag == 1 ? omega : omegaInv;
  36. for(int i=0; i<n; i++) if(i < rev[i]) swap(a[i], a[rev[i]]);
  37. for(int l=2; l<=n; l<<=1) {
  38. int m = l>>1;
  39. for(cd *p = a; p != a+n; p += l)
  40. for(int k=0; k<m; k++) {
  41. cd t = w[maxlen/l*k] * p[k+m];
  42. p[k+m] = p[k] - t;
  43. p[k] = p[k] + t;
  44. }
  45. }
  46. if(flag == -1) for(int i=0; i<n; i++) a[i].x /= n;
  47. }
  48. void mul(cd *a, cd *b, int n) {
  49. int k = 0; while((1<<k) < n) k++;
  50. for(int i=0; i<n; i++) rev[i] = (rev[i>>1]>>1) | ((i&1)<<(k-1));
  51. dft(a, n, 1); dft(b, n, 1);
  52. for(int i=0; i<n; i++) a[i] = a[i] * b[i];
  53. dft(a, n, -1);
  54. }
  55. }
  56. cd f[N], g[N];
  57. int n, m, Q, a[N], b[N]; ll c[N];
  58. void cdq(int l, int r) {
  59. if(l == r) {c[0] += (ll) a[l] * b[l]; return;}
  60. int mid = (l+r)>>1;
  61. int n = 1, lim = r-l+1;
  62. if(r-l < 200) {
  63. for(int i=l; i<=mid; i++) if(a[i] || b[i])
  64. for(int j=mid+1; j<=r; j++) c[i+j] += (ll) a[i] * b[j], c[j-i] += (ll) a[j] * b[i];
  65. } else {
  66. while(n < lim) n<<=1;
  67. for(int i=0; i<n; i++) f[i] = g[i] = cd();
  68. for(int i=l; i<=mid; i++) f[i-l].x += a[i];
  69. for(int i=mid+1; i<=r; i++) g[i-mid].x += b[i];
  70. fft::mul(f, g, n);
  71. for(int i=0; i<lim; i++) c[i+l+mid] += (ll) floor(f[i].x + 0.5);
  72. for(int i=0; i<n; i++) f[i] = g[i] = cd();
  73. for(int i=mid+1; i<=r; i++) f[i-mid].x += a[i];
  74. for(int i=l; i<=mid; i++) g[mid-i].x += b[i];
  75. fft::mul(f, g, n);
  76. for(int i=0; i<lim; i++) c[i] += (ll) floor(f[i].x + 0.5);
  77. }
  78. cdq(l, mid); cdq(mid+1, r);
  79. }
  80. int main() {
  81. //freopen("in", "r", stdin);
  82. int T=read();
  83. fft::init();
  84. while(T--) {
  85. n=read(); m=read(); Q=read();
  86. int l=0, r=0, x;
  87. memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); memset(c, 0, sizeof(c));
  88. for(int i=1; i<=n; i++) x=read(), a[x]++, r = max(r, x);
  89. for(int i=1; i<=m; i++) x=read(), b[x]++, r = max(r, x);
  90. cdq(l, r);
  91. while(Q--) printf("%lld\n", c[read()]);
  92. }
  93. }

bzoj 4836: 二元运算的更多相关文章

  1. bzoj 4836: [Lydsy2017年4月月赛]二元运算 -- 分治+FFT

    4836: [Lydsy2017年4月月赛]二元运算 Time Limit: 8 Sec  Memory Limit: 128 MB Description 定义二元运算 opt 满足   现在给定一 ...

  2. bzoj 4836 [Lydsy1704月赛]二元运算 分治FFT+生成函数

    [Lydsy1704月赛]二元运算 Time Limit: 8 Sec  Memory Limit: 128 MBSubmit: 577  Solved: 201[Submit][Status][Di ...

  3. BZOJ 4836: [Lydsy1704月赛]二元运算 分治FFT

    Code: #include<bits/stdc++.h> #define ll long long #define maxn 500000 #define setIO(s) freope ...

  4. [BZOJ4836]二元运算(分治FFT)

    4836: [Lydsy1704月赛]二元运算 Time Limit: 8 Sec  Memory Limit: 128 MBSubmit: 578  Solved: 202[Submit][Stat ...

  5. BZOJ4836: [Lydsy1704月赛]二元运算

    BZOJ4836: [Lydsy1704月赛]二元运算 https://lydsy.com/JudgeOnline/problem.php?id=4836 分析: 分开做,维护两个桶. 分治每次求\( ...

  6. BZOJ 2127: happiness [最小割]

    2127: happiness Time Limit: 51 Sec  Memory Limit: 259 MBSubmit: 1815  Solved: 878[Submit][Status][Di ...

  7. BZOJ 3275: Number

    3275: Number Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 874  Solved: 371[Submit][Status][Discus ...

  8. BZOJ 2879: [Noi2012]美食节

    2879: [Noi2012]美食节 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1834  Solved: 969[Submit][Status] ...

  9. bzoj 4610 Ceiling Functi

    bzoj 4610 Ceiling Functi Description bzoj上的描述有问题 给出\(n\)个长度为\(k\)的数列,将每个数列构成一个二叉搜索树,问有多少颗形态不同的树. Inp ...

随机推荐

  1. POJ_1064 二分搜索

    /*POJ 1064 *题目大意:有N条绳子,他们的长度分别为Li,如果从他们中切割K条长度相同的绳子的话,这K条绳子每条最长能有多长?答案保留到小数点后2位 *算法分析:这个问题用二分搜索可以非常容 ...

  2. Spring学习日志之Glance

    Spring的本质 Spring最根本的意图只有一个:简化Java开发 Spring的核心主要有两个: 依赖注入 AOP Spring容器 Spring容器负责对对象进行创建,装配,配置并管理它们的整 ...

  3. Qt 共享库(动态链接库)和静态链接库的创建及调用

    前言: 编译器 Qt Creator, 系统环境 win7 64 位 1.创建共享库: 新建文件或项目->选择 Library 和 c++ 库->选择共享库->下一步(工程名为 sh ...

  4. Android studio启动后卡在refreshing gradle project(包解决)

    这个问题几乎每个刚使用Android studio的同学都会碰到过,网上有各式各样的方法,有的说使用本地gradle,我试过多次,每次启动android studio时还是会检查更新,所以根本上解决的 ...

  5. PHPStudy+PHPStorm下配置隐藏项目入口文件

    img { max-width: 100% } 默认情况下项目入口文件是站点根目录下index.php文件,一般程序启动时通过这个文件,定义文件路径,配置重要节点(比如是否开启调试模式),注册路由等, ...

  6. linux日志查看命令

    tail tail 命令用于显示文本文件的末尾几行, 对于监控文件日志特别有用 tail example.txt #显示文件 example.txt 的后十行内容: tail -n 20 exampl ...

  7. Oracle_view视图

    Oracle_view视图 视图view --视图:view --查询班级信息并统计各班的人数 select * from stu; select * from clazz;     select c ...

  8. eclipse配置虚拟路径后,每次启动tomcat都会虚拟路径失效的问题解决

    由于,eclipse启动tomcat部署项目并不是直接把项目放到tomcat的webapps目录下的,而是从我们在eclipse配置的外部tomcat中取出二进制文件,在eclipse内部插件中作为t ...

  9. dede首页调用分类信息

    格兰斯-与你共同开创门窗定制5G时代 5G战略--明确的,没有歧义的明亮奥尼克斯的观点与追求 {dede:channelartlist row=1}         {dede:arclistsg r ...

  10. sublime text如何保存为uft-8无bom编码格式文件

    https://jingyan.baidu.com/article/9158e000388092a2541228b6.html 今天发现自己的文件突然多了很多特殊符号,真是奇了怪,查找html里面也并 ...