【ACdream】1157 Segments cdq分治

2024-09-05 14:58
文章标签 分治 cdq acdream segments 1157

本文主要是介绍【ACdream】1157 Segments cdq分治,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

传送门:【ACdream】1157 Segments


题目分析:第一题cdq(陈丹琦)分治!cdq_____Orz!

听说cdq分治可以写,就去学了cdq分治了。。


在我们平常使用的分治中,每一个子问题只解决它本身(可以说是封闭的)。

而在cdq分治中,对于划分出来的两个子问题,前一个子问题用来解决后一个子问题而不是它本身。

具体算法流程如下:

1.将整个操作序列分为两个长度相等的部分(分)

2.递归处理前一部分的子问题(治1)

3.计算前一部分的子问题中的修改操作对后一部分子问题的影响(治2)

4.递归处理后一部分子问题(治3)


本题是将修改操作和查询操作分开来,每次都用左区间的修改操作更新右区间的查询操作,因为左区间的修改操作对左区间的查询操作在递归左区间时就已经处理了,同理查询操作也是一样。

太神奇了!!!

然后用左区间的修改操作去更新右区间的查询操作的时候可以用树状数组来维护。


代码如下:


#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std ;#define REP( i , a , b ) for ( int i = ( a ) ; i <  ( b ) ; ++ i )
#define FOR( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define REV( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define travel( e , H , u ) for ( Edge* e = H[u] ; e ; e = e -> next )
#define CLR( a , x ) memset ( a , x , sizeof a )
#define mid ( ( l + r ) >> 1 )typedef long long LL ;const int MAXN = 100005 ;struct Line {int l , r , v ;int idx ;
} L[MAXN] ;int T[MAXN << 1] ;
int a[MAXN << 1] , cnt ;
int del[MAXN] , num ;
int ans[MAXN] ;
int n ;int unique ( int n , int cnt = 1 ) {sort ( a + 1 , a + n + 1 ) ;FOR ( i , 2 , n ) if ( a[i] != a[cnt] ) a[++ cnt] = a[i] ;return cnt ;
}int search ( int x , int l , int r ) {while ( l < r ) {int m = mid ;if ( a[m] >= x ) r = m ;else l = m + 1 ;}return l ;
}inline int cmp1 ( const Line& a , const Line& b ) {if ( a.r != b.r ) return a.r > b.r ;if ( a.l != b.l ) return a.l < b.l ;return a.idx < b.idx ;
}inline int cmp2 ( const Line& a , const Line& b ) {return a.idx < b.idx ;
}void modify ( int x , int v ) {while ( x <= cnt ) {T[x] += v ;x += x & -x ;}
}int sum ( int x , int ans = 0 ) {while ( x ) {ans += T[x] ;x -= x & -x ;}return ans ;
}void cdq_solve ( int l , int r ) {if ( l == r ) return ;int m = mid ;cdq_solve ( l , m ) ;cdq_solve ( m + 1 , r ) ;sort ( L + l , L + r + 1 , cmp1 ) ;FOR ( i , l , r ) {if ( L[i].idx <= m && L[i].v ) modify ( L[i].l , L[i].v ) ;if ( L[i].idx > m && !L[i].v ) ans[L[i].idx] += sum ( L[i].l ) ;}FOR ( i , l , r ) if ( L[i].idx <= m && L[i].v ) modify ( L[i].l , -L[i].v ) ;
}void scanf ( int& x , char c = 0 ) {while ( ( c = getchar () ) < '0' || c > '9' ) ;x = c - '0' ;while ( ( c = getchar () ) >= '0' && c <= '9' ) x = x * 10 + c - '0' ;
}void solve () {char op ;CLR ( T , 0 ) ;CLR ( ans , 0 ) ;cnt = num = 0 ;FOR ( i , 1 , n ) {L[i].idx = i ;op = getchar () ;if ( op == 'D' ) {scanf ( L[i].l ) , scanf ( L[i].r ) ;a[++ cnt] = L[i].l ;a[++ cnt] = L[i].r ;del[++ num] = i ;L[i].v = 1 ;} else if ( op == 'Q' ) {scanf ( L[i].l ) , scanf ( L[i].r ) ;a[++ cnt] = L[i].l ;a[++ cnt] = L[i].r ;L[i].v = 0 ;} else {scanf ( L[i].l ) ;int index = del[L[i].l] ;L[i].l = L[index].l ;L[i].r = L[index].r ;L[i].v = -1 ;}}cnt = unique ( cnt ) ;FOR ( i , 1 , n ) {L[i].l = search ( L[i].l , 1 , cnt ) ;L[i].r = search ( L[i].r , 1 , cnt ) ;}cdq_solve ( 1 , n ) ;sort ( L + 1 , L + n + 1 , cmp2 ) ;FOR ( i , 1 , n ) if ( !L[i].v ) printf ( "%d\n" , ans[i] ) ;
}int main () {while ( ~scanf ( "%d " , &n ) ) solve () ;return 0 ;
}


