模板 codeforces-669E cdq分治

Posted xiaobuxie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模板 codeforces-669E cdq分治相关的知识,希望对你有一定的参考价值。

题目传送门:https://vjudge.net/contest/325125#problem/A

题意:三种操作:1 t x  在第t秒x点的值+1  ,     2 t x  在第t秒x点的值-1  ,  3 t x  查询在t秒x点处的值

感觉用结构体写好看一点。然后记得归并。坑点都在注释里了。

技术图片
 1 /*************************************************************************
 2     > File Name: codeforces669E.cpp
 3 # File Name: codeforces669E.cpp
 4 # Author : xiaobuxie
 5 # QQ : 760427180
 6 # Email:760427180@qq.com
 7 # Created Time: 2019年09月10日 星期二 22时43分43秒
 8  ************************************************************************/
 9 
10 #include<iostream>
11 #include<cstdio>
12 #include<map>
13 #include<cmath>
14 #include<cstring>
15 #include<set>
16 #include<queue>
17 #include<vector>
18 #include<algorithm>
19 using namespace std;
20 typedef long long ll;
21 #define inf 0x3f3f3f3f
22 #define pq priority_queue<int,vector<int>,greater<int> >
23 ll gcd(ll a,ll b)
24     if(a<b) return gcd(b,a);
25     return b==0?a:gcd(b,a%b);
26 
27 
28 const int N=1e5+9;
29 struct node
30     int tim,pos,ord,ty,ans;
31     bool operator<(const node& o)
32         return ord<o.ord;
33     
34 A[N],B[N];
35 map<int,int> mp;
36 
37 void cdq(int l,int r)
38     if(l==r) return;
39     int m=(l+r)>>1;
40     cdq(l,m); cdq(m+1,r);
41     for(int i=l,t1=l,t2=m+1;i<=r;++i)
42         if(t1<=m && (A[t1].tim<=A[t2].tim || t2>r))
43             if(A[t1].ty==1) mp[A[t1].pos]++;
44             if(A[t1].ty==2) mp[A[t1].pos]--;
45             B[i]=A[t1];  //赋值要放在操作之后
46             ++t1;
47         
48         else
49             if(A[t2].ty==3) A[t2].ans+=mp[A[t2].pos];
50             B[i]=A[t2];   //赋值要放在操作之后,因为B待会要给回A
51             ++t2;
52         
53     
54     for(int i=l;i<=m;++i)
55         if(A[i].ty==1) mp[A[i].pos]--;
56         if(A[i].ty==2) mp[A[i].pos]++;
57     
58     for(int i=l;i<=r;++i) A[i]=B[i]; //不要忘了
59 
60 int main()
61     int n; scanf("%d",&n);
62     for(int i=1;i<=n;++i)
63         scanf("%d %d %d",&A[i].ty,&A[i].tim,&A[i].pos);
64         A[i].ord=i; 
65     
66     cdq(1,n);
67     sort(A+1,A+1+n);
68     for(int i=1;i<=n;++i) if(A[i].ty==3) printf("%d\n",A[i].ans);
69     return 0;
70 
View Code

 

以上是关于模板 codeforces-669E cdq分治的主要内容,如果未能解决你的问题,请参考以下文章

CDQ分治模板题

CDQ

CDQ分治嵌套模板:多维偏序问题

模板CDQ分治

模板:CDQ分治

CDQ 分治算法模板