↓ Skip to main content
  1. Posts/

codeforces 61 E. Enemy is weak (离散化+线段树求逆序三元组)

·638 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题目链接 题意:给出n个数,求满足 i<j<k且a[i]>a[j]>a[k]的三元组有多少个。

思路:对于这种要求三个数满足条件的题目,老司机的经验是考虑中间那个数,这道题也不例外。

我们枚举j,通过求两次逆序对求出对于每个a[j],满足a[i]>a[j]的i的个数,以及满足a[j]>a[k]的个数。

两个个数的乘积就是j作为中间数满足的三元组的个数,这样把所有的j累加就是答案。

其他的就是,要离散化,以及最后答案可能会爆long long?

1A,好爽啊哈哈哈。

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :Mon 05 Sep 2016 03:40:20 PM CST
  4File Name :code/cf/problem/61E.cpp
  5************************************************ */
  6#include <cstdio>
  7#include <cstring>
  8#include <iostream>
  9#include <algorithm>
 10#include <vector>
 11#include <queue>
 12#include <set>
 13#include <map>
 14#include <string>
 15#include <cmath>
 16#include <cstdlib>
 17#include <ctime>
 18#define fst first
 19#define sec second
 20#define lson l,m,rt<<1
 21#define rson m+1,r,rt<<1|1
 22#define ms(a,x) memset(a,x,sizeof(a))
 23typedef long long LL;
 24#define pi pair < int ,int >
 25#define MP make_pair
 26using namespace std;
 27const double eps = 1E-8;
 28const int dx4[4]={1,0,0,-1};
 29const int dy4[4]={0,-1,1,0};
 30const int inf = 0x3f3f3f3f;
 31const int N=1E6+7;
 32int a[N];
 33int A[N];
 34int H[N];
 35int n;
 36int m;
 37int tree1[N<<2],tree2[N<<2];
 38pair<LL,LL>ans[N];
 39void PushUp(int rt,int *tree)
 40{
 41    tree[rt] = tree[rt<<1] + tree[rt<<1|1];
 42}
 43void update(int p,int l,int r,int rt,int *tree)
 44{
 45    if (l==r)
 46    {
 47	tree[rt]++;
 48	return;
 49    }
 50    int m = (l+r)>>1;
 51    if (p<=m) update(p,lson,tree);
 52    else update(p,rson,tree);
 53    PushUp(rt,tree);
 54}
 55int query(int L,int R,int l,int r,int rt,int *tree)
 56{
 57//    cout<<"L:"<<L<<" R:"<<R<<" l:"<<l<<" r:"<<r<<" rt:"<<rt<<endl;
 58    if (L>R) return 0;
 59    if (L<=l&&r<=R) return tree[rt];
 60    int m = (l+r)>>1;
 61    int ret = 0 ;
 62    if (L<=m)
 63    {
 64	int res = query(L,R,lson,tree);
 65	ret+=res;
 66    }
 67    if (R>=m+1)
 68    {
 69	int res =  query(L,R,rson,tree);
 70	ret +=res;
 71    }
 72    return ret;
 73}
 74int Hash( int x)
 75{
 76    return lower_bound(H,H+m,x)-H;
 77}
 78int main()
 79{
 80	#ifndef  ONLINE_JUDGE
 81	freopen("code/in.txt","r",stdin);
 82  #endif
 83	cin>>n;
 84	m = n;
 85	for ( int i = 0 ; i < n ; i++)
 86	{
 87	    scanf("%d",&a[i]);
 88	    H[i] = a[i];
 89	}
 90	sort(H,H+m);
 91	m = unique(H,H+m)-H;
 92	int mx = 0 ;
 93	for ( int i = 0 ; i < n ; i++) A[i] = Hash(a[i])+1,mx = max(mx,A[i]);
 94	ms(tree1,0);
 95	ms(tree2,0);
 96	for ( int i = n-1 ; i >=0 ; i--)
 97	{
 98	    LL tmp = LL(query(1,A[i]-1,1,mx,1,tree1));
 99	    ans[i].fst = tmp;
100	    update(A[i],1,mx,1,tree1);
101	}
102	for ( int i =  0 ; i < n ; i++)
103	{
104	    LL tmp = LL(query(A[i]+1,mx,1,mx,1,tree2));
105	    ans[i].sec = tmp;
106	    update(A[i],1,mx,1,tree2);
107	}
108	LL res = 0LL;
109	for ( int i = 0 ; i < n ; i++) res = res + ans[i].fst*ans[i].sec;
110	cout<<res<<endl;
111  #ifndef ONLINE_JUDGE
112  fclose(stdin);
113  #endif
114    return 0;
115}

Related

hdu 1754 I Hate It (线段树模板题,炒鸡详细注释版)

·1717 words·4 mins
hdu 1754 题目链接 题意:单点更新,区间查询最大值。 思路:线段树。 一开始借鉴了 clj 的 pointer 写法,wjmzbmr’s code 直接 MLE,看来也许只能在 cf 上用。 下面是 MLE 的代码: 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月18日 星期四 18时40分24秒 4File Name :code/hdu/1754.cpp 5************************************************ */ 6#include <cstdio> 7#include <cstring> 8#include <iostream> 9#include <algorithm> 10#include <vector> 11#include <queue> 12#include <stack> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <deque> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27using namespace std; 28const double eps = 1E-8; 29const int dx4[4]={1,0,0,-1}; 30const int dy4[4]={0,-1,1,0}; 31const int inf = 0x3f3f3f3f; 32const int N=2E5+7; 33int a[N],n,m; 34int _max( int x,int y) 35{ 36 if (x==-1||y==-1) 37 return x==-1?y:x; 38 return a[x]>a[y]?x:y; 39} 40struct Tree 41{ 42 Tree *pl,*pr; 43 int l,r,mx; 44 void update() 45 { 46 mx = _max(pl->mx,pr->mx); 47 } 48 Tree(int l,int r) : 49 l(l),r(r) 50 { 51 if ( l + 1 == r) 52 { 53 mx = l ; 54 return ; 55 } 56 pl = new Tree(l,(l+r)>>1); 57 pr = new Tree((l+r)>>1,r); 58 update(); 59 } 60 void change(int p,int x) 61 { 62 if (p < l|| p>=r) return; 63 if (l+1==r) 64 { 65 a[l] = x; 66 return ; 67 } 68 pl->change(p,x); 69 pr->change(p,x); 70 update(); 71 } 72 int queryMax(int L,int R) 73 { 74 if (L <= l && r <= R) return mx; 75 if (L>=r || l >=R) 76 return -1; 77 return _max(pl->queryMax(L,R),pr->queryMax(L,R)); 78 } 79}*rt; 80int main() 81{ 82 #ifndef ONLINE_JUDGE 83 freopen("code/in.txt","r",stdin); 84 #endif 85 while (~scanf("%d%d",&n,&m)) 86 { 87 ms(a,0); 88 for ( int i = 0 ; i < n ; i++) scanf("%d",a+i); 89 rt = new Tree(0,n); 90 while (m--) 91 { 92 char opt[3]; 93 int x,y; 94 scanf("%s %d %d",opt,&x,&y); 95 x--; 96 if (opt[0]=='Q') 97 printf("%d\n",a[rt->queryMax(x,y)]); 98 else rt->change(x,y); 99 } 100 } 101 #ifndef ONLINE_JUDGE 102 fclose(stdin); 103 #endif 104 return 0; 105} 关于线段树的理解,见代码注释: