昨晚玩游戏竟然不小心错过了CF。。我是有多浪啊。 今天总算趁着下课时间补了,感觉最后两题还是挺有意思的,写个题解。

D:

题目大意:

给出一个括号序列,问有多少个子序列 是k个'(' + k个')' 这样的形式。        n<=200000

解法:

对于每个'('的位置,计算以它为最右边的'('的合法子序列数。 假设它左边(包括它)有$l$个'(', 右边有 $r$个')' ,  如果子序列的长度2k, 那么

方案数有$\binom{l-1}{k-1} * \binom{r}{k}= \binom{l-1}{l-k} * \binom{r}{k}$

这个式子对$k$求和是  $\binom{l+r-1}{l}$  可以预处理阶乘O(1)计算出。

代码:

 #include <iostream>
#include <string>
#include <cstring>
#include <map>
#include <cmath>
#include <set>
#include <bitset>
using namespace std; typedef long long ll;
typedef pair<int,int> pii; #define N 400010
#define X first
#define Y second const int INF=<<;
const int Mod=;
int ans;
char s[N];
int fac[N],fac_inv[N]; int Power(int x,int p)
{
int res=;
for (;p;p>>=)
{
if (p&) res=1ll*res*x%Mod;
x=1ll*x*x%Mod;
}
return res;
} int C(int n,int m)
{
if (m==) return ;
if (n==) return ;
int res=1ll*fac[n]*fac_inv[m]%Mod;
return 1ll*res*fac_inv[n-m]%Mod;
} int l[N],r[N]; int main()
{
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout); fac[]=fac_inv[]=;
for (int i=;i<N;i++)
{
fac[i]=1ll*fac[i-]*i%Mod;
fac_inv[i]=Power(fac[i],Mod-);
}
int n; scanf("%s",s+);
n=strlen(s+); for (int i=;i<=n;i++) l[i]=l[i-]+(s[i]=='(');
for (int i=n;i>=;i--) r[i]=r[i+]+(s[i]==')'); for (int i=;i<=n;i++)
{
if (s[i]!='(') continue;
if (!r[i]) continue;
ans+=C(l[i]+r[i]-,l[i]);
if (ans>=Mod) ans-=Mod;
} printf("%d\n",ans);
return ;
}

E:

题目大意:

一开始有一个1-n的排列,然后每次操作交换两个数,每次询问逆序对数。        n<=200000,Q<=50000

解法:

假设我们交换了a[l],a[r], 那么逆序对数如何改变呢?

首先逆序对数会减少[l+1,r-1]这个区间里 比a[r]大的数  和 比a[l]小的数。

然后会增加[l+1,r-1]这个区间里 比a[r]小的数  和 比a[l]大的数。

因此我们需要快速计算某个区间里有多少个数比x大的。

我的做法是分块,每个块维护一个树状数组即可。 复杂度比较悬,换了几次块的大小,极限数据本地跑要10s,但是CF机子2s就跑完了?

代码:

 By lzw4896s, contest: Codeforces Round # (Div. ), problem: (E) Anton and Permutation, Accepted, #
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <cmath> using namespace std; typedef long long ll;
#define N 200010 int n,m,block;
int v[N],id[N]; inline int Lowbit(int x){return x&-x;} struct BIT
{
int c[N];
void Insert(int x,int v)
{
while (x<=n)
{
c[x]+=v;
x+=Lowbit(x);
}
}
int Sum(int l,int r)
{
if (l>r) return ;
int res=,x=r;
while (x)
{
res+=c[x];
x-=Lowbit(x);
}
x=l-;
while (x)
{
res-=c[x];
x-=Lowbit(x);
}
return res;
}
}tree[]; int Query(int l,int r,int low,int high)
{
int res=;
if (id[r]-id[l]<=)
{
for (int i=l;i<=r;i++) res+=(v[i]>=low && v[i]<=high);
return res;
}
for (int i=l;i<=block*id[l];i++) res+=(v[i]>=low && v[i]<=high);
for (int i=block*(id[r]-)+;i<=r;i++) res+=(v[i]>=low && v[i]<=high);
for (int i=id[l]+;i<=id[r]-;i++) res+=tree[i].Sum(low,high);
return res;
} int main()
{
///freopen("in.in","r",stdin);
//freopen("out.out","w",stdout); int Q,l,r; ll ans=;
scanf("%d%d",&n,&Q);
if (n>) block=;
else block=;
for (int i=;i<=n;i++) v[i]=i;
//block=4; for (int i=;i<=n;i+=block)
{
int j=min(n,i+block-); m++;
for (int k=i;k<=j;k++)
{
id[k]=m;
tree[m].Insert(k,);
}
}
while (Q--)
{
scanf("%d%d",&l,&r);
if (l==r)
{
printf("%I64d\n",ans);
continue;
}
if (l>r) swap(l,r);
if (v[l]>v[r]) ans--;
else ans++;
tree[id[l]].Insert(v[l],-);
tree[id[r]].Insert(v[r],-);
if (r-l>)
{
ans-=Query(l+,r-,v[r]+,n);
ans-=Query(l+,r-,,v[l]-);
ans+=Query(l+,r-,,v[r]-);
ans+=Query(l+,r-,v[l]+,n);
}
swap(v[l],v[r]);
tree[id[l]].Insert(v[l],);
tree[id[r]].Insert(v[r],);
//cout<<"eee\n";
printf("%I64d\n",ans);
//for (int i=1;i<=n;i++) cout<<v[i]<<" ";
//cout<<endl;
/*for (int i=1;i<=m;i++)
{
for (int j=1;j<=n;j++) cout<<tree[i].Sum(j,j)<<" ";
cout<<endl;
}*/
} return ;
}

