Submission #955970

# Submission time Handle Problem Language Result Execution time Memory
955970 2024-03-31T18:16:24 Z chrisvilches Dragon 2 (JOI17_dragon2) C++14
100 / 100
642 ms 11904 KB
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<stdlib.h>
#include<complex>
using namespace std;

const int MN=30000,MQ=100000,BS=15,RQ=300;


typedef complex<long long> point;

long long op(point P1,point P2)
{
  return imag((conj(P1)*P2));
}

namespace std{
  bool operator<(point P1,point P2)
  {
    return op(P1,P2)>0;
  }
};



template<class T> class sumBIT
{
private:
  T *bit;
  int S;
public:
  sumBIT(int S0){
    S=S0;
    bit=(T *)calloc(sizeof(T),1<<S);
  }
  void add(int i,T k){
    int p=i+1;
    while(p<1<<S){
      bit[p]+=k;
      p+=p&-p;
    }
  }
  T sum(int p){
    T ret=0;
    while(p>0){
      ret+=bit[p];
      p-=p&-p;
    }
    return ret;
  }
  T rag(int a,int b){
    return sum(b)-sum(a);
  }
};


int main()
{
  int n,m;
  scanf("%d%d",&n,&m);
  static point P[MN];
  static int C[MN];
  for(int i=0;i<n;i++){
    long long x,y;
    scanf("%lld%lld%d",&x,&y,C+i);
    P[i]=point(x,y);
    C[i]--;
  }
  long long d1,e1,d2,e2;
  scanf("%lld%lld%lld%lld",&d1,&e1,&d2,&e2);
  point X=point(d1,e1),Y=point(d2,e2);
  static bool F[MN];
  for(int i=0;i<n;i++){
    F[i]=(op(Y-X,P[i]-X)>0);
  }
  static point A[MN];
  static pair<int,point> AA[MN];
  for(int i=0;i<n;i++){
    A[i]=(F[i]?P[i]-Y:Y-P[i]);
    AA[i]=make_pair(C[i],A[i]);
  }
  sort(AA,AA+n);
  static int D[MN];
  for(int i=0;i<n;i++){
    D[i]=lower_bound(AA,AA+n,make_pair(C[i],A[i]))-AA;
  }
  static int T[MN+1];
  for(int i=0;i<=m;i++){
    T[i]=lower_bound(AA,AA+n,make_pair(i,Y-X))-AA;
  }
  static pair<point,int> Pr[MN];
  for(int i=0;i<n;i++){
    Pr[i]=make_pair(F[i]?P[i]-X:X-P[i],i);
  }
  sort(Pr,Pr+n);
  int Q;
  scanf("%d",&Q);
  static int as[MQ],bs[MQ];
  for(int i=0;i<Q;i++){
    scanf("%d%d",as+i,bs+i);
    as[i]--,bs[i]--;
  }
  static vector<int> V[MN],VN[MN];
  for(int i=0;i<Q;i++){
    V[as[i]].push_back(bs[i]);
    VN[as[i]].push_back(i);
  }
  static vector<int> V2[MN],VN2[MN];
  for(int i=0;i<Q;i++){
    if(V[as[i]].size()>=RQ){
      V2[bs[i]].push_back(as[i]);
      VN2[bs[i]].push_back(i);
    }
  }
  static long long ans[MQ]={0};
  sumBIT<int> seg0(BS),seg1(BS);
  for(int i=0;i<n;i++){
    if(!F[i]){
      seg1.add(D[i],1);
    }
  }
  for(int t=0;t<n;t++){
    int i=Pr[t].second;
    int a=C[i];
    if(F[i]){
      if(V[a].size()<RQ){
	for(int k=0;k<V[a].size();k++){
	  int b=V[a][k],l=VN[a][k];
	  int L=lower_bound(AA,AA+n,make_pair(b,A[i]))-AA;
	  ans[l]+=seg0.rag(L,T[b+1])+seg1.rag(T[b],L);
	}
      }
      seg0.add(D[i],1);
    }
    else{
      if(V[a].size()<RQ){
	for(int k=0;k<V[a].size();k++){
	  int b=V[a][k],l=VN[a][k];
	  int L=lower_bound(AA,AA+n,make_pair(b,A[i]))-AA;
	  ans[l]+=seg0.rag(L,T[b+1])+seg1.rag(T[b],L);
	}
      }
      seg1.add(D[i],-1);
    }
  }
  sumBIT<int> seg2(BS),seg3(BS);
  for(int i=0;i<n;i++){
    seg2.add(D[i],1);
  }
  for(int t=0;t<n;t++){
    int i=Pr[t].second;
    int b=C[i];
    if(F[i]){
      for(int k=0;k<V2[b].size();k++){
	int a=V2[b][k],l=VN2[b][k];
	int L=lower_bound(AA,AA+n,make_pair(a,A[i]))-AA;
	ans[l]+=seg2.rag(T[a],L);
      }
      seg2.add(D[i],-1);
      seg3.add(D[i],1);
    }
    else{
      for(int k=0;k<V2[b].size();k++){
	int a=V2[b][k],l=VN2[b][k];
	int L=lower_bound(AA,AA+n,make_pair(a,A[i]))-AA;
	ans[l]+=seg3.rag(L,T[a+1]);
      }
      seg2.add(D[i],-1);
      seg3.add(D[i],1);
    }
  }
  for(int t=0;t<Q;t++){
    printf("%lld\n",ans[t]);
  }
  return 0;
}

