#3876 快速排序

描述

输入

输出

对于每个询问,输出一行三个整数,分别表示最大差距、最小差距和方案数。

样例输入[复制]

3 3
1 2 3 4 5 6
1 1 6
0 1 6 10
1 1 6

样例输出[复制]

9 3 5
9 3 5

提示

n,m<=5e5,-5e5<=val<=5e5,区间为偶数长度

考场代码
code:
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #define N 2000005
  5 #define min(i,j) i>j?j:i
  6 #define lc (p<<1)
  7 #define rc (p<<1|1)
  8 using namespace std;
  9 const int mod=1e9+7;
 10 long long a[N],cat[N];
 11 struct SEGMENT_TREE {
 12     int type;
 13     struct node {
 14         int l,r;
 15         long long sum,lazy;
 16     } t[N];
 17     inline void pushup(int p) {
 18         t[p].sum=t[lc].sum+t[rc].sum;
 19     }
 20     inline void pushnow(int p,int v) {
 21         t[p].sum+=(t[p].r-t[p].l+1)*v;
 22         t[p].lazy=t[p].lazy+v;
 23     }
 24     inline void pushdown(int p) {
 25         if(t[p].lazy) {
 26             pushnow(lc,t[p].lazy);
 27             pushnow(rc,t[p].lazy);
 28             t[p].lazy=0;
 29         }
 30     }
 31     void build(int p,int l,int r) {
 32         t[p].l=l,t[p].r=r;
 33         if(l==r) {
 34             if(type==3) {
 35                 t[p].sum=a[l];
 36             } else
 37                 t[p].sum=a[2*l-type];
 38             t[p].lazy=0;
 39             return;
 40         }
 41         int mid=l+r>>1;
 42         build(lc,l,mid);
 43         build(rc,mid+1,r);
 44         pushup(p);
 45     }
 46     void update(int p,int ql,int qr,int v) {
 47         if(ql<=t[p].l&&t[p].r<=qr) {
 48             pushnow(p,v);
 49             return;
 50         }
 51         pushdown(p);
 52         int mid=t[p].l+t[p].r>>1;
 53         if(ql<=mid)update(lc,ql,qr,v);
 54         if(qr>mid)update(rc,ql,qr,v);
 55         pushup(p);
 56     }
 57     int query(int p,int ql,int qr) {
 58         if(ql<=t[p].l&&t[p].r<=qr) {
 59             return t[p].sum;
 60         }
 61         pushdown(p);
 62         long long ans=0;
 63         int mid=t[p].l+t[p].r>>1;
 64         if(ql<=mid)ans+=query(lc,ql,qr);
 65         if(qr>mid)ans+=query(rc,ql,qr);
 66         pushup(p);
 67         return ans;
 68     }
 69 } A,B,C;
 70 int f[12];
 71 int n;
 72 long long inv[N];
 73 inline void pre() {
 74     inv[1]=1;int n1=n+1;
 75     for(register int i=2;i<=n1;i++)
 76     {
 77         inv[i]=inv[mod%i]*(-mod/i)%mod;
 78         inv[i]=inv[i]<0?inv[i]+mod:inv[i];
 79     }
 80     cat[1]=1;
 81     for(register int i=2; i<=n; i++)cat[i]=((cat[i-1]*((i<<2)-2)%mod)*inv[i+1])%mod;
 82 }
 83 inline int read(){
 84     int x=0;
 85     bool f=true;
 86     char c=getchar();
 87     while(!isdigit(c)){
 88         if(c=='-')f=false;
 89         c=getchar();
 90     }
 91     while(isdigit(c)){
 92         x=(x<<3)+(x<<1)+(c^48);
 93         c=getchar();
 94     }
 95     return f?x:-x;
 96 }
 97 int main() {
 98 //    freopen("sort.in","r",stdin);
 99 //    freopen("sort.out","w",stdout);
100     int m,n2,md,l2,r2,op,l,r,val;
101     long long max0,temp1;
102     n=read(),m=read();n2=n<<1;
103     pre();
104     for(register int i=1; i<=n2; i++)
105         a[i]=read();
106     A.type=1,B.type=0,C.type=3;
107     A.build(1,1,n);
108     B.build(1,1,n);
109     C.build(1,1,n2);
110     while(m--) {
111         op=read();l=read(),r=read();md=l+r>>1;l2=l>>1;r2=r>>1;
112         if(op) {
113             max0=C.query(1,md+1,r)-C.query(1,l,md);
114             if(l&1)
115                 temp1=B.query(1,l2+1,r2)-A.query(1,l2+1,r2);
116             else
117                 temp1=A.query(1,l2+1,r2+1)-B.query(1,l2,r2);
118             printf("%lld %lld %lld\n",max0,temp1,cat[(r-l+1)>>1]);
119         }
120         else{
121             val=read();
122             C.update(1,l,r,val);
123             if(l&1){
124                 B.update(1,l2+1,r2,val);
125                 A.update(1,l2+1,r2,val);
126             }
127             else{
128                 B.update(1,l2,r2,val);
129                 A.update(1,l2+1,r2+1,val);
130             }
131         }
132     }
133     return 0;
134 }

