传送门——BZOJ


THUWC2019D1T1撞题可还行

以前有些人做过还问过我,但是我没有珍惜,直到进入考场才追悔莫及……

设\(que_{i,j}\)表示询问\((1,i,1,j)\)的答案,那么询问\((a,b,c,d)=que_{b,d} - que_{a-1 , d} - que_{b , c - 1} + que_{a - 1 , c - 1}\)

把一个询问拆成\(4\)个询问,然后对这\(4\)个询问莫队就可以了

不知道怎么回事THUWC上想到了莫队想到了前缀和想到了容斥就是没想到莫队+前缀和+容斥……

  1. #include<bits/stdc++.h>
  2. //This code is written by Itst
  3. using namespace std;
  4. inline int read(){
  5. int a = 0;
  6. char c = getchar();
  7. bool f = 0;
  8. while(!isdigit(c) && c != EOF){
  9. if(c == '-')
  10. f = 1;
  11. c = getchar();
  12. }
  13. if(c == EOF)
  14. exit(0);
  15. while(isdigit(c)){
  16. a = a * 10 + c - 48;
  17. c = getchar();
  18. }
  19. return f ? -a : a;
  20. }
  21. const int MAXN = 5e4 + 3;
  22. int N , M , T , cnt , cntQ;
  23. int arr[MAXN] , times[MAXN][2];
  24. long long ans[MAXN] , cur;
  25. struct query{
  26. int p1 , p2 , ind , flg;
  27. query(int a = 0 , int b = 0 , int c = 0 , int d = 0):p1(a) , p2(b) , ind(c) , flg(d){}
  28. bool operator <(const query b)const{
  29. return p1 / T == b.p1 / T ? p2 < b.p2 : p1 < b.p1;
  30. }
  31. }que[MAXN << 2];
  32. inline void add(int a , int ind){
  33. cur += times[a][ind ^ 1];
  34. ++times[a][ind];
  35. }
  36. inline void del(int a , int ind){
  37. cur -= times[a][ind ^ 1];
  38. --times[a][ind];
  39. }
  40. int main(){
  41. #ifndef ONLINE_JUDGE
  42. freopen("in","r",stdin);
  43. freopen("out","w",stdout);
  44. #endif
  45. N = read();
  46. T = sqrt(N);
  47. for(int i = 1 ; i <= N ; ++i)
  48. arr[i] = read();
  49. M = read();
  50. for(int i = 1 ; i <= M ; ++i){
  51. int a = read() , b = read() , c = read() , d = read();
  52. que[++cntQ] = query(b , d , i , 1);
  53. if(a - 1)
  54. que[++cntQ] = query(a - 1 , d , i , -1);
  55. if(b - 1)
  56. que[++cntQ] = query(b , c - 1 , i , -1);
  57. if(a - 1 && b - 1)
  58. que[++cntQ] = query(a - 1 , c - 1 , i , 1);
  59. }
  60. sort(que + 1 , que + cntQ + 1);
  61. int l1 = 0 , l2 = 0;
  62. for(int i = 1 ; i <= cntQ ; ++i){
  63. while(l1 < que[i].p1)
  64. add(arr[++l1] , 0);
  65. while(l1 > que[i].p1)
  66. del(arr[l1--] , 0);
  67. while(l2 < que[i].p2)
  68. add(arr[++l2] , 1);
  69. while(l2 > que[i].p2)
  70. del(arr[l2--] , 1);
  71. ans[que[i].ind] += que[i].flg * cur;
  72. }
  73. for(int i = 1 ; i <= M ; ++i)
  74. cout << ans[i] << '\n';
  75. return 0;
  76. }

Gym101138D Strange Queries/BZOJ5016 SNOI2017 一个简单的询问 莫队、前缀和、容斥的更多相关文章

  1. BZOJ5016:[SNOI2017]一个简单的询问(莫队)

    Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input 第 ...

  2. 【BZOJ5016】[Snoi2017]一个简单的询问 莫队

    [BZOJ5016][Snoi2017]一个简单的询问 Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计 ...

  3. 【bzoj5016】[Snoi2017]一个简单的询问 莫队算法

    题目描述 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. 输入 第一行,一个数字N,表 ...

  4. bzoj5016 & loj2254 [Snoi2017]一个简单的询问 莫队

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5016 https://loj.ac/problem/2254 题解 原式是这样的 \[ \su ...

  5. [bzoj5016][Snoi2017]一个简单的询问

    来自FallDream的博客,未经允许,请勿转载,谢谢. 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出   get(l,r,x)表示计算区间[l,r]中 ...

  6. BZOJ5016 Snoi2017一个简单的询问(莫队)

    容易想到区间转化成前缀和.这样每个询问有了二维坐标,莫队即可. #include<iostream> #include<cstdio> #include<cmath> ...

  7. [SNOI2017]一个简单的询问

    [SNOI2017]一个简单的询问 题目大意: 给定一个长度为\(n(n\le50000)\)的序列\(A(1\le A_i\le n)\),定义\(\operatorname{get}(l,r,x) ...

  8. bzoj P5016[Snoi2017]一个简单的询问——solution

    Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出   get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input ...

  9. bzoj 5016: [Snoi2017]一个简单的询问

    Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input 第 ...

随机推荐

  1. C#字典Dictionay多线程读是否是安全的

    答案:是线程安全的,只读不写多线程下,完全不需要加锁! 测试代码: using System; using System.Diagnostics; using System.Threading; us ...

  2. 章节四、3-While循环-DoWhile语句

    一.while死循环 package introduction5; public class WhileDemo { public static void main(String[] args) { ...

  3. 应用生命周期终极 DevOps 工具包

    [编者按]本文作者为 Kevin Goldberg,主要介绍了在开发.运营应用的完整生命周期当中,可能用到的 DevOps 工具大集合.文章系 OneAPM 工程师编译整理. DevOps工具包中合适 ...

  4. 内核线程的进程描述符task_struct中的mm和active_mm

    task_struct进程描述符中包含两个跟进程地址空间相关的字段mm, active_mm, struct task_struct { // ... struct mm_struct *mm; st ...

  5. 【Git学习一】Git 初始化

    在开始Git之旅之前,我们需要设置一下Git的配置变量. 1.告诉Git当前用户的姓名和邮件地址,配置用户名和邮件地址将在版本库提交时用到. 例子: ------------------------- ...

  6. vscode中配置php的xdebug

    vscode中配置php的xdebug vscode配置php的xdebug,步骤如下: 1. 安装phpdebug插件: PHP Debug 2.网上下载php的xdebug扩展(注意根据自己的ph ...

  7. ASP.NET -- WebForm -- ViewState

    ASP.NET -- WebForm --  ViewState 1. ViewState的作用 当 ASP .NET 中的表单被提交时,表单会随所有表单值一同重新出现.这是由于 ASP .NET 维 ...

  8. JDK动态代理和cglib代理详解

    JDK动态代理 先做一下简单的描述,通过代理之后返回的对象已并非原类所new出来的对象,而是代理对象.JDK的动态代理是基于接口的,也就是说,被代理类必须实现一个或多个接口.主要原因是JDK的代理原理 ...

  9. keepalived脑裂问题查找

    在自己环境做keepalived+redis实验时,当重启了备用redies机器后,发现两台redies主机都拿到了VIP [root@redis2 ~]# ip addr list 1: lo: & ...

  10. InfluxDB部署

    InfluxDB介绍 官网:https://www.influxdata.com/ 文档:https://docs.influxdata.com/influxdb/v1.2/introduction/ ...