Compilation message

dragon2.cpp: In function 'int main()':
dragon2.cpp:135:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  135 |  for(int k=0;k<V[a].size();k++){
      |              ~^~~~~~~~~~~~
dragon2.cpp:145:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |  for(int k=0;k<V[a].size();k++){
      |              ~^~~~~~~~~~~~
dragon2.cpp:162:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |       for(int k=0;k<V2[b].size();k++){
      |                   ~^~~~~~~~~~~~~
dragon2.cpp:171:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  171 |       for(int k=0;k<V2[b].size();k++){
      |                   ~^~~~~~~~~~~~~
dragon2.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |   scanf("%d%d",&n,&m);
      |   ~~~~~^~~~~~~~~~~~~~
dragon2.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |     scanf("%lld%lld%d",&x,&y,C+i);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
dragon2.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |   scanf("%lld%lld%lld%lld",&d1,&e1,&d2,&e2);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dragon2.cpp:105:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |   scanf("%d",&Q);
      |   ~~~~~^~~~~~~~~
dragon2.cpp:108:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |     scanf("%d%d",as+i,bs+i);
      |     ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 5 ms 4952 KB Output is correct
3 Correct 20 ms 5252 KB Output is correct
4 Correct 56 ms 7196 KB Output is correct
5 Correct 44 ms 7288 KB Output is correct
6 Correct 4 ms 4952 KB Output is correct
7 Correct 4 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 7068 KB Output is correct
2 Correct 43 ms 7076 KB Output is correct
3 Correct 25 ms 7004 KB Output is correct
4 Correct 21 ms 6940 KB Output is correct
5 Correct 23 ms 7260 KB Output is correct
6 Correct 20 ms 7004 KB Output is correct
7 Correct 23 ms 6948 KB Output is correct
8 Correct 20 ms 7004 KB Output is correct
9 Correct 15 ms 7004 KB Output is correct
10 Correct 16 ms 6944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 5 ms 4952 KB Output is correct
3 Correct 20 ms 5252 KB Output is correct
4 Correct 56 ms 7196 KB Output is correct
5 Correct 44 ms 7288 KB Output is correct
6 Correct 4 ms 4952 KB Output is correct
7 Correct 4 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 22 ms 7068 KB Output is correct
12 Correct 43 ms 7076 KB Output is correct
13 Correct 25 ms 7004 KB Output is correct
14 Correct 21 ms 6940 KB Output is correct
15 Correct 23 ms 7260 KB Output is correct
16 Correct 20 ms 7004 KB Output is correct
17 Correct 23 ms 6948 KB Output is correct
18 Correct 20 ms 7004 KB Output is correct
19 Correct 15 ms 7004 KB Output is correct
20 Correct 16 ms 6944 KB Output is correct
21 Correct 24 ms 7252 KB Output is correct
22 Correct 41 ms 6912 KB Output is correct
23 Correct 243 ms 7252 KB Output is correct
24 Correct 529 ms 9560 KB Output is correct
25 Correct 98 ms 9528 KB Output is correct
26 Correct 81 ms 10380 KB Output is correct
27 Correct 24 ms 8096 KB Output is correct
28 Correct 23 ms 8052 KB Output is correct
29 Correct 63 ms 11900 KB Output is correct
30 Correct 224 ms 10064 KB Output is correct
31 Correct 61 ms 10320 KB Output is correct
32 Correct 75 ms 11668 KB Output is correct
33 Correct 612 ms 10068 KB Output is correct
34 Correct 63 ms 10324 KB Output is correct
35 Correct 73 ms 11904 KB Output is correct
36 Correct 59 ms 10092 KB Output is correct
37 Correct 67 ms 10324 KB Output is correct
38 Correct 227 ms 11420 KB Output is correct
39 Correct 568 ms 11168 KB Output is correct
40 Correct 642 ms 10308 KB Output is correct
41 Correct 62 ms 11628 KB Output is correct
42 Correct 68 ms 11632 KB Output is correct
43 Correct 78 ms 11600 KB Output is correct
44 Correct 34 ms 8276 KB Output is correct
45 Correct 28 ms 8280 KB Output is correct
46 Correct 28 ms 8436 KB Output is correct
47 Correct 27 ms 8104 KB Output is correct
48 Correct 27 ms 8284 KB Output is correct
49 Correct 27 ms 8272 KB Output is correct