赞
踩
看见这种区间更新的题目,首先想到的就是线段树的区间更新。本体套用线段树的区间更新模板,更新的区间要二分去找。
- #include<stdio.h>
- #include<string.h>
- #include<algorithm>
- #define MAX 200009
- #define LL long long int
- using namespace std;
- LL a[MAX],cnt;
- struct Node {
- LL L;
- LL R;
- Node *pL;
- Node *pR;
- LL sum;
- LL add;
- } tree[4*MAX];
- LL n,m,k;
- LL ans;
- LL mid(struct Node *root) {
- return (root->L+root->R)/2;
- }
- LL Search(LL low,LL high,LL goal) {
- LL mid;
- while(low<=high){
- mid=(low+high)/2;
- if(goal<=a[mid]){
- high=mid-1;
- }else{
- low=mid+1;
- }
- }
- return low;
- }
- void build(struct Node *root,LL L,LL R) {
- root->L=L;
- root->R=R;
- root->add=0;
- if(L==R) return;
- LL midd=(L+R)/2;
- cnt++;
- root->pL=tree+cnt;
- build(root->pL,L,midd);
- cnt++;
- root->pR=tree+cnt;
- build(root->pR,midd+1,R);
- }
- void add(struct Node *root, LL L, LL R, LL val) {
- if(root->L==L&&root->R==R) {
- root->add+=val;
- return;
- }
- LL midd=mid(root);
- if(R<=midd)add(root->pL, L, R, val);
- else if(L>midd) add(root->pR, L, R, val);
- else {
- add(root->pL, L, mid(root), val);
- add(root->pR, mid(root)+1, R,val);
- }
- }
- LL query(struct Node *root, LL L, LL R){
- if(L == R){
- if(root->add>=k) ans ++;
- return 0;
- }
- LL midd=mid(root);
- add(root->pL, root->L, midd, root->add);
- add(root->pR, midd+1, root->R, root->add);
- root->add=0;
- if(R<=midd) return query(root->pL, L, R);
- else if(L>midd) return query(root->pR, L, R);
- else return query(root->pL, L, midd)+query(root->pR, midd+1, R);
- }
- int main() {
- int t=1;
- while(~scanf("%lld %lld %lld",&n,&m,&k)) {
- cnt=0;
- ans=0;
- for(LL i=1; i<=n; i++) {
- scanf("%lld",&a[i]);
- }
- sort(a+1,a+n+1);
- build(tree,1,n);
- LL first=2,second;
- for(LL i=0; i<m; i++) {
- scanf("%lld",&second);
- LL firsttmp=lower_bound(a + 1, a + n + 1, first) - a;
- LL secondtmp = upper_bound(a + 1, a + n + 1, second) - a - 1;
- if(firsttmp>secondtmp) continue;
- // if(secondtmp>n) secondtmp=n;
- scanf("%lld",&first);
- if(secondtmp < firsttmp) continue;
- add(tree,firsttmp,secondtmp,1);
- first++;
- }
- query(tree,1,n);
- printf("Case %d: %lld\n",t++, ans);
- }
- return 0;
- }
- /*
- 10 10 2
- 2 4 6 9 6 2 4 3 19 39
- 5 2
- 6 3
- 9 2
- 8 3
- 4 3
- 9 6
- 8 2
- Case 1: 6
- 9 8 2
- 3 7 2 9 8 4 3 2 1
- 5 2
- 6 3
- 9 2
- 8 4
- 9 2
- 10 6
- 46 5
- 10 3
- Case 2: 6
- 4 2 2
- 2 2 2 2
- 5 2
- 7 2
- Case 3: 0
- */
- #include<stdio.h>
- #include<string.h>
- #include<algorithm>
- using namespace std;
- #define MAX 100009
- #define inf 100000009
- int a[MAX],num[MAX],ans[MAX];
- int main(){
- int n,m,k,t=1;
- while(~scanf("%d %d %d",&n,&m,&k)){
- memset(num,0,sizeof(num));
- for(int i=1;i<=n;i++){
- scanf("%d",&a[i]);
- }
- sort(a+1,a+n+1);
- int first=2,second;
- for(int i=0;i<m;i++){
- scanf("%d",&second);
- int firsttmp=lower_bound(a+1,a+n+1,first)-a;
- int secondtmp=upper_bound(a+1,a+1+n,second)-a-1;
- num[firsttmp]++;
- num[secondtmp+1]--;
- scanf("%d",&first);
- first++;
- }
- int cnt=0;
- ans[0]=0;
- for(int i=1;i<=n;i++){
- ans[i]=ans[i-1]+num[i];
- if(ans[i]>=k) cnt++;
- }
- printf("Case %d: %d\n",t++, cnt);
- }
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。