over

转载于:https://www.cnblogs.com/saionjisekai/p/9911843.html

11.05T2 线段树+卡特兰数相关推荐

  1. 【算法讲11:卡特兰数】默慈金数 | 那罗延数 | 施罗德数

    [算法讲11:卡特兰数]默慈金数 | 那罗延数 | 施罗德数 ⌈\lceil⌈卡特兰数⌋\rfloor⌋Catalan Number 引入 思考 ⌈\lceil⌈卡特兰数⌋\rfloor⌋的性质 ⌈\ ...

  2. [帝皇杯day 1] [NOIP2018模拟赛]小P的loI(暴力+素筛),【NOIP模拟赛】创世纪(贪心),无聊的数对(线段树)

    文章目录 T1:小P的lol title solution code T2:创世纪 title solution code T3:无聊的数对 title solution code T1:小P的lol ...

  3. [2019 牛客CSP-S提高组赛前集训营4题解] 复读数组(数论)+ 路径计数机(数上DP)+ 排列计数机(线段树+二项式定理)

    文章目录 T1:复读数组 题目 题解 代码实现 T2:路径计数机 题目 题解 代码实现 T3:排列计数机 题目 题解 CODE T1:复读数组 题目 有一个长为n×k的数组,它是由长为n的数组A1,A ...

  4. [集训队作业2018] count(笛卡尔树,生成函数,卡特兰数)

    传送门 什么情况下两序列同构 对于两序列A[1,n],B[1,n]A[1,n],B[1,n]A[1,n],B[1,n],设fA(1,n)=pa,fB(1,n)=pbf_A(1,n)=p_a,f_B(1 ...

  5. Bzoj 4408: [Fjoi 2016]神秘数 可持久化线段树,神题

    4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 177  Solved: 128 [Submit][Stat ...

  6. [算法学习] 线段树,树状数组,数堆,笛卡尔树

    都是树的变种,用途不同 [线段树 Interval Tree] 区间管理,是一种平衡树 可看做是对一维数组的索引进行管理.一维数组不需要是排序好的 深度不超过logL 任一个区间(线段)都分成不超过2 ...

  7. D-query SPOJ - DQUERY(求区间不同数的个数)(树状数组||线段树+离散)(主席树+在线)

    English Vietnamese Given a sequence of n numbers a1, a2, -, an and a number of d-queries. A d-query ...

  8. HDU - 4578Transformation——线段树+区间加法修改+区间乘法修改+区间置数+区间和查询+区间平方和查询+区间立方和查询

    [题目描述] HDU - 4578Transformation Problem Description Yuanfang is puzzled with the question below: The ...

  9. P3939 数颜色 (权值线段树)

    题目链接: P3939 数颜色 大致题意 略 解题思路 权值线段树 对于这个题, 由于询问的是, 对于c颜色, [l, r]区间有多少该颜色的兔子. 相当于每次询问的就是一种颜色的区间查询. 假设颜色 ...

最新文章

  1. android APK内存多少字节,Android apk安全测评、应用加固、字节对齐、二次签名(有这一篇就够了)...
  2. qlabel 边加载边更新_王者荣耀:9月版本更新,九位英雄调整,三大战边回归,飞牛笑了...
  3. hadoop2.6.0实践:002 检查伪分布式环境搭建
  4. python爬虫如何运行在web_Python Web爬网-使用爬虫进行测试
  5. Intel 平台编程总结----缓存的优化
  6. ecshop几个价格
  7. android屏幕适配的五种方式_讲一讲Android 9.0系统的新特性,对刘海屏设备进行适配...
  8. 智能化改造!AI技术在传统企业大有可为!
  9. 图解BIO、NIO、AIO、多路复用IO的区别
  10. 【实例解析】某集团BI决策系统建设方案分享
  11. 缝衣间走出来的创始人,他的开源软件公司被 340 亿美元收购了
  12. 基础 - jQuery
  13. 老师用计算机教我们画画拼音,《ang eng ing ong》教案
  14. 杂谈-苹果账号调查事件始末,Apple审核流程或有变
  15. 但行好事 莫问前程 学习笔记 media=screen
  16. Pcode粗略分析(1)
  17. 【计蒜客】蒜头君的旅游计划
  18. Linux-Shell脚本练习
  19. 硬件设计学习笔记---第一季第一集
  20. 虚拟拔号服务器,windows实现虚拟拨号服务器

热门文章

  1. 20211010 PHP笔记
  2. 树莓派桌面没有时间_树莓派日期时间不准的修正方法
  3. vim显示行号_使用 vim 不得不看的 2 个 tips
  4. arm-linux-gcc 裸机程序,Linux下ARM裸机开发-交叉工具链
  5. 20200720:每日一题之两数之和Ⅱ(leetcode167)
  6. hive和hadoop关系
  7. 移动设备安全隐患分析
  8. VB根据窗口标题获取应用程序完整路径
  9. 【转】VB中NEW的用法(申请内存空间)
  10. [vb]On Error GoTo 0和On Error resume区别