Codeforces Round #404 (Div. 2) DE的更多相关文章

  1. Codeforces Round #404 (Div. 2) C 二分查找

    Codeforces Round #404 (Div. 2) 题意:对于 n and m (1 ≤ n, m ≤ 10^18)  找到 1) [n<= m] cout<<n; 2) ...

  2. Codeforces Round #404 (Div. 2) D. Anton and School - 2 数学

    D. Anton and School - 2 题目连接: http://codeforces.com/contest/785/problem/D Description As you probabl ...

  3. Codeforces Round #404 (Div. 2) A,B,C,D,E 暴力,暴力,二分,范德蒙恒等式,树状数组+分块

    题目链接:http://codeforces.com/contest/785 A. Anton and Polyhedrons time limit per test 2 seconds memory ...

  4. Codeforces Round #404 (Div. 2)(A.水,暴力,B,排序,贪心)

    A. Anton and Polyhedrons time limit per test:2 seconds memory limit per test:256 megabytes input:sta ...

  5. Codeforces Round #404 (Div. 2)A,B,C

    A. Anton and Polyhedrons 题目链接:http://codeforces.com/contest/785/problem/A 智障水题 实现代码: #include<bit ...

  6. Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 二分

    C. Anton and Fairy Tale 题目连接: http://codeforces.com/contest/785/problem/C Description Anton likes to ...

  7. Codeforces Round #404 (Div. 2) B. Anton and Classes 水题

    B. Anton and Classes 题目连接: http://codeforces.com/contest/785/problem/B Description Anton likes to pl ...

  8. Codeforces Round #404 (Div. 2) A - Anton and Polyhedrons 水题

    A - Anton and Polyhedrons 题目连接: http://codeforces.com/contest/785/problem/A Description Anton's favo ...

  9. 【组合数】【乘法逆元】 Codeforces Round #404 (Div. 2) D. Anton and School - 2

    http://codeforces.com/blog/entry/50996 官方题解讲得很明白,在这里我复述一下. 枚举每个左括号,考虑计算一定包含其的简单括号序列的个数,只考虑其及其左侧的左括号, ...

随机推荐

  1. HTML5:绘制图形

    canvas绘图通过属于 canvas 的 JavaScript 方法完成 针对不支持html5的IE浏览器 <!--[if IE]> <script type="text ...

  2. redis_安装及使用

    一.文档资料       1.官方网站:http://redis.io/       2.官方文档:http://redis.io/documentation       3.常用命令文档:http: ...

  3. MPFIT for python

    MPFIT本来用IDL语言写的,后面有人翻译成了C语言版本.再后面鉴于python语言的流行使用,又有人将其用Cython加了python接口,直接可以在python中调用,极大地方便了额们这些经常用 ...

  4. [Android Traffic] android 流量计算方法

    android流量简介 流量统计文件:路径/proc/net/dev 打开文件,其中 lo 为本地流量, rmnet0 为3g/2g流量, wlan0 为无线流量. 在/sys/class/net/下 ...

  5. 使用webpack构建本地服务器

    想不想让你的浏览器监听你的代码的修改,并自动刷新显示修改后的结果,其实Webpack提供一个可选的本地开发服务器,这个本地服务器基于node.js构建,可以实现你想要的这些功能,不过它是一个单独的组件 ...

  6. 【Cocos2d-x 3.0 基础系列一】 各类回调函数写法汇总

    一.button回调 1. Lambda 表达式,C++11 Lambda 赋予了Cocos2d-x 3.0创建回调函数的灵活性. auto itemNor = Sprite::create(&quo ...

  7. 【千纸诗书】—— PHP/MySQL二手书网站后台开发之基础知识

    前言: 在具体回顾每一个功能的实现前,还是有必要先温习一些项目涉及到的PHP.MySQL[语法基础].项目github地址:https://github.com/66Web/php_book_stor ...

  8. 在线安装eclipse中html/jsp/xml editor插件 eclipseeditor

    1.打开eclipse中的help————>Install New Software 2.点击Add按钮,然后弹出一个框,第一个文本框可以随便写,第二个一定要写: http://download ...

  9. Node.js 访问https网站

    源码: //==================================================== // 访问https://www.zhihu.com/得到pagecode // ...

  10. jquery 中attr和prop的区别

    在jQuery API中也有专门解释: Attributes VS. Properties 在一些特殊的情况下,attributes和properties的区别非常大.在jQuery1.6之前,.at ...