Crayon 线段树或者树状数组
Crayon
Problem Description
Background
Mary love painting so much, but as we know she can't draw very well. There is no one appreciate her works, so she come up with a puzzle with herself.
Description
There are only one case in each input file, the first line is a integer N (N ≤ 1,000,00) denoted the total operations executed by Mary.
Then following N lines, each line is one of the folling operations.
- D L R : draw a segment [L, R], 1 ≤ L ≤ R ≤ 1,000,000,000.
- C I : clear the ith added segment. It’s guaranteed that the every added segment will be cleared only once.
- Q L R : query the number of segment sharing at least a common point with interval [L, R]. 1 ≤ L ≤ R ≤ 1,000,000,000.
Input
n
Then following n operations ...
Output
For each query, print the result on a single line ...
Sample Input
- 6
- D 1 3
- D 2 4
- Q 2 3
- D 2 4
- C 2
- Q 2 3
Sample Output
- 2
- 2
- 线段树
- #include <iostream>
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #include <algorithm>
- using namespace std;
- #define ll long long
- typedef struct abcd
- {
- int l,r,ci,i;
- char x;
- } abcd;
- abcd a[];
- typedef struct abc
- {
- int x,i;
- } abc;
- abc c[];
- int cn=,b[],bn=;
- bool cmp(abc x,abc y)
- {
- return x.x<y.x;
- }
- bool cmp1(abcd x,abcd y)
- {
- return x.l<y.l;
- }
- bool cmp2(abcd x,abcd y)
- {
- return x.r<y.r;
- }
- bool cmp3(abcd x,abcd y)
- {
- return x.i<y.i;
- }
- typedef struct tree
- {
- int a,d,sub;
- } tree;
- tree t[];
- void fun(int x)
- {
- if(t[x].d)
- {
- t[x<<].d+=t[x].d;
- t[x<<].sub+=t[x].d;
- t[x<<|].sub+=t[x].d;
- t[x<<|].d+=t[x].d;
- t[x<<].a+=t[x].d;
- t[x<<|].a+=t[x].d;
- t[x].d=;
- }
- }
- void update(int x,int y,int b,int c,int tt,int z)
- {
- if(x<=b&&y>=c)
- {
- t[tt].sub+=z;
- t[tt].d+=z;
- t[tt].a+=z;
- return ;
- }
- if(t[tt].d)
- fun(tt);
- int m=(b+c)>>;
- if(x<=m&&y>m)t[tt].sub+=z;
- if(x<=m)update(x,y,b,m,tt<<,z);
- if(y>m)update(x,y,m+,c,tt<<|,z);
- t[tt].a=t[tt<<].a+t[tt<<|].a-t[tt].sub;
- }
- int query(int x,int y,int b,int c,int tt)
- {
- if(x<=b&&y>=c)
- {
- return t[tt].a;
- }
- if(t[tt].d)
- fun(tt);
- int m=(b+c)>>;
- int r=,sub;
- if(x<=m)r=query(x,y,b,m,tt<<);
- if(y>m)r=r+query(x,y,m+,c,tt<<|);
- t[tt].a=t[tt<<].a+t[tt<<|].a-t[tt].sub;
- if(x<=m&&y>m)
- return r-t[tt].sub;
- else return r;
- }
- int main()
- {
- int n,i,j;
- //freopen("in.txt","r",stdin);
- scanf("%d",&n);
- for(i=; i<n; i++)
- {
- getchar();
- scanf("%c",&a[i].x);
- if(a[i].x=='C')
- {
- scanf("%d",&a[i].ci);
- }
- else
- {
- scanf("%d%d",&a[i].l,&a[i].r);
- c[cn++].x=a[i].l,c[cn++].x=a[i].r;
- if(a[i].x=='D')
- b[bn++]=i;
- }
- a[i].i=i;
- }
- int now=;
- sort(c,c+cn,cmp);
- c[].i=;
- for(i=; i<cn; i++)
- {
- if(c[i].x==c[i-].x)
- c[i].i=c[i-].i;
- else c[i].i=now++;
- }
- sort(a,a+n,cmp1);
- j=;
- for(i=; i<n; i++)
- {
- while(i<n&&a[i].x=='C')i++;
- if(i==n)break;
- while(a[i].l!=c[j].x)j++;
- a[i].l=c[j].i;
- }
- sort(a,a+n,cmp2);
- j=;
- for(i=; i<n; i++)
- {
- while(i<n&&a[i].x=='C')i++;
- if(i==n)break;
- while(a[i].r!=c[j].x)j++;
- a[i].r=c[j].i;
- }
- sort(a,a+n,cmp3);
- /*for(i=0; i<n; i++)
- cout<<a[i].x<<" "<<a[i].l<<" "<<a[i].r<<endl;*/
- memset(t,,sizeof(t));
- for(i=; i<n; i++)
- {
- if(a[i].x=='D')
- {
- update(a[i].l,a[i].r,,c[cn-].i,,);
- }
- else if(a[i].x=='C')
- {
- update(a[b[a[i].ci]].l,a[b[a[i].ci]].r,,c[cn-].i,,-);
- }
- else
- {
- printf("%d\n",query(a[i].l,a[i].r,,c[cn-].i,));
- }
- }
- }
树状数组
- #include <iostream>
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #include <algorithm>
- using namespace std;
- #define ll long long
- typedef struct abcd
- {
- int l,r,ci,i;
- char x;
- } abcd;
- abcd a[];
- typedef struct abc
- {
- int x,i;
- } abc;
- abc c[];
- int cn=,b[],bn=;
- bool cmp(abc x,abc y)
- {
- return x.x<y.x;
- }
- bool cmp1(abcd x,abcd y)
- {
- return x.l<y.l;
- }
- bool cmp2(abcd x,abcd y)
- {
- return x.r<y.r;
- }
- bool cmp3(abcd x,abcd y)
- {
- return x.i<y.i;
- }
- int ab[][],m;
- int lowbit(int x)
- {
- return x&(-x);
- }
- void update(int y,int x,int z)
- {
- while(x<=m)
- {
- ab[x][y]+=z;
- x+=lowbit(x);
- }
- }
- int query(int y,int x)
- {
- int sum=;
- while(x>)
- {
- sum+=ab[x][y];
- x-=lowbit(x);
- }
- return sum;
- }
- int main()
- {
- int n,i,j;
- // freopen("in.txt","r",stdin);
- scanf("%d",&n);
- for(i=; i<n; i++)
- {
- getchar();
- scanf("%c",&a[i].x);
- if(a[i].x=='C')
- {
- scanf("%d",&a[i].ci);
- }
- else
- {
- scanf("%d%d",&a[i].l,&a[i].r);
- c[cn++].x=a[i].l,c[cn++].x=a[i].r;
- if(a[i].x=='D')
- b[bn++]=i;
- }
- a[i].i=i;
- }
- int now=,sum=;
- sort(c,c+cn,cmp);
- c[].i=;
- for(i=; i<cn; i++)
- {
- if(c[i].x==c[i-].x)
- c[i].i=c[i-].i;
- else c[i].i=now++;
- }
- sort(a,a+n,cmp1);
- j=;
- for(i=; i<n; i++)
- {
- while(i<n&&a[i].x=='C')i++;
- if(i==n)break;
- while(a[i].l!=c[j].x)j++;
- a[i].l=c[j].i;
- }
- sort(a,a+n,cmp2);
- j=;
- for(i=; i<n; i++)
- {
- while(i<n&&a[i].x=='C')i++;
- if(i==n)break;
- while(a[i].r!=c[j].x)j++;
- a[i].r=c[j].i;
- }
- sort(a,a+n,cmp3);
- /*for(i=0; i<n; i++)
- cout<<a[i].x<<" "<<a[i].l<<" "<<a[i].r<<endl;*/
- m=c[cn-].i;
- for(i=; i<n; i++)
- {
- if(a[i].x=='D')
- {
- update(,a[i].l,);
- update(,a[i].r,);
- sum++;
- }
- else if(a[i].x=='C')
- {
- update(,a[b[a[i].ci]].l,-);
- update(,a[b[a[i].ci]].r,-);
- sum--;
- }
- else
- {
- int ans=query(,a[i].r);
- ans-=query(,a[i].l-);
- printf("%d\n",ans);
- }
- }
- }
Crayon 线段树或者树状数组的更多相关文章
- BZOJ2120:数颜色(数状数组套主席树)(带修改的莫对)
墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2. R P ...
- 5.15 牛客挑战赛40 E 小V和gcd树 树链剖分 主席树 树状数组 根号分治
LINK:小V和gcd树 时限是8s 所以当时好多nq的暴力都能跑过. 考虑每次询问暴力 跳父亲 这样是nq的 4e8左右 随便过. 不过每次跳到某个点的时候需要得到边权 如果直接暴力gcd的话 nq ...
- [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- HDU 1556 线段树或树状数组,插段求点
1.HDU 1556 Color the ball 区间更新,单点查询 2.题意:n个气球,每次给(a,b)区间的气球涂一次色,问最后每个气球各涂了几次. (1)树状数组 总结:树状数组是一个查 ...
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- 【树状数组套权值线段树】bzoj1901 Zju2112 Dynamic Rankings
谁再管这玩意叫树状数组套主席树我跟谁急 明明就是树状数组的每个结点维护一棵动态开结点的权值线段树而已 好吧,其实只有一个指针,指向该结点的权值线段树的当前结点 每次查询之前,要让指针指向根结点 不同结 ...
- HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description The inve ...
- POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树
题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...
- Turing Tree_线段树&树状数组
Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...
随机推荐
- MVC配置伪静态
提出需求 伪静态能提高搜索引擎收录,还不影响硬盘寿命,是一个不错的选择,但是会增加CPU和内存开销,由于时候也需要实现伪静态. web.config配置 <system.webServer> ...
- UWP 手绘视频创作工具技术分享系列
开篇先来说一下写这篇文章的初衷. 初到来画,通读了来画 UWP App 的代码,发现里面确实有很多比较高深的技术点,同时也是有很多问题的,扩展性,耦合,性能,功能等等.于是我们决定从头重构这个产品,做 ...
- ORACLE SEQUENCE的简单介绍
先假设有这么一个表: create table S_Depart ( DepartId INT not null, DepartName NVARCHAR2() not null, DepartOrd ...
- java开发3轮技术面+hr面 面经(MT)
一直没打理博客园 发现博客园阅读量好大,就把前段时间写的一个面经也搬过来咯,大家一起加油.... 作者:小仇Eleven 链接:https://www.nowcoder.com/discuss/37 ...
- About Cheating and Plagiarism
我先描述一下此次事件的具体经过.昨天3月15号的晚上十点,是第四次作业的deadline.在15号之前,只有五位同学提交了作业,而在临近deadline的这几个小时内密密麻麻地提交了二十多份作业.和第 ...
- 结对编程1 (四则运算基于GUI)
https://git.coding.net/Luo_yujie/sizeyunsuan.app.git 201421123034 201421123032 1. 需求分析 这次作业新引用了语言选择, ...
- 第03周-Java作业评价
1.作业存在的问题 几大扣分点: 思维导图敷衍了事 -1 代码格式混乱 -2 无码云提交记录 -2 无PTA实验总结 -2 部分题目未完成. 格式问题:到现在还搞不清楚的,主要是态度问题.相关的教程 ...
- 201521145《Java程序设计》第2周学习总结
1. 本章学习总结 掌握了整数类型(byte short int long char),浮点型(float double),布尔型(boolean)的使用,以及它们的包装类Byte Short Int ...
- 201521123076《java程序设计》第12周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...
- 201521123017 《Java程序设计》第12周学习总结
1. 本周学习总结 2. 书面作业 Q1.字符流与文本文件:使用 PrintWriter(写),BufferedReader(读) 1.1 生成的三个学生对象,使用PrintWriter的printl ...