这篇关于【ACdream】1157 Segments cdq分治的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1139283

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

ACdream区域赛指导赛之手速赛系列(4)

点击打开题目链接 #include <iostream>#include <map>#include <cstdio>#include <string>using namespace std;int a[501];//题意是能不能把一组两个人分到两个不同的正营里面,关键利用map映射void init(){for(int i = 0; i <= 200; i++){a[i]

找第K大数(ACdream 1099)

瑶瑶的第K大 Time Limit: 4000/2000MS (Java/Others)  Memory Limit: 256000/128000KB (Java/Others) Submit  Statistic  Next Problem Problem Description 一天,萌萌的妹子--瑶瑶(tsyao)很无聊,就来找你玩。可是你们都不知道玩什么。。。

分治算法与凸包问题

1. 什么是凸包问题? 凸包问题是计算几何中的经典问题。给定二维平面上的点集,凸包是一个最小的凸多边形,它包含了点集中所有的点。你可以把凸包想象成一根松紧带将所有点紧紧包裹住的样子,凸包的边缘仅沿着最外面的点延伸。 2. 分治法简介 分治算法是解决复杂问题的强大策略,它的思想是将问题分解为多个子问题,分别解决这些子问题后再合并得到最终解。凸包问题可以通过分治算法高效地解决,时间复杂度可以达到

【ACDream】1074 风之国 线段树+DP

风之国 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 KB (Java/Others) Problem Description 在X轴上有这样一个国家——风之国。风之国虽然是一个国家,但是却有N个首领,每个首领管辖着各自的一个城市。曾几何时,风之国是非常和

【ACDream】 1093 女神的正多面体 矩阵快速幂

题目大意:给你三种正多边形,给你起点s,终点e以及最多行走的步数k,问有多少种路径方案(路径中只要有一个顶点不同即视为不同)。 题目分析: 可以通过矩阵快速幂求解。 为每个正多边形(最多三个)构建一个邻接矩阵A,然后第K步的方案数即为A^k。 结果即为A^1 + A^2 + A^3 + ...... + A^k. 对于这种形式的矩阵运算,我们可以把它拆分成: k为奇:ans = (

【COGS】577 蝗灾 cdq分治

传送门:【COGS】577 蝗灾 题目分析:cdq分治入门题= =。。。。用差分思想将矩阵分成四块来计算。。排序一维,另一维用树状数组解决。 代码如下: #include <cstdio>#include <vector>#include <cstring>#include <algorithm>using namespace std ;#define REP(

【ACdream】ACdream原创群赛(18)のAK's dream

这次的群赛AK的不少,7题的也很多啊。。Orrrrrrrz。。。。 暂时只写出7题。。。 A:1196 模拟。。 /** this code is made by poursoul* Problem: 1196* Verdict: Accepted* Submission Date: 2014-09-06 19:12:44* Time: 0MS* Memo

【HDU】4871 Shortest-path tree 最短路+点分治

传送门:【HDU】4871 Shortest-path tree 题目分析: 学了点分治后来看这道题,简直就是水题。。。 但是我竟然花了将近一个晚上才写出来。。。就因为一个地方写漏了T U T。。 首先根据题意求一棵树,最短路一下,然后最小字典序就按照编号顺序遍历邻接表给节点标记。 剩下的就是树分治的事了。 在以重心X为根的子树中,按照X的子节点v的子树中最长路径包含节点数升序遍