【离散化树状数组】Nordic Collegiate Programming Contest G.Galactic Collegiate Programming Contest

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 int n,m;
 5 const int maxn=1e5+3;
 6 struct node
 7 {
 8     int team;
 9     int num;
10     int time;
11     int id;
12 }a[maxn];
13 int ans[maxn];
14 int b[maxn];
15 int c[maxn];
16 int s[maxn];
17 int team[maxn];
18 int tree[maxn];
19 int pre[maxn];
20 int lowbit(int x)
21 {
22     return x&(-x);
23 }
24 void add(int k,int x)
25 {
26     while(k<=m)
27     {
28         tree[k]+=x;
29         k+=lowbit(k);
30     }
31 }
32 int query(int k)
33 {
34     int res=0;
35     while(k)
36     {
37         res+=tree[k];
38         k-=lowbit(k);
39     }
40     return res;
41 }
42
43 bool cmp(node x,node y)
44 {
45     if(x.num!=y.num) return x.num>y.num;
46     else if(x.time!=y.time) return x.time<y.time;
47     else if(x.team!=1) return false;
48     //else return true;
49 }
50 int main()
51 {
52     while(~scanf("%d%d",&n,&m))
53     {
54         memset(b,0,sizeof(b));//题数
55         memset(c,0,sizeof(c));//罚时数
56         memset(tree,0,sizeof(tree));
57         memset(ans,0,sizeof(ans));
58         memset(pre,0,sizeof(pre));
59         int tmp;
60         for(int i=1;i<=m;i++)
61         {
62             scanf("%d%d",&a[i].team,&tmp);
63             a[i].num=b[a[i].team]+1;
64             b[a[i].team]=a[i].num;
65             a[i].time=c[a[i].team]+tmp;
66             c[a[i].team]=a[i].time;
67             a[i].id=i;
68             team[i]=a[i].team;
69         }
70         sort(a+1,a+1+m,cmp);
71         for(int i=1;i<=m;i++)
72         {
73             s[a[i].id]=i;
74         }
75         for(int i=1;i<=m;i++) pre[team[i]]=-1;
76         int r=m;
77         for(int i=1;i<=m;i++)
78         {
79             if(pre[team[i]]!=-1)
80                 add(pre[team[i]],-1);
81             pre[team[i]]=s[i];
82             if(team[i]==1)
83                 r=s[i]-1;
84             add(s[i],1);
85             ans[i]=query(r)+1;
86         }
87         for(int i=1;i<=m;i++) cout<<ans[i]<<endl;
88
89     }
90     return 0;
91 }

注意自定义cmp,最后没有return 导致wa

http://codeforces.com/gym/101572/attachments/download/6091/20172018-acmicpc-nordic-collegiate-programming-contest-ncpc-2017-en.pdf

时间: 2024-11-08 22:45:34

【离散化树状数组】Nordic Collegiate Programming Contest G.Galactic Collegiate Programming Contest的相关文章

hdu 3015 Disharmony Trees (离散化+树状数组)

Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 663    Accepted Submission(s): 307 Problem Description One day Sophia finds a very big square. There are n trees in the square. T

hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)

Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 481    Accepted Submission(s): 245 Problem Description You were driving along a highway when you got caught by the road p

poj 2299 Ultra-QuickSort 离散化 + 树状数组

题目链接:http://poj.org/problem?id=2299 离散化 + 树状数组 教科书例题般的题目 #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <vector> #include <stack> #include <set> #include

CodeForces 540E - Infinite Inversions(离散化+树状数组)

花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树状数组,但是发现那些没有交换的数也会产生逆序对数,但我没有算. 经明神提示, 把没有用到的数字段化成点.然后用树状数组算一下就好了. 然后我用一个数组记录每个点的长度.比如 <1,2><5,6>,1,2,3,4,5,6只有1,2,5,6用到了,那么离散化为1,2,3,4,5,f[1]=

【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组

原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html 题目描述 The cows have once again tried to form a startup company, failing to remember from past experience that cows make terrible managers!The cows, conveniently numbered 1-N1-N (1≤N≤100,000), organi

POJ 2299 Ultra-QuickSort (离散化+树状数组)

题目链接:POJ 2299 Ultra-QuickSort 求一串序列相邻连个元素交换多少后,是一串上升的序列. 思路:求该串序列的逆序数,数据比较大,要离散化. AC代码: #include<stdio.h> #include<string.h> #include<set> #include<map> #include<algorithm> #define ll __int64 using namespace std; const ll max

2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ&#39;s Salesman 【离散化+树状数组维护区间最大值】

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 919    Accepted Submission(s): 290 Problem Description YJJ is a salesman who h

Codeforces Round #629 (Div. 3) F - Make k Equal (离散化 树状数组维护前缀和)

https://codeforces.com/contest/1328/problem/F 首先把a数组处理成pair对(num,cnt),表示数字num有cnt个,然后按num升序排序离散化一下. 对于一个数x,若想使得小于x的数字都变成x,必须先把所有小于x的数变成x-1,然后再+1变成x. 同理,要使得大于x的数变成x,必须把所有大于x的数字变成x+1,然后再-1变成x. 以上是题意所要求的必须操作. 思路: 1. 用f[i]数组记录离散化后前i大的数字的总数,那么对于任意第i大数字,可以

codeforces 652D Nested Segments 离散化+树状数组

题意:给你若干个区间,询问每个区间包含几个其它区间 分析:区间范围比较大,然后离散化,按右端点排序,每次更新树状数组中的区间左端点,查询区间和 注:(都是套路) #include<cstdio> #include<cstring> #include<queue> #include<cstdlib> #include<algorithm> #include<vector> #include<cmath> using name

poj 2299(离散化+树状数组)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 53777   Accepted: 19